1

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 ?

2
  • The while loop works fine, am not sure of the syntax of the sshpass command, the proper syntax is sshpass -p "pass" ssh -o StrictHostKeyChecking=no user@host command_to_run. What are you trying to achieve with export? Commented May 6, 2016 at 7:43
  • the sshpass is working since ive generated an export file for the 1st IP in the list. yah export is the command i want to run. I want to use the while loop so I can use multiple variable when reading the file. Currently I have a working for loop script that reads from a file containing only IP addresses. Commented May 6, 2016 at 8:12

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

It works.. but i cant fully grasp the function of the -n flag.
-n redirects /dev/null to stdin whereas without it, the whole contents of your file become stdin to ssh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.