Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I stop SSH Module from dieing?

by Smith (Initiate)
on Jan 29, 2007 at 15:51 UTC ( #597103=perlquestion: print w/ replies, xml ) Need Help??
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"; }

Comment on How do I stop SSH Module from dieing?
Download Code
Re: How do I stop SSH Module from dieing?
by Fletch (Chancellor) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-07-13 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls