0
#!/bin/bash
input="/home/ptx/script/input.file"
 while IFS= read -r line;
  do
  sshpass -f password_file ssh -tt admin@$line 'echo password | sudo -S -s ls -ltra  &> collect.txt'
  done < "$input"
password_file content: password

input.file contents:

mescmb49

mescmb46

mescmb44

mescmb33

Question

need to collect remote servers home directory contents to remote server file. want to automate this task. all the servers are listed in input.file issue is, once script works only for the first server that is "mescmb42"

Mangala
  • 1
  • 1

1 Answers1

2

sshpass is reading from its stdin, which it inherited from the while loop. So sshpass is consuming all of the data that you intended to go to read. The easiest fix is probably to close sshpass's input stream (or redirect it:

 while IFS= read -r line; do
     </dev/null sshpass ...
 done < "$input"

 while IFS= read -r line; do
     <&- sshpass ...
 done < "$input"

Another option is to have read read from an alternate fd:

while IFS= read -r line <&3; do
   sshpass ...
done 3< "$input"
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • HI William Pursell, first method works for me but I don't really get the concept behind it, if you can please provide some knowledge article to read and understand. – Mangala Jan 28 '22 at 11:50
  • I'm not sure about an article, but there are several questions on SO that address this. The concept is simple; when you do `while read var; do cmd; done`, `read` and `cmd` are both reading from the same stream. If `cmd` reads any data from the stream , `read` will never see it. – William Pursell Jan 28 '22 at 11:59