Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Net::SSH::Perl hanging on creating object

by gepebril69 (Scribe)
on Jun 08, 2011 at 19:24 UTC ( #908766=perlquestion: print w/replies, xml ) Need Help??
gepebril69 has asked for the wisdom of the Perl Monks concerning the following question:


I have created a program to check processes on remote Linux based machines. Now and than one of the machines seems to hang. When I ping it it sends good responses. When I do the following

$ssh = Net::SSH::Perl->new($hostname);

the Perl scripts hangs....... So before the actual login attempt. How can I prevent this? Or create something that detects this state, so I could restart that machine by a remote power switch..

Replies are listed 'Best First'.
Re: Net::SSH::Perl hanging on creating object
by salva (Abbot) on Jun 08, 2011 at 20:55 UTC
    You can use Net::OpenSSH that has support for timeouts:
    my $ssh = Net::OpenSSH->new($host, timeout => 30); $ssh->error and die "Unable to connect to remote host $host: ". $ssh-> +error;
      Thanks for all answers. I love this approach. And the solution works for me. The small script don't hang forever like it did before. I will try to implement all the current build functionality in Net:OpenSSH
Re: Net::SSH::Perl hanging on creating object
by Perlbotics (Chancellor) on Jun 08, 2011 at 20:04 UTC

    Use a timeout. By skimming through the documentation of Net::SSH::Perl, I couldn't find any timeout option, but you should be able to wrap your constructor call in your own timeout implementation (see alarm). I am not familiar with this module, but I expect, that you have to find your way around safe signals (see perlipc).

    Another observation: If a host answers to a ping, it is still alive. Maybe there is something else stopping the initial SSH connection (firewalls? account locked? etc.)... Net::SSH::Perl supports a debug option that might be useful:
    $ssh = Net::SSH::Perl->new($hostname, debug => 1);

Re: Net::SSH::Perl hanging on creating object
by rgcosma (Beadle) on Jun 08, 2011 at 20:27 UTC
    Dealing with network timeouts is tricky, your code can hang while resolving the hostname, the host can be slow, blocking your SYNs, your ACKs if it's really evil etc. I usually take the lazy approach and backtick nmap, relying on its excellent detection techniques:
    perl -e '$a=`nmap -sS -P0 -p 22 --host-timeout 30s -oG - hostname.doma +in`; if($a=~/Status: (\w+).*?Ports: 22\/(\w+)/gsi) {print "Host statu +s: $1, SSH service: $2\n"} else {warn $a}'
      I think nc approach is better:
      if ( ! system ("nc -z -w 2 22 2> /dev/null")) { print "ok"."\n" }
Re: Net::SSH::Perl hanging on creating object
by Khen1950fx (Canon) on Jun 09, 2011 at 02:34 UTC
    Do you get the feeling that you maybe forgot something? It hangs because you haven't told it to do anything. Here's an example that I used to ping
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; my $host = ''; my $user = 'anonymous'; my $pass = 'anonymous'; my $cmd = exec("ping", "$host"); my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my ($stdout, $stderr, $exit) = $ssh->cmd($cmd); print $stdout, "\n";
    I really like this module, and I've to decided to contact the module's author to see if he can use a co-maintainer. It'll work for you, but only if you work for it:).
      It hangs because you haven't told it to do anything
      Not really. If you don't tell it to to anything, it will not do anything, but it will not hang either. It will just silently drop the connection when the script exits.
      $ time perl -MNet::SSH::Perl -E '$ssh=Net::SSH::Perl->new(localhost, d +ebug => 1); say "connected!"' topo: Reading configuration data /home/salva/.ssh/config topo: Reading configuration data /etc/ssh_config topo: Connecting to localhost, port 22. topo: Remote protocol version 2.0, remote software version OpenSSH_5.8 +p1 Debian-4ubuntu1 topo: Net::SSH::Perl Version 1.34, protocol version 2.0. .opo: No compat match: OpenSSH_5.8p1 Debian-4ubuntu1 topo: Connection established. connected! real 0m0.263s user 0m0.200s sys 0m0.010s

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://908766]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2017-07-24 06:47 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (348 votes). Check out past polls.