1

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?

Joundill
  • 6,828
  • 12
  • 36
  • 50
IppoMuto
  • 13
  • 2

2 Answers2

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

Kaffe Myers
  • 424
  • 3
  • 9
0

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

Regretful
  • 336
  • 2
  • 10