Saturday, March 30, 2013

Running script on remote systems

Instead of copying script to all systems, say 200, to run, following will make things easier.

1. Running script without argument on remote systems
ssh user@remote /bin/bash < scriptname.sh

2. Running script with arguments on remote systems.
runremote.sh user remote scriptname.sh -arguments

runremote.sh is shown below.

#!/bin/bash
# runremote.sh (revised, not dependent upon /dev/stdin)
# usage: runremote.sh remoteuser remotehost localscript arg1 arg2 ...

if [ $# -lt 3 ]; then
        echo "Usage: $0 remoteuser remotehost localscript arg1 arg2 ..."
        exit
fi

realscript=$3
user=$1
host=$2
shift 3

# escape the arguments
declare -a args

count=0
for arg in "$@"; do
  args[count]=$(printf '%q' "$arg")
  count=$((count+1))
done

{
  printf '%s\n' "set -- ${args[*]}"
  cat "$realscript"
} | ssh $user@$host "bash -s"

Details see  http://backreference.org/2011/08/10/running-local-script-remotely-with-arguments/

No comments: