Data pipe lost when using ssh in shell script

Hi,

I want to do SSH on many different machines and then run some commands on them. A binary application randomly generates IP addresses and my script will take care of doing SSH.

$ ./IPGen.exe | ./myScript.sh

my script looks like this:

while read line; do
      result1=$(ssh $line 'LinuxCommand1') &
      result2=$(ssh $line 'LinuxCommand2') &
      result3=$(ssh $line 'LinuxCommand3') &
      wait;
      echo "$result1 - $result2 - $result3";
done

The problems are:
1- The while loop ends after first round!!!
2- All variables (result1, result2, result3) are empty.

I tried to search the forum before posting but I really didn't know what should I search for?!

If the ./IPGen.exe is generating multiple ip addresses, then you need to use it like

 
./IPGen.exe | while read ip; do ./myScript.sh $ip; done

In the shell script,

 
echo $1 | while read line; do
 result1=$(ssh $line 'LinuxCommand1') &
      result2=$(ssh $line 'LinuxCommand2') &
      result3=$(ssh $line 'LinuxCommand3') &
      wait;
      echo "$result1 - $result2 - $result3";
done

1 Like

Thanks itkamaraj

Also, I found another solution.
Usin "-n" with SSH will redirect output to /dev/null and SIGTTIN will not break the STDIN.