Skip to content

Instantly share code, notes, and snippets.

@DannyBen
Last active February 25, 2016 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DannyBen/56e5803e3c83b35de0fd to your computer and use it in GitHub Desktop.
Save DannyBen/56e5803e3c83b35de0fd to your computer and use it in GitHub Desktop.
Run a ruby or shell script remotely
# 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