Skip to content

Instantly share code, notes, and snippets.

@mrmichalis
Forked from derekp7/gist:9978986
Created April 5, 2014 03:03
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 mrmichalis/9986938 to your computer and use it in GitHub Desktop.
Save mrmichalis/9986938 to your computer and use it in GitHub Desktop.

Let's say you have a Bash shell script, and you need to run a series of operations on another system (such as via ssh). There are a couple of ways to do this.

First, you can stage a child script on the remote system, then call it, passing along appropriate parameters. The problem with this is you will need to manually keep the remote script updated whenever you change it -- could be a bit of a challenge when you have something to execute on a number of remote servers (i.e., you have a backup script running on a central host, and it needs to put remote databases in hot backup mode before backing them up).

Another option is to embed the commands you want to run remotely within the ssh command line. But then you run into issues with escaping special characters, quoting, etc. This is ok if you only have a couple commands to run, but if it is a complex piece of Bash code, it can get a bit unwieldy.

So, to solve this, you can use a technique called rpcsh -- rpc in shell script, as follows:

First, place the remote code segment into a shell function:

hello() {
    echo "Hello, world, I'm coming from $(uname -n)."
}

Now, using "$(declare -f)", you can push this function to a remote host and execute it via ssh, as follows:

ssh user@rmthost "$(declare -f hello); hello"

Output:

Hello, world, I'm coming from rmthost.

Bash will take care of all the escaping and quoting, load the function up, and leave the function in the environment to be executed. You can also push variables and arrays over, using "$(declare -p varname)". You can also list multiple functions and variables, if the main function relies on them, by listing them individually.

To make this more automatic, check out the attached rpcsh() function.

# rpcsh -- Runs a function on a remote host
# This function pushes out a given set of variables and functions to
# another host via ssh, then runs a given function with optional arguments.
# Usage:
# rpcsh -h remote_host [ -p ssh-port ] -u remote_login -v "variable list" \
# -f "function list" -m mainfunc
#
# The "function list" is a list of shell functions to push to the remote host
# (including the main function to execute, and any functions that it calls)
# Use the "variable list" to send a group of variables to the remote host.
# Finally "mainfunc" is the name of the function (from "function list")
# to execute on the remote side. Any additional parameters specified gets
# passed along to mainfunc.
rpcsh() {
if ! args=("$(getopt -l "rmthost:,rmthostport:,rmtlogin:,pushvars:,pushfuncs:,rmtmain:" -o "h:p:u:v:f:m:A" -- "$@")")
then
exit 1
fi
sshvars=( -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null )
eval set -- "${args[@]}"
while [ -n "$1" ]
do
case $1 in
-h|--rmthost) rmthost=$2; shift; shift;;
-p|--rmtport) sshvars=( "${sshvars[@]}" -p $2 ); shift; shift;;
-u|--rmtlogin) rmtlogin=$2; shift; shift;;
-v|--pushvars) pushvars=$2; shift; shift;;
-f|--pushfuncs) pushfuncs=$2; shift; shift;;
-m|--rmtmain) rmtmain=$2; shift; shift;;
-A) sshvars=( "${sshvars[@]}" -A ); shift;;
-i) sshvars=( "${sshvars[@]}" -i $2 ); shift; shift;;
--) shift; break;;
esac
done
rmtargs=( "$@" )
ssh ${sshvars[@]} ${rmtlogin}@${rmthost} "
$(declare -p rmtargs 2>/dev/null)
$([ -n "$pushvars" ] && declare -p $pushvars 2>/dev/null)
$(declare -f $pushfuncs 2>/dev/null)
$rmtmain \"\${rmtargs[@]}\"
"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment