Skip to content

Instantly share code, notes, and snippets.

@gene1wood
Created June 13, 2014 17:55
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 gene1wood/962613622884ea94f2db to your computer and use it in GitHub Desktop.
Save gene1wood/962613622884ea94f2db to your computer and use it in GitHub Desktop.
Some old bash functions for doing parallel ssh and scp, pushing and fetching files through jump hosts and deep escaping
function multiscp {
hostlist=$1
filename=$2
tempfiletemplate="/tmp/`basename $0`-XXXXXX"
pidlistfile="`mktemp $tempfiletemplate`"
exitstatus=0
for host in $hostlist; do
scp $filename $host: &
echo "$!" >>$pidlistfile
done
for pid in `cat $pidlistfile`; do
if ! wait $pid; then
let exitstatus++
fi
done
rm $pidlistfile
return $exitstatus
}
function multissh {
hostlist=$1
remotecommand=$2
tempfiletemplate="/tmp/`basename $0`-XXXXXX"
pidlistfile="`mktemp $tempfiletemplate`"
exitstatus=0
for host in $hostlist; do
bash -c "ssh $host \"$remotecommand\" 2>&1 | sed -e \"s/^/$host: /\"
test ${PIPESTATUS[0]} -eq 0 ||
(echo \"Encountered an error running command on to $host\" && false)" &
echo "$!" >>$pidlistfile
done
for pid in `cat $pidlistfile`; do
if ! wait $pid; then
let exitstatus++
fi
done
rm $pidlistfile
return $exitstatus
}
function push {
remotehost=$1
localfile=$2
adm=$3
jump=$4
if ssh -o ConnectTimeout=1 $remotehost 'true' >/dev/null; then
cat < $localfile | ssh $remotehost "
cat > `basename $localfile`
"
elif ssh -o ConnectTimeout=1 $adm 'true' >/dev/null; then
cat < $localfile | ssh -A $adm "
ssh $remotehost \"
cat > `basename $localfile`
\"
"
else
cat < $localfile | ssh -A $jump "
ssh -A $adm \"
ssh $remotehost \\\"
cat > `basename $localfile`
\\\"
\"
"
fi
}
function fetch {
path="$1"
remotefile="$2"
remotehost="$3"
ssh $remotehost "
cat \"$path/$remotefile\"
" > "$remotefile"
if [ ! -e "$remotefile" ]; then
exit 1
fi
}
escaping_example {
ssh -A jump.example.com "
ssh -A adm1.example.com \"
xapply -P9 \\\"
scp $rpmfilename %1: &&
ssh %1 \\\\\\\"test -e $rpmfilename &&
sudo /path/to/script.sh $rpmfilename
\\\\\\\" 2>&1 | sed -e 's/^/%1: /'; test ${PIPESTATUS[0]} -eq 0 ||
(echo \\\\\\\"Encountered an error installing to %1\\\\\\\" && false)
\\\" $targets
\"
"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment