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

Hello all, I am attempting to connect to a network device using Net::Telnet, the issue is that the device needs to have a new line sent after connection to load the login window. Currently (and not working) my code is as follows:
#! /usr/bin/perl use strict; use warnings; use Net::Telnet; use Data::Dumper; my $t = new Net::Telnet (Timeout => 10, Prompt => '/>/'); $t->open("192.168.3.35"); $t->print(""); $t->login('user','****'); $t->print('3'); $t->print('1'); my @lines = $t->print('2'); print Dumper @lines; exit(0);
I have also tried with:
#! /usr/bin/perl use strict; use warnings; use Net::Telnet; use Data::Dumper; my $t = new Net::Telnet (Timeout => 10, Prompt => '/>/'); $t->open("192.168.3.35"); $t->login('user','****'); $t->print('3'); $t->print('1'); my @lines = $t->print('2'); print Dumper @lines; exit(0);
I have also attempted the $t->waitfor('/string/') option but all return the following error:
timed-out waiting for login prompt at rici_test.pl line 13
So, in essence, I am trying to send a carriage return before attempting to log in and I am having no luck. This is part of a log gathering utility, connecting to a Rad RICi-T3. My other option is to go through the web gui but then the interface is all in Javascript so, WWW::Mechanize does not work. Any help would be greatly appreciated. Thanks

Replies are listed 'Best First'.
Re: Net::Telnet issue
by Eliya (Vicar) on Feb 24, 2011 at 18:51 UTC
    So, in essence, I am trying to send a carriage return...

    This should normally happen with an empty print, as the default output record separator is \n, which is then internally translated to CR LF.

    Are you sure you've set the correct prompt?  Usually, time-outs happen as a result of waiting for something the other side doesn't send.  In case you can't the get the standard login to work, you could try crafting your own custom one, using print/put and waitfor.

    Also see Debugging to figure out what the other sends.

      I understand that, the issue is with the Rad RICi-T3 I am attempting to telnet into. Before you can even log into the device, you need to send a char because it loads a blank screen initially. I have attempted to use "", " ", "\033" all to no avail. Once the char is sent, then you get the username and password prompts. My issue is that with Net::Telnet, I am not able to send the char before attempting to authenticate through the device. Once you log into the device, the prompt is '>'. I have used the waitfor and print calls but to no avail.

        Can you log in with a normal telnet client from the command line?  If so, what exactly do you have to type?

        Net::Telnet definitely does send \r\n when you say $t->print():

        Here's a simple "telnet server" that simply echoes back in hex what you send it

        #!/usr/bin/perl use IO::Socket; my $sock = IO::Socket::INET->new( LocalAddr => "localhost:9999", ReuseAddr => 1, Listen => SOMAXCONN, ) or die "couldn't create socket: $!\n"; while (1) { my $client = $sock->accept(); while (my $line = <$client>) { print $client join(" ",unpack("(H2)*", $line)), "\n"; } close $client; }

        When you connect to it with a standard telnet client, and press <Enter> once, you get

        $ telnet localhost 9999 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 0d 0a

        When you connect with the following Net::Telnet code

        #!/usr/bin/perl -w use strict; use Net::Telnet; my $t = Net::Telnet->new(Port => 9999); $t->open("localhost"); $t->print; my $r = $t->getline; print $r;

        you get exactly the same:

        $ ./890020_c.pl 0d 0a
Re: Net::Telnet issue
by roboticus (Chancellor) on Feb 24, 2011 at 18:57 UTC

    dimishome:

    Did you try putting $t->print("\n"); before the login call?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      yes, but it still doesn't allow the login on the following line.
Re: Net::Telnet issue
by frodonl (Novice) on Feb 24, 2011 at 19:03 UTC

    First of all, I have no experience with using Net::Telnet, so I am not sure if what you want can be done in it.

    You might want to have a look at Expect instead - it is all about handling interactive access to remote servers. (This does assume you are not using ActivePerl on Windows, under which - afaik - it doesn't work.)