LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-28-2005, 08:34 AM   #1
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Rep: Reputation: 40
Perl: Wait for ssh connection to close


I'm trying to get my perl script to execute a command (using ssh) on a remote computer, and keep the connection open until the job is finished.

To illustrate, say I execute the following code:

Code:
 foreach $machine (@machines) { 
	      system("xterm -e ssh $machine 'emacs' &");     
              }
My intention is that I execute emacs on the remote machines, but not closing the connection until I exit emacs. I want one xterm pr machine executing the program, and the xterm should exit when the remote programs exit.

As of now, emacs is executed, but the connection is closed as soon as the command is issued on the remote program. Is want the connection to stay open until the actual application closes.

How can this be accomplised?
 
Old 10-28-2005, 09:27 AM   #2
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Re: Perl: Wait for ssh connection to close

The ampersand (bold below) puts the job in the background:
Code:
system("xterm -e ssh $machine 'emacs' &");
And that's why system returns right away. You should leave it out. It's much like what you get when typing the command yourself in a shell. Compare these two:
Code:
xterm &
xterm
The first runs the terminal in the background, and you get the prompt back right away. The second waits till the xterm completes.

Maybe you want to elaborate on what you intend to happen.

Last edited by Quigi; 10-28-2005 at 09:28 AM.
 
Old 10-28-2005, 10:19 AM   #3
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
I added the "&" on purpose so that the xterm would run in the background. My fist post was not very precise, so let my try again.

My aim is to automate this:
1) log onto the remote machines (using ssh) in xterms, one xterm pr machine/connection
2) run a program (for example emacs)
3) when the program exits, close the ssh connection and exit the script

In essence: How do I open, say, 3 xterms at once, each having logged onto a different machine, and the xterms run a remote program (i.e. emacs) and are not closed until the program has finished?

Did this make it more clear?
 
Old 10-28-2005, 01:03 PM   #4
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Still more questions: Why do you use xterm? Does ssh require a password? Do you tunnel X11, or should emacs run in the xterm, like "emacs -nw"?
Do you need the perl script to wait till all emacses and their xterms exit?

One answer: ssh -t
 
Old 10-28-2005, 01:26 PM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
The following will open all windows (and exit).
Code:
my @machines = qw(a b c);
foreach my $machine (@machines) {
  system "xterm -e ssh -t -x $machine emacs &";
}
Then you can talk to ssh in each window if necessary (confirm, password) after which emacs runs. When you type C-x C-c, exacs exits, then the remote shell, the ssh connection, and finally xterm.

I'm aware that that violates your aim
Quote:
3) when the program exits, close the ssh connection and exit the script
I think it's possible to achieve, if it's important to you.
 
Old 10-29-2005, 04:44 AM   #6
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
I'm using ssh-keys to authenticate my machine to the remote machines. These machines doen't have X installed, as they as just "slaves" which are to run a distributed program. I use xterm so that I can have one ssh connection pr xterm window, and communicate with these machines through the terminals. Is there another, better way of doing this?

The perl script need not wait till the xterms exit, as long as these xterms maintains the ssh-connection. I thought the xterms would exit automatically
when the script ended, but I guess I was wrong. So never mind that last aim.


I'll try the "-t" option and see if it help. Thanks for the help so far!
 
Old 10-29-2005, 08:03 AM   #7
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
Why not fork for each machine and then waitpid in the main script after your loop.
 
Old 10-29-2005, 08:36 AM   #8
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
I thought about it, but as this script is my very first perl-script I don't know how to do it + I figured I'd try without forking.
Could you provide me a link to some example code for forking in perl?
 
Old 10-29-2005, 09:03 AM   #9
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
You should just need to use:
Code:
foreach $machine (@machines){
  my $pid=fork(); # Perform the fork
  next if($pid); # Next machine if we are the parent
  system("xterm -e ssh $machine 'emacs'");
  exit; # Exit the child
}
# Wait for child processes to complete
use POSIX ":sys_wait_h";
do {
 $kid = waitpid(-1,&WNOHANG);
} until $kid == -1;
There are lots of examples and documentation out there:
http://search.cpan.org/~jhi/perl-5.8.0/pod/perlfork.pod
 
Old 10-29-2005, 09:19 AM   #10
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Original Poster
Rep: Reputation: 40
Okay, thanks! I'll check it out!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what happens if I close ssh terminal ? juanb Linux - General 1 09-17-2004 05:15 PM
Close ssh for all except one IP krilen Linux - Security 13 03-26-2004 05:07 PM
How do you close port 22 SSH? Linux~Powered Linux - Security 7 03-16-2004 11:13 PM
Game server shutting down when I close my SSH connection jader9920 Linux - Software 7 01-05-2004 07:38 PM
How to tell proxy to close a connection ? gt73 Programming 0 12-01-2003 05:03 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration