I have the following script that SSH to a server with a key and makes a lot of stuff there.
#!/usr/bin/env bash
ssh -i mykey.pem myuser@SERVER_IP << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
(which I run it with sh scriptname.sh
)
Now I want to to the same in another server, so I've to SSH to two different servers (ip_1
and ip_2
) with two different .pem
files (mykey1.pem
and mykey2.pem
).
So far I know how to loop the ips as follows:
#!/usr/bin/env bash
ip_list="ip_1 ip_2"
for ip in $ip_list; do
ssh -i mykey.pem myuser@$ip << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done
but now I would like to loop also to get the proper pem file. How can I archieve this? Maybe with another list? Can someone provide me an elegant solution?
ip_1
should usemykey1.pem
ip_2
should usemykey2.pem
Thanks in advance
Best Answer
Since you're using bash, you can use associative arrays:
Note that associative arrays, unlike regular indexed arrays, are not saved in a specific order, so there is no guarantee that
ip_1
will be processed beforeip_2
.If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:
Then, use this script:
And run it with:
But if you go that route, Stéphane's approach is better.