Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: SSH2 - Asynchronous Opens & Synchronous Commands

by 5haun (Sexton)
on Apr 04, 2014 at 11:40 UTC ( #1081111=note: print w/ replies, xml ) Need Help??


in reply to Re: SSH2 - Asynchronous Opens & Synchronous Commands
in thread SSH2 - Asynchronous Opens & Synchronous Commands

Thanks for the suggestion Salva, but I don't believe your modules will suffice in this case. Net::OpenSSH lacks sufficient shell support for this use case, and Net::OpenSSH::Parallel is does not fit the use case (only the connect() calls are to be done in parallel - I can't queue up a bunch of commands and run).

I know you strongly believe the idea of having to interact with the shell is a bad one, but that is the requirement. The framework we've built is used to verify command sets executed in the remote shell environment of hosts and devices. I can't just connect, run a command, and close. I need to connect, send a remote command (which could be a shell builtin like cd), check the status, run a command, inspect the output, and so on. It is an automated interaction. Combining commands with '&' isn't an option - they need to be executed individually and evaluated. Basically, I need the full functionality of Net::SSH::Expect, which we've also implemented, but this task to implement Control::CLI/Net::SSH2 is to replace the use of Net::SSH::Expect, as Net::SSH::Expect is not thread-safe.

If Net::OpenSSH had shell support like Net::SSH2, I'd try it again in a heartbeat.

Net::SSH2 is also thread-safe. At least it is when the entire test for a device is contained within a thread. The questing being asked is what is the best method of making only the connections in parallel (for performance reasons) and still allow the remainder of the test to be performed serially?


Comment on Re^2: SSH2 - Asynchronous Opens & Synchronous Commands
Re^3: SSH2 - Asynchronous Opens & Synchronous Commands
by salva (Abbot) on Apr 04, 2014 at 12:44 UTC
    You can run shells with Net::OpenSSH quite easily and even use Expect to interact with them:
    my %ssh; for my $host (@hosts) { $ssh{$host} = Net::OpenSSH->new($host, async => 1, ...); } for my $host (@hosts) { my $ssh = $ssh{$host}; my ($pty, $pid) = $ssh->open2pty; # whith out command launchs a shel +l my $expect = Expect->init($pty); $expect->... }

      This is an interesting proposal. I'll have to do some tests with this. Thanks. I'll report back with my findings.

      Update: I'm still working on this test case. I've got something implemented using Telnet, but I need to do some more testing. I'll post code tomorrow morning.

      In the meantime, how would you handle waiting for all the async open requests to complete? I this what wait_for_master is for? My current solution loops checking for $ssh->wait_for_master(1) returning true.

      Thanks.

        You have to call $ssh->wait_for_master(1) repeatedly until it succeeds:
        my %connecting = map { $_ => 1 } @hosts; while (1) { for my $host (keys %connecting) { my $ssh = $ssh{$host}; if ($ssh->wait_for_master(1)) { delete $connecting{$host}; } elsif ($ssh->error) { # handle connection error delete $connecting{$host}; } } last unless %connecting; select (undef, undef, undef, 0.05); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081111]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2014-12-28 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls