0

bash script to read a file and process commands remotely. It currently only processes the first line (server1)

Need to remotely process 3 commands on server1 , then server2 ......

#!/bin/bash

while read line; do
    sshpass -f password ssh -o StrictHostKeyChecking=no user@$line zgrep "^A30=" /var/tmp/logs1/messages.* |  >> Output.txt
    sshpass -f password ssh -o StrictHostKeyChecking=no user@$line zgrep "^A30=" /var/tmp/logs2/messages.* |  >> Output.txt
    sshpass -f password ssh -o StrictHostKeyChecking=no user@$line zgrep "^A30=" /var/tmp/logs3/messages.* |  >> Output.txt
done < file1

file1:

server1
server2
server3
2
  • What is ` | >> Output.txt` supposed to do? Commented Jan 23, 2019 at 15:44
  • That construction may work in some shells, but you probably just want to do done < file 1 >> Output.txt Commented Jan 23, 2019 at 15:48

1 Answer 1

4

sshpass is reading from the same file descriptor as the while loop, and exhausting that input before read is able to read it. Best thing is to close its stdin explicitly sshpass <&- ... or redirect from /dev/null sshpass < /dev/null.

Another option is to let sshpass inherit stdin from the script and read from a different file descriptor:

while read line <&3; do 
   ...
done 3< file1
Sign up to request clarification or add additional context in comments.

1 Comment

Thxs your suggestions worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.