LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2012, 03:31 PM   #1
siavashgh
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Rep: Reputation: 0
Question 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?!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-31-2012, 03:43 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
If you run IPGen.exe what does the output look like?
 
Old 01-31-2012, 03:46 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Also, see what line actually being set to
Code:
while read line; do
      echo $line
done
 
Old 01-31-2012, 03:54 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Send ssh commands to random IPs ? What is your intentions lol
 
Old 01-31-2012, 04:04 PM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Quote:
Originally Posted by Cedrik View Post
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...?
 
Old 01-31-2012, 04:09 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
2 members found this post helpful.
Old 01-31-2012, 04:46 PM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Quote:
Originally Posted by arizonagroovejet View Post
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

Last edited by Cedrik; 01-31-2012 at 04:47 PM.
 
1 members found this post helpful.
Old 01-31-2012, 04:47 PM   #8
siavashgh
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
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 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script - pipe command casperdaghost Linux - Newbie 2 08-15-2009 11:28 AM
LXer: pv (Pipe Viewer) - Shell pipeline element to meter data passing through LXer Syndicated Linux News 0 12-28-2008 10:20 PM
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 06:14 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM


All times are GMT -5. The time now is 01:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration