Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Net::SSH::Perl and SSH protocols

by xorl (Deacon)
on Mar 22, 2008 at 20:34 UTC ( [id://675674]=perlquestion: print w/replies, xml ) Need Help??

xorl has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Net::SSH::Perl to run a few commands on a remote box.
#!/usr/bin/perl use strict; use warnings; use Net::SCP::Expect; my $remote_host = "xxx.xxx.xxx.xxx"; my $password = "xxxxxx"; my $username = "xxxxxx"; my $ssh = Net::SSH::Perl->new($remote_host, protocol=>'1', options => +[ "UserKnownHostsFile /home/apache/.ssh/known_hosts" ]) or die("could + not connect"); print "ssh set\n"; $ssh->login($username, $password); print "login done\n"; my $test_dir = "/foo/bar/"; my $cmd = "if [ -d $test_dir ]; then echo 1; else echo 0; fi"; print "CMD: $cmd\n"; my($stdout, $stderr, $exit) = $ssh->cmd($cmd); print "STDOUT: $stdout STDERR: $stderr EXIT: $exit\n";
The above is a sample script. If I run this on box 1 it gets all the way to the line with $ssh->cmd($cmd) and then dies with the following error:

Connection closed by remote host. at /usr/lib/perl5/site_perl/5.8.5/Net/SSH/Perl/SSH1.pm line 75

If I change the protocol in the line starting with "my $ssh" from '1' to '1,2', It takes forever but eventually it runs without any errors. If I try '2,1' it again takes forever but eventually it runs without any errors. If I try '2' same extremely long delay but it eventually works. The delay every time is at the login line

Now on box2, the unmodified script (where protocol is just '1') runs just fine and is very speedy.

What could I have screwed up on box1 to cause this weirdness and how to I make it work like box2?

Replies are listed 'Best First'.
Re: Net::SSH::Perl and SSH protocols
by Khen1950fx (Canon) on Mar 22, 2008 at 22:24 UTC
    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Net::SSH::Perl; my $remote_host = 'localhost'; my $password = 'yourpassword'; my $username = 'yourusername'; my $cmd = 'if [ -d $test_dir ]; then echo 1; else echo 0; fi'; warn "Starting SSH Services:..."; my $ssh = Net::SSH::Perl->new($remote_host, protocol => '1,2'); print "done", "\n"; warn "Starting Login:..."; $ssh->login($username, $password); print "login done", "\n"; my $test_dir = '/foo/bar'; warn "Starting command:..."; my($stdout, $stderr, $exit) = $ssh->cmd($cmd); print $stdout, "\n";
Re: Net::SSH::Perl and SSH protocols
by hipowls (Curate) on Mar 22, 2008 at 22:35 UTC

    There are a myriad of possible causes. I've had a Solaris host sending an ident back to the client and timing out on the response. A google search shows others.

    I'd guess that you get the same delay when using ssh from the command line. Try ssh -v -l user host to see what is happening. Multiple vs can be specified to get more and more debug output.

Re: Net::SSH::Perl and SSH protocols
by radiantmatrix (Parson) on Mar 24, 2008 at 14:56 UTC

    If you just need to connect and run a script, consider using the command-line SSH via the Expect module. I've found that quite helpful. Something like:

    use strict; use warnings; use Expect; my $login = "xxx"; my $pass = "xxx2"; my $test_dir = "/foo/bar/"; my $cmd = "if [ -d $test_dir ]; then echo DIR_EXIST:1; else echo DIR_E +XIST:0; fi"; my $timeout = 30; my $ex = Expect->new('ssh','-t',"$login\@hostname",$cmd); # the -t fo +rces TTY allocation, helpful for Expect! $ex->expect( $timeout, [ qr/password:\s*$/i => sub { $ex->send($pass,"\n"); return 1; } ] +, [ timeout => sub { warn "Timeout waiting for password prompt"; ret +urn 1; } ], ); $ex->expect ( $timeout, [ qr/DIR_EXIST:\d/ => sub { print "Result: ",$ex->match(); } ], );

    I've used this approach to do remote password-resets, user deletes, software installation, &c. It's a bit more code, but you get a lot more control.

    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: Net::SSH::Perl and SSH protocols
by zentara (Archbishop) on Mar 23, 2008 at 14:13 UTC
    It takes forever but eventually it runs without any errors.

    This is a common problem, usually caused by the XS based Math modules (which are fast) not being installed, and then it results in the math intensive work done by plain Perl routines. See Net::SSH::Perl and f-Secure SSH. Also read the README file for Net::SSH::Perl, which states you need Math::Pari or Math::GMP for best performance. The other thing to try is to move up to the Net::SSH2 module ( which avoids the Math module dependencies). See A little demo for Net::SSH2


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Math::Pari and Math::GMP are both installed. When we originally wrote the script, the remote box was old and could only handle protocol 1. Obviously the box has been upgraded so maybe we can switch to Net::SSH2. I'd just like to figure out what is different between box 1 and box 2. Both are supposed to be clones but obviously something is different.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://675674]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-25 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found