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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

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
William Pursell
  • 204,365
  • 48
  • 270
  • 300