IppoMuto
IppoMuto

Reputation: 13

How to launch a script for a list of ip addresses (bash script)

i have a problem with my bash shell script. This is my code:

#!/bin/bash/

while read line  
do   
   echo -e "$line
"  
sleep 5;
done < Ip.txt

sshpass -p 'Password' ssh -o StrictHostKeyChecking=no root@$ip -t "cd /exemple/ && sh restart-launcher.sh;exec \$SHELL -l"

My script allows to launch for each ip (Ip.txt) in the folder "exemple" a script(restart-launcher.sh) but when I launch it , it only lists the ip without taking this part into account:

sshpass -p 'Password' ssh -o StrictHostKeyChecking=no root@$ip -t "cd /folders/ && sh restart-launcher.sh;exec \$SHELL -l"

How do I create a bash script that works in Linux?

Upvotes: 1

Views: 1362

Answers (2)

Regretful
Regretful

Reputation: 335

If we assume that "Ip.txt" contains a list of only IPs perhaps what you mean to do is use sshpass inside the while-loop so it runs for each IP in the .txt file.

#!/bin/bash/

while read line  
do   
   echo -e "$line
"  
sshpass -p 'Password' ssh -o StrictHostKeyChecking=no root@$line -t "cd /exemple/ && sh restart-launcher.sh;exec \$SHELL -l"
sleep 5;
done < Ip.txt

sshpass has been moved into the while loop and $ip replaced with $line

Upvotes: 0

Kaffe Myers
Kaffe Myers

Reputation: 464

#!/bin/bash/
while read -r line; do   
    echo -e "$line\n"
    sshpass -p 'Password' ssh -o StrictHostKeyChecking=no root@$line -t \
        "cd /exemple/ && sh restart-launcher.sh;exec \$SHELL -l"
    sleep 5
done < Ip.txt

Now, we could have a discussion about using sshpass (or rather, why you shouldn't), but it feels out of scope.

So, all commands you wish to be looped over need to be inside the loop. As you read sets the variable $line, that is what you need to use. In your example, you used the variable $ip which you haven't set anywhere.

Upvotes: 1

Related Questions