Skip to content

Instantly share code, notes, and snippets.

@rzrbld
Last active September 2, 2021 09:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rzrbld/bc32a9d4f443383d829c to your computer and use it in GitHub Desktop.
Save rzrbld/bc32a9d4f443383d829c to your computer and use it in GitHub Desktop.
bash script to ssh multiple servers in a loop
#!/bin/bash
for server in $(cat hosts.txt); do
IP=`echo "$server" | awk 'BEGIN {RS = "|"}; END {print $1}'`;
HOST=`echo "$server" | awk 'BEGIN {FS = "|"}; END {print $1}'`;
USER=`echo $HOST | sed 's/tst-centos-//g;s/\-.//g;'`;
echo "$HOST($IP)"
echo $USER
ssh -nt $USER@$IP -i ssh/$USER 'sudo yum -y remove test-pkg && sudo rm -rf /var/lib/test'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment