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

Does anyone know how I would go about retrieving error information using the Net::Telnet module? Code example would be a great help?
  • Comment on Net::Telnet-Retrieving error information

Replies are listed 'Best First'.
Re: Net::Telnet-Retrieving error information
by bart (Canon) on Feb 04, 2003 at 23:45 UTC

    As I gathered from your comment in the CB, you specifically want to know if you failed to login. Well, the docs from Net::Telnet say:

    login:
    This method performs a standard login by waiting for a login prompt [...]. If any of those prompts sent by the remote side don't match what's expected, this method will time-out, unless timeout is turned off.

    On success, 1 is returned. On time out, eof, or other failures, the error mode action is performed. See errmode().

    errmode:
    This method gets or sets the action used when errors are encountered using the object. [...] The second calling sequence sets it to $mode and returns the previous mode. Valid values for $mode are "die" (the default), "return", a coderef, or an arrayref.

    When mode is "die" and an error is encountered using the object, then an error message is printed to standard error and the program dies.

    When mode is "return" then the method generating the error places an error message in the object and returns an undefined value in a scalar context and an empty list in list context. The error message may be obtained using errmsg().

    When mode is a coderef, then when an error is encountered coderef is called with the error message as its first argument. Using this mode you may have your own subroutine handle errors. If coderef itself returns then the method generating the error returns undefined or an empty list depending on context.

    [Text on arrayref snipped]

    errmsg:
    most recent error message
    $msg = $obj->errmsg;
    The first calling sequence returns the error message associated with the object.
    Perhaps it's just me, but that seems pretty clear? Don't set errmode, and the script will die. Set it to "return", and in case of an error, login() will return undef. Check $telnet->errmsg to see what failed.

    p.s.: You should check out the EXAMPLES section of these docs. The examples given are pretty neat.

    In addition, the chapter on Net::Telnet (and Net::FTP) from Lincoln Stein's book "Network Programming with Perl" is online. You can get the sample code here.

Re: Net::Telnet-Retrieving error information
by steves (Curate) on Feb 04, 2003 at 17:19 UTC

    Can you clarify? Do you want to use Net::Telnet to go to another machine and examine files and/or run commands? Or do you want code examples of how to check for Net::Telnet errors?