CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Net::SSH::Perl multiplexing multiple sessions

 



Yuma
New User

Feb 20, 2010, 1:57 PM

Post #1 of 6 (1756 views)
Net::SSH::Perl multiplexing multiple sessions Can't Post

I want to automate an annoying task that involves logging on to multiple machines and running various coordinated commands.

I did a little searching and Net::SSH::Perl seems like it can help, but the ->cmd() function seems to start a new session each time. Since I have to coordinate commands between machines I can't just use ->cmd() to run multiple commands on a single machine at once, I need to run a command on machine A, then B, then A again, then C, and so on.

There's a ->shell() function but when I call it I see the remote shell in my terminal, which is not what I want. According to the docs ->shell() routes the remote STD* to mine. Can I somehow redirect that?

Does anyone know how I can get around this or if there's another way to do what I want?


FishMonger
Veteran / Moderator

Feb 20, 2010, 3:52 PM

Post #2 of 6 (1752 views)
Re: [Yuma] Net::SSH::Perl multiplexing multiple sessions [In reply to] Can't Post

Which protocol are you connecting with, ssh-1 or ssh-2?

SSH-2 fully supports running more than one command over the same connection. So if you connect with ssh2, you can run multiple $ssh->cmd() commands within the same session.


Yuma
New User

Feb 20, 2010, 10:06 PM

Post #3 of 6 (1746 views)
Re: [FishMonger] Net::SSH::Perl multiplexing multiple sessions [In reply to] Can't Post

I'm using SSH2 and apparently it doesn't work like that or I'm doing something wrong.


Code
my $cl = Net::SSH::Perl->new("asti", protocol=>2); 
$cl->login("yuma", "********");
$cl->cmd("cd bin");
my ($out,$err,$rc)=$cl->cmd("pwd");
print "$out";



Code
$ ./Run.pl 
/home/yuma


When I enable debug output I see that it opens a new channel and session for each cmd.

I'm going to try $ssh->open2() instead, maybe I'll have more luck.


salva
Novice

Feb 21, 2010, 9:59 AM

Post #4 of 6 (1734 views)
Re: [Yuma] Net::SSH::Perl multiplexing multiple sessions [In reply to] Can't Post

I can't see how starting a new session for every command can interfere with the scheduling. Could you elaborate?

Anyway, you can use Net::OpenSSH::Parallel to schedule running commands in several machines with dependencies between the jobs in the best way.


Yuma
New User

Feb 21, 2010, 12:05 PM

Post #5 of 6 (1729 views)
Re: [salva] Net::SSH::Perl multiplexing multiple sessions [In reply to] Can't Post

You're right, the new session per $ssh->cmd() isn't a big problem, I can work around it. However I ran into another one.

I have to start a process on one of the machines first and then run a few commands on the others. The problem is $ssh->cmd() doesn't return from starting that process no matter what I do. I've tried running it in the background, redirecting stdout/stderr, nohup and no matter what $ssh->cmd() doesn't return until that process terminates. If I could get around this problem I'd be happy, but it seems that $ssh->cmd() likes to block.


Yuma
New User

Feb 21, 2010, 2:46 PM

Post #6 of 6 (1719 views)
Re: [Yuma] Net::SSH::Perl multiplexing multiple sessions [In reply to] Can't Post

Nevermind, I got it to work by using nohup, &, and redirecting 0,1,2 to /dev/null.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives