Skip to content

Instantly share code, notes, and snippets.

@andreipak
Last active November 8, 2019 04:52
Show Gist options
  • Save andreipak/7314836 to your computer and use it in GitHub Desktop.
Save andreipak/7314836 to your computer and use it in GitHub Desktop.
Attempt to roll two useful pssh tools (parallel-ssh and parallel-scp) into reusable scripts with our old friend xargs. Link: http://90kts.com/2010/10/31/parallel-ssh-for-osx/
#!/bin/bash
usage()
{
cat << EOF
Usage: $0 [OPTIONS] -h hosts.txt local remote
-h hosts file (each line "host")
-r recusively copy directories (OPTIONAL)
-l username
-p max number of parallel threads (OPTIONAL)
-O SSH options (OPTIONAL)
Example: parallel-scp -h hosts.txt -l koopmans foo /home/koopmans/bar
EOF
}
HOSTS=
RECURSIVE=
USER=
PARALLEL=10
OPTIONS=
while getopts "h:rl:p#t#O:" OPTION
do
case $OPTION in
h)
HOSTS=$OPTARG
;;
r)
RECURSIVE=-r
;;
l)
USER=$OPTARG
;;
p)
PARALLEL=$OPTARG
;;
O)
OPTIONS=$OPTARG
;;
?)
usage
exit
;;
esac
done
shift $((OPTIND-1))
LOCAL=$1
REMOTE=$2
if [[ -h $HOSTS ]]
then
usage
exit 1
fi
cat $HOSTS | awk '{print $1}' | xargs -P $PARALLEL -I{} scp $RECURSIVE $OPTIONS $LOCAL $USER@'{}':$REMOTE
#!/bin/bash
usage()
{
cat << EOF
Usage: $0 [OPTIONS] -h hosts.txt prog [arg0] ...
-h hosts file (each line "host")
-l username
-p max number of parallel threads (OPTIONAL)
-O SSH options (OPTIONAL)
Example: parallel-ssh -h hosts.txt -l koopmans uptime
EOF
}
HOSTS=
USER=
PARALLEL=10
OPTIONS=
while getopts "h:l:p#O:" OPTION
do
case $OPTION in
h)
HOSTS=$OPTARG
;;
l)
USER=$OPTARG
;;
p)
PARALLEL=$OPTARG
;;
O)
OPTIONS=$OPTARG
;;
?)
usage
exit
;;
esac
done
shift $((OPTIND-1))
PROG=$@
if [[ -h $HOSTS ]]
then
usage
exit 1
fi
cat $HOSTS | awk '{print $1}' | xargs -P $PARALLEL -I{} ssh $OPTIONS $USER@'{}' $PROG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment