Skip to content

Instantly share code, notes, and snippets.

@checkaayush
Last active May 14, 2022 07:58
Show Gist options
  • Save checkaayush/7271e96e0be409a05a9f to your computer and use it in GitHub Desktop.
Save checkaayush/7271e96e0be409a05a9f to your computer and use it in GitHub Desktop.
SSH: Run local bash script on remote server
#!/bin/bash
# Run local bash script on remote server via SSH connection
# Login variables
USER="username"
HOST="host_address" #Remote host
PATH_TO_SCRIPT="path/to/your_script.sh"
ssh -l $USER $HOST 'bash -s' < $PATH_TO_SCRIPT
## To run just a command on remote machine:
# COMMAND="echo 'Hi there!'"
# ssh -l $USER $HOST $COMMAND
## If you don't want to manually insert authentication password:
## Requires 'sshpass' locally (Debian-based systems: apt-get install sshpass)
## Note: This is a less secure way; You might want to use 'openssl' for password encryption and decryption
# PASSWD="your_password"
# sshpass -p $PASSWD ssh -l $USER $HOST 'bash -s' < $PATH_TO_SCRIPT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment