Skip to content

Instantly share code, notes, and snippets.

@psachin
Last active April 18, 2016 05:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psachin/35b4c85688dd9f23a800985766dbd237 to your computer and use it in GitHub Desktop.
Save psachin/35b4c85688dd9f23a800985766dbd237 to your computer and use it in GitHub Desktop.
Objective is to ssh IPaddr in an array and use the consecutive IPaddr in the conf file. It should complete one full loop. See example in the comment.
#!/usr/bin/env bash
# Objective is to ssh IPaddr in an array and use the consecutive IPaddr in the conf file.
# Something like this:
# ssh 192.168.0.1 -C "echo 192.168.0.2 >> ~/ips"
# ssh 192.168.0.2 -C "echo 192.168.0.3 >> ~/ips"
# ssh 192.168.0.3 -C "echo 192.168.0.4 >> ~/ips"
# ssh 192.168.0.4 -C "echo 192.168.0.1 >> ~/ips"
x="192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4"
arr=($(echo $x | tr " " "\n"))
len=${#arr[@]}
for (( i=0; i<${len}; i++ ));
do
if [ $i -lt $(( $len - 1 )) ];
then
SSH_IP=${arr[$i]}
CONFIG_IP=${arr[$i+1]}
else
SSH_IP=${arr[$i]}
CONFIG_IP=${arr[0]}
fi
ssh ${SSH_IP} -C "echo ${CONFIG_IP} >> ~/ips"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment