in reply to Re: Trying to capture an error using NET::TELNET
in thread Trying to capture error message using telnet::net

My OS is Linux.
Yes, the script basically works. but it stops when an error occur.
Let me explain.
The script go on a text file that contain IP addresses. I have a subroutine that duty is to connect to a remote machine.

I use a while loop that each time call the subroutine to connect to a remote machine. this function suppose to go on all the file with the IP addresses. but when an error occurs during the connection ( No ping, console port is occupied, etc) the script stops.


I want the script to generate an error lets say "The server is unreacable" each time it encounter an error, and continue to the outher IP's.

sub connect { my ($console_server,$console_port) = @_; my $telnet = new Net::Telnet ( Timeout=>10,Port => $console_po +rt ,Errmode=>'return'); $telnet->open("$console_server"); print "errmsg: " . $telnet->errmsg . "\n"; print ("loginig in...\n"); $telnet->print('user'); $telnet->waitfor('/password: $/i'); $telnet->print('password'); $telnet->waitfor('/# ?$/i'); print ("exiting...\n"); $telnet->print('exit'); } } while (<DATA>) { my $line = $_; print "trying to connect to console server $console_server_tem +p port $console_port_temp \n"; &connect($console_server_temp,$console_port_temp); }

Replies are listed 'Best First'.
Re^3: Trying to capture an error using NET::TELNET
by Anonymous Monk on Sep 02, 2008 at 08:44 UTC
    eval { connect ... }; warn "The server is unreacable? $@" if $@;
      Hi,

      I did :

      eval { my $telnet = new Net::Telnet ( Timeout=>10,Port => $console_por +t ,Errmode=>'die'); $telnet->open("$console_server"); }; warn "The server is unreacable? $@" if $@;

      But got a lot of error like :
      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 3
      What can i do??

        Most probably, you're just not declaring $telnet in a sufficiently large scope: the code you pasted above, as of itself is fine.

        --
        If you can't understand the incipit, then please check the IPB Campaign.
        Fix it.
Re^3: Trying to capture an error using NET::TELNET
by Anonymous Monk on Sep 20, 2011 at 16:21 UTC
    Here's what I do to circumvent this problem (I know this is an old post...this allows for logging if you are using print FILE ... ): $fail = sub { print "failed" }; $telnet->open(Host => $server, Port => $port, Errmode => $fail, Timeout => 5); print "success" if ($telnet); # return works here, but I can't get return to work correctly in the sub) I'm trying to get the actual error message to differentiate between dns issues vs. bad port. Think I'll try eval. This is inside another module, so printing is not adequate.