Home > Software engineering >  How to launch a script for a list of ip addresses (bash script)
How to launch a script for a list of ip addresses (bash script)

Time:02-01

i have a problem with my bash shell script. That's 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" an script(restart-launcher.sh) but when i launch it , it only lists me 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")

Can you please Help me ?

I need to create a bash script that work in Linux

CodePudding user response:

#!/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.

CodePudding user response:

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

  •  Tags:  
  • Related