-
-
Save DannyBen/56e5803e3c83b35de0fd to your computer and use it in GitHub Desktop.
Run a ruby or shell script remotely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run a ruby or shell script remotely | |
# | |
# Install: | |
# - Put the file in your home directory | |
# - Source it from your .bashrc: | |
# if [ -f $HOME/ssx ]; then source $HOME/ssx; fi | |
ssx() { | |
show_usage() { | |
echo "Run a ruby or shell script remotely" | |
echo "Usage: ssx [HOST] SCRIPT" | |
echo "Either provide a HOST or add '#- HOST' as the first line of your script" | |
} | |
if [ $# -lt 1 ]; then | |
show_usage | |
return | |
fi | |
if [ $# -lt 2 ]; then | |
script=$1 | |
header=$(head -n 1 $script) | |
if [[ $header =~ ^#-\s*(.*) ]]; then | |
host=${BASH_REMATCH[1]} | |
else | |
show_usage | |
return | |
fi | |
else | |
host=$1 | |
script=$2 | |
fi | |
if [[ $script == *.rb ]]; then | |
ssh $host 'ruby' < "$script" | |
return | |
fi | |
if [[ $script == *.sh ]]; then | |
ssh $host 'bash' < "$script" | |
return | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment