LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Data pipe lost when using ssh in shell script (https://www.linuxquestions.org/questions/linux-newbie-8/data-pipe-lost-when-using-ssh-in-shell-script-926762/)

siavashgh 01-31-2012 02:31 PM

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.

Code:

$ ./IPGen.exe | ./myScript.sh
my script looks like this:

Code:

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?!

arizonagroovejet 01-31-2012 02:43 PM

If you run IPGen.exe what does the output look like?

arizonagroovejet 01-31-2012 02:46 PM

Also, see what line actually being set to
Code:

while read line; do
      echo $line
done


Cedrik 01-31-2012 02:54 PM

Send ssh commands to random IPs ? What is your intentions lol

arizonagroovejet 01-31-2012 03:04 PM

Quote:

Originally Posted by Cedrik (Post 4589660)
Send ssh commands to random IPs ? What is your intentions lol

Yeah I wondered about the random part. I assume when they say random they mean 'randomly selected from a pool of IP addresses belonging to machines which I can log in to using ssh key authentication'. Perhaps too large an assumption. Can't think what they mean otherwise though. I mean no one would really expect to log in to machines by just randomly generating IP addresses... would they...?

colucix 01-31-2012 03:09 PM

The problem is due to a feature of the remote command execution through ssh: the standard input coming from the pipe and feeding the while read loop, becomes the standard input of the remote commands. Once the commands are executed, ssh sends back a SIGTTIN signal to stop the input flow. The side effect is that the standard input of the loop is broken and no further iteration is performed.

To avoid this, add option -n to each ssh command in the script: it will redirect the standard input from /dev/null and will protect the actual input of the loop from the SIGTTIN signal.

Cedrik 01-31-2012 03:46 PM

Quote:

Originally Posted by arizonagroovejet (Post 4589671)
I mean no one would really expect to log in to machines by just randomly generating IP addresses... would they...?

I don't know, the internet is big. Who knows if there is not somewhere a ssh server waiting for root to login with '1234' password :p

siavashgh 01-31-2012 03:47 PM

Thanks all.


Quote:

If you run IPGen.exe what does the output look like?
One IP on each line.


Quote:

Send ssh commands to random IPs ? What is your intentions lol
Quote:

I assume when they say random they mean 'randomly selected from a pool of IP addresses belonging to machines which I can log in to using ssh key authentication'.
Exactly.


Quote:

Originally Posted by colucix (Post 4589677)
The problem is due to a feature of the remote command execution through ssh: the standard input coming from the pipe and feeding the while read loop, becomes the standard input of the remote commands. Once the commands are executed, ssh sends back a SIGTTIN signal to stop the input flow. The side effect is that the standard input of the loop is broken and no further iteration is performed.

To avoid this, add option -n to each ssh command in the script: it will redirect the standard input from /dev/null and will protect the actual input of the loop from the SIGTTIN signal.

Dear colucix thank you very much. That little "-n" did the trick. Also thanks for detailed info about SIGTTIN. I learned something new.


All times are GMT -5. The time now is 12:11 AM.