http://www.perlmonks.org?node_id=597103

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

Im using Net::SSH::Perl and Net::Telnet to login to a list of routers. I dont know if the routers are using SSH or Telnet, so I need to try both. The problem is the script is dying within the SSH module and not going to the next line in my script. Can anyone tell me how I can get around this? Heres the portion of my script Im refering to:
$rtrs = "$workdir/bin/wanrtrs"; $outfile = "$workdir/$date-allnetrtrscn.raw"; open ($INR,$rtrs) || die "Can't open the file: $!\n"; open ($OUT,">>$outfile") || die "Can't open the file: $!\n"; while (<$INR>){ chomp(); push(@rtrs, $_); } foreach $host (@rtrs){ $remote = Net::SSH::Perl->new($host); $remote->login($user, $pass); if ( ! $remote ){ print "ERROR: Problem Connecting to $host\n"; $remote = Net::Telnet->new($host); $remote->login($user, $pass); } ($stdout) = $remote->cmd("sh ip vrf int"); print $OUT "$host:sh ip vrf int\n$stdout\n"; }

Replies are listed 'Best First'.
Re: How do I stop SSH Module from dieing?
by Fletch (Bishop) on Jan 29, 2007 at 16:09 UTC

    Wrap the code which is dieing in an eval (the BLOCK form, not the string); Check $@ afterwards and see if the connect was successful.

Re: How do I stop SSH Module from dieing?
by educated_foo (Vicar) on Jan 29, 2007 at 16:15 UTC
    You might also want to visit rt.cpan.org to report this completely undocumented behavior as a bug.
Re: How do I stop SSH Module from dieing?
by RobPayne (Chaplain) on Jan 29, 2007 at 16:40 UTC
    You haven't stated whether the code is dying on all routers, or only those that are only available via telnet. If it is the latter, you might try the telnet connection first as a workaround until you get resolution on the other issue. As another poster has suggested, it is wise to also wrap the connection code in an eval so you can "catch" the error which is occuring and handle it appropriately.
      The error that is occuring is, cant connect to port 22 when it hits a telnet router, which is fine. The problem is it dies at that point instead of going to the next line, which is my if statement to use the telnet login. If I reverse it and do Telnet first, the same thing occurs when it hits the first SSH router. Ill look into the eval suggestion. Thank you.