Skip to content

Instantly share code, notes, and snippets.

@derekp7
Last active May 11, 2024 04:10
Show Gist options
  • Save derekp7/9978986 to your computer and use it in GitHub Desktop.
Save derekp7/9978986 to your computer and use it in GitHub Desktop.
RPC in Bash (rpcsh)

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[@]}\"
"
}
@coderofsalvation
Copy link

coderofsalvation commented Nov 3, 2022

omg this completely blew my mind!

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

This is exactly what I ended up with for deploying our servers: a shellscript called from a laptop which also (needs to) exist on the servers.
Indeed, the downside of this is that every minor script-tweak requires updating all servers.

With your $(declare -f ...) approach I can teleport the whole script during runtime:

cluster.sh

#!/bin/bash
mycluster="serverA serverB"

rpc(){
  foo(){ echo hello from $(hostname) $*; }
  "$@"
}

for server in $mycluster; do
  ssh $server "$(declare -f rpc); rpc foo $*" 
done
$ ./cluster.sh 123
hello from serverA 123
hello from serverB 123

ps. I've created a tweet to raise awareness https://twitter.com/coderofsalvatio/status/1588110979560361985

@mralusw
Copy link

mralusw commented May 11, 2024

Also in https://github.com/derekp7/snebu/blob/main/snebu-client along with rpcsudo. Interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment