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

I am using telnet.pm to login to a machine and I want to see the output of the program to be written to the screen, so i can actually see the script logging in and determine how far the script is getting and where it is stopping, how is this possible ?? script being used below
#!/usr/bin/perl -w use strict; use Net::Telnet; my $username = "admin"; my $password = 'admin1'; my $hostname = 'server.com'; my $t ; sleep 1; $t = Net::Telnet->new( Timeout => 5, Prompt => '/%/', Host => $hostname); $t->open($hostname); $t -> waitfor('/login:/'); $t->print('admin'); $t->waitfor('/Password:/'); $t->print('admin1'); print "script is now here\n";

Replies are listed 'Best First'.
Re: writing output to screen with telnet.pm
by shenme (Priest) on Oct 08, 2003 at 22:13 UTC
    (I think I'm much too grumpy to answer this, but...)

    So you want to debug the interactions between your script and your host?   Let's see, bring up the docs for Net::Telnet on, oh, say on search.cpan.org.   Oh, gee, lookee there, it starts out

    NAME
    SYNOPSIS
    DESCRIPTION
    What To Know Before Using
    Debugging
    Hmmm, it suggests dumping out the data passed between program and host.   Gosh, you might see something...   Oooo, here's another quote
    Use dump_log() to debug when this method keeps timing-out and you don't think it should.
    And another
    dump_log - log all I/O in dump format 
        $fh = $obj->dump_log;
        $fh = $obj->dump_log($fh);
        $fh = $obj->dump_log($filename);
      This method starts or stops dump format logging of all
      the object's input and output. The dump format shows
      the blocks read and written in a hexadecimal and
      printable character format. This method is useful when
      debugging, however you might want to first try input_log()
      as it's more readable.
    
    Gawllee, there are nine hits on the word 'debug' in the docs.   One or more might be of help to you.
      thanks ver much shenme ...go raibh mile maith agat!
        Now that's _really_ reading between the lines!   How'd you know I was (part) Irish?   (Cork - O'Sionnaigh, originally)
Re: writing output to screen with telnet.pm
by LTjake (Prior) on Oct 08, 2003 at 15:50 UTC

    Hi.

    Firstly, I've never used Net::Telnet. But, after looking at the docs, this might be an option:

    When $obj->waitfor is called in list context, the "characters before the match and the matched characters are returned".

    So, maybe calling it like:

    my ( $prematch, $match ); ( $prematch, $match ) = $t->waitfor( '/login:/' ); print $prematch, $match; $t->print( 'admin' ); ( $prematch, $match ) = $t->waitfor( '/Password:/' ); print $prematch, $match; $t->print( 'admin1' );

    Will show you what's been recieved.

    Alternatively, you might just spatter in some debug-like statements.

    print "Sending username...\n"; $t->waitfor( '/login:/' ); $t->print('admin'); print "Sending password...\n"; $t->waitfor( '/Password:/' ); $t->print( 'admin1' );

    Lastly, consider using the login method and checking its return value to see if it succeeded or not, or even using input_log and dump_log to debug.

    Oh, and i noticed that your password match is using an uppercase "P", i think, generally, it should be lowercase. *shrug*

    Good luck.

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

Re: writing output to screen with telnet.pm
by vek (Prior) on Oct 08, 2003 at 16:17 UTC

    You could always use the Perl debugger and step through each line.

    perl -d yourprogram.pl
    -- vek --