Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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"; }

Replies are listed 'Best First'.
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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://597103]
Approved by Corion
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-06-18 22:02 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.