John
John

Reputation: 21

Bash While loop is not looping

Im doing a backup script for Mikrotik devices using bash. I want to use a while loop that will read from a file.

The Contents of the file to read from will be like:

1.1.1.1 router1
2.2.2.2 router2

Here is my sample script:

while read -r ip name
do

sshpass -p "pass" ssh -o StrictHostKeyChecking=no export\@$ip "/export;/quit" 2> errors.log >> $name.export

done < iplistandnamefile

The problem is that the script will work for only the 1st IP address on the iplistandnamefile file but will not loop to the 2nd IP.

Any suggestions ?

Upvotes: 1

Views: 2609

Answers (1)

Cwissy
Cwissy

Reputation: 2176

ssh is eating the rest of the file as it defaults to reading stdin

add -n to the ssh command.

sshpass -p "pass" ssh -no StrictHostKeyChecking=no user@host command_to_run

Upvotes: 6

Related Questions