3

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 use mykey1.pem
  • ip_2 should use mykey2.pem

Thanks in advance

  • Don't use mykey1.pem but ip_1.pem. Then it's easy: ssh -i $ip.pem myuser@$ip << 'ENDSSH' – stoney Mar 26 at 11:09
6

Since you're using bash, you can use associative arrays:

#!/usr/bin/env bash

declare -A ip_list=(["ip_1"]="mykey1.pem" ["ip_2"]="mykey2.pem")

for ip in "${!ip_list[@]}"; do
  ssh -i  "${ip_list[$ip]}" myuser@"$ip" << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done

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 before ip_2.


If you need to use a simple, POSIX compatible shell, create a file with the ip and key files, one per line:

$ cat iplist.txt
ip1 mykey1.pem
ip2 mykey2.pem

Then, use this script:

#!/bin/sh

while read -r ip key; do
    ssh -i "$key" myuser@"$ip" << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH
done 

And run it with:

sh /path/to/script <  /path/to/iplist.txt

But if you go that route, Stéphane's approach is better.

  • I'm running it with sh (I cant change that), so it seems I cannot use arrays. – Avión Mar 26 at 11:13
  • 3
    @Avión what do you mean? You have #!/usr/bin/env bash in your script. Just make the script executable (chmod a+x /path/to/script) and then run it directly: /path/to/script. Alternatively, run it with bash /path/to/script. The only reason you're using sh is because you're calling it with sh /path/to/script. – terdon Mar 26 at 11:15
6

One way to do it is to use a while IFS=, read -r loop on a csv here-document.

#! /bin/sh -
while IFS=, read <&3 -r ip key; do
  ssh -i "$key" "$ip" << ENDSSH
  ...
ENDSSH
done 3<< ENDCSV
10.0.0.1,p1.pem
10.0.0.2,p2.pem
ENDCSV

Then you don't even need to require users to install bash. If portability is not a concern, you could use zsh instead which supports looping over more than one variable.

#! /usr/bin/env zsh
for ip key (
  10.0.0.1 p1.pem
  10.0.0.2 p2.pem
) ssh -i $key $ip << ENDSSH
  ...
ENDSSH
1

Your original script can be made slightly more portable via set built-in, plus have ip and key go together as one string, colon separated. We later can use prefix and suffix removal to extract corresponding entry

#!/usr/bin/env bash

# Set positional parameters
# Example ip addresses
set -- 192.168.0.1:mykey1 192.168.1.1:mykey2

# iterating without specifying 'in' assumes positiona parameters
for host; do
    ssh -i ${host##*:}.pem myuser@${host%%:*} << 'ENDSSH'
[A LOT OF STUFF]
ENDSSH

done
  • 2
    set is not really helping here. You could just as well do for host in 192.168.0.1:mykey1 192.168.1.1:mykey2; do. Note that : is not the best choice of delimiter as it's found in IPv6 addresses. You also need to quote parameter expansions to prevent split+glob – Stéphane Chazelas Mar 26 at 17:14
  • All fair points. Semicolon could serve a better delimiter, maybe ? – Sergiy Kolodyazhnyy Mar 26 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.