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

I'm having trouble capturing errors from Net::Telnet as I would expect to. In the following code snippet, I would have thought that I would have gotten a warning 'ugh' from the cmd method, but I had no such luck. Next I would have thought for sure the error message would have been returned by the errmsg method, again no joy. The only way I see the error is if I examine what the cmd method returned to @p.
#!/usr/bin/perl -w use strict; use Net::Telnet; my $user = 'foo'; my $passwd = 'bar'; my $t= new Net::Telnet(Host => '****.****.com', Errmode => "return"); $t->login($user, $passwd); print $t->errmsg, " login\n"; my @p=$t->cmd("dwho") or warn "ugh\n"; print $t->errmsg, " who\n"; print "\@p contains:\n", join("\n", @p),"End\n"; __END__ login who @p contains: sh: dwho: not found. End
Regardless of how I mangle the cmd, I still cannot get the errmsg method to return any error. In my actual code I,m not really concerned that the command will fail as in my example but it's quite possible that a file could be missing.

Any Ideas???

TIA

Sweetblood

Replies are listed 'Best First'.
Re: Net::Telnet error trapping
by pg (Canon) on Nov 12, 2004 at 21:10 UTC

    errmsg() returns the error of the Net::Telnet object itself, not that of the command/script you executed. For example:

    use Net::Telnet; use strict; use warnings; my $t = new Net::Telnet(timeout => 10, Errmode => "return", Prompt => +'/\[AAUA1\]/'); $t->open("ofgaix1"); $t->login("foo", "bar"); print "errmsg: " . $t->errmsg . "\n";

    Host ofgaix1 does not exsit, and when I tested it, it printed this:

    errmsg: login failed: filehandle isn't open

    The error message if they get printed on the server side, then they are returned from cmd() call in an array, together with all other print outs.

      Hi I get the same error message in some PCsne but it works ok on others (working on same UNIX env). Any ideas? Thanks, Gil
Re: Net::Telnet error trapping
by nuttervm (Initiate) on Oct 26, 2011 at 17:34 UTC

    I know this is an old question but I ran into the same issue and could not find the answer...

    As far as I can tell, the error basically tells you that the "open" command was not successful and you are still trying to execute functions anyway (e.g. login, cmd). Below is a code snippet that should prevent this error in the future:

    my $t = new Net::Telnet (Timeout => '30', Errmode => 'return', ); my $openResult = $t->open("$remoteHost"); if ($openResult eq 1){ my $loginResult = $t->login($username, $password); if ($loginResult eq 1){ @lines = $t->cmd("your_command_here"); } else { $lines[0] = "Error logging into device: '" . $t->errmsg . "' \n" +; } $t->close; } else { $lines[0] = "Error connecting to device: '" . $t->errmsg . "' \n"; }