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

Hi, I am tring to use the errmg() option of telnet::net but without any success. My script is tring to connect to several machines, but i don't want the script to die. I want to generate an error message when the script encounter a problem and continue executing the script. for example :
my $telnet = new Net::Telnet ( Timeout=>10,Port => $console_port ,Errm +ode=>'return'); $telnet->open("$console_server"); print "errmsg: " . $telnet->errmsg . "\n";
I want to be able to generate any message I want , and let the scipt keep running. Help will be most appreciated!! Thanks!

Replies are listed 'Best First'.
Re: Trying to capture error message using telnet::net
by lamp (Chaplain) on Sep 01, 2008 at 14:22 UTC
    If you are setting 'Errmode' parameter of 'Net::Telnet' module as 'return', whenever the script encounter a problem, objet will return an error message and same can be get by calling 'errmsg' function. Please check the below mentioned sample for more details
    use Net::Telnet; my $telnet = new Net::Telnet ( Timeout=>10, Port=>80, Errmode => 'retu +rn' ); $telnet->open("testerrmode.com"); if($telnet->errmsg ) { print "errmsg: " . $telnet->errmsg . "\n"; } else { print "success\n"; } print "moving to next statement\n";
    If you are setting the 'Errmode' parameter of 'Net::Telnet' module as 'die', use 'eval' function for catching the error string. Please check the code given below
    use Net::Telnet; eval { my $telnet = new Net::Telnet ( Timeout=>10, Port=>80, Errmode +=> 'die' ); $telnet->open("testerrmode.com"); }; if($@) { print $@; } else { print "success\n"; } print "moving to next statement\n";
      Let me explain.
      My 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"); if ($telnet->errmsg){ print "errmsg: " . $telnet->errmsg . "\n"; } else { print"success\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); }

      I tried the first option and the script stoped the moment an error occured.

      The secoond option didn't worked, when i tried to run the script i got a lot of error messages about not recognizing the $telnet.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 37.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 39.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 43.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 56.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 57.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 58.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 59.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 63.

      Global symbol "$telnet" requires explicit package name at ./connect_telnet.pl line 7

      Any suggestions?? Thanks!!

        Hi,
        Just ignore "my", "use strict" in your code.
        Regards,
        Anbarasu
Re: Trying to capture error message using telnet::net
by toolic (Bishop) on Sep 01, 2008 at 13:57 UTC
    Have you tried using eval?
    eval { $telnet->open("$console_server"); print "errmsg: " . $telnet->errmsg . "\n"; }
Re: Trying to capture an error using NET::TELNET
by dda (Friar) on Sep 02, 2008 at 08:08 UTC
    What is your OS? I just tried your code and it worked:
    dda@x700:/tmp$ perl nt.pl errmsg: problem connecting to "localhost", port 23: Connection refused dda@x700:/tmp$ cat nt.pl #!/usr/bin/perl -w use strict; use Net::Telnet; my $telnet = new Net::Telnet ( Timeout=>10, Port => 23, Errmode=>'retu +rn'); $telnet->open("localhost"); print "errmsg: " . $telnet->errmsg . "\n"; dda@x700:/tmp$

    --dda

      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); }
        eval { connect ... }; warn "The server is unreacable? $@" if $@;
        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.