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

I have recently been made aware of Net::Telnet and ::Cisco modules which are pretty handy for telneting to a device.

My problem lies in that im trying to use Perl (for automation) to telnet to a line on a 2511 Access Server, a line that contains a directly connected device.

For instance:

####BEGIN CODE
#!/usr/bin/perl no strict; #### Define Library Locations ### #use lib '/root/Net-Telnet-3.03/blib/lib/Net/';use lib '/root/Net-Teln +et-Cisco-1.10/blib/lib/'; #### Define Modules to use #### use Net::Telnet::Cisco; #### Create Telnet Connection to Device#### my $telnet = Net::Telnet::Cisco->new(Host=>'1.2.3.4 2005', Timeout=>2, Errmode=>'die'); $telnet->waitfor('/Username:/') or die('FAILED'); $telnet->print('xxxx'); $telnet->waitfor('/Password:/') or die('FAILED'); $telnet->print('xxxx'); #$telnet->send_wakeup('connect'); $telnet->waitfor('/PROMPT>/'); @output = $telnet->cmd('show ver'); print @output;
####END CODE

Someone mentioned they thought I needed a colon between the IP and LINE of the device (e.g. 1.2.3.4:2005 instead of 1.2.3.4 2005). I use a space whenever I telnet via a shell prompt and it works.

The odd thing is that when I use the script above and issue "cmd('show ver')" the output is a 'show ver' of the 2511 and not the device connected. Its like im connected to the 2511 and not the device.

I've used both ::telnet and ::cisco but both produce the same result. It's as though perl's interpretation of telnet is different than that of which i type from a shell prompt.

Am i really missing something? I ask because you guys are the masters, im the schoolboy.

Any help is greatly appreciated,

Edit by tye: Add CODE tags

Replies are listed 'Best First'.
Re: Telneting to a 2511 Access Server Device on line 2005
by jasonk (Parson) on Mar 29, 2005 at 20:50 UTC

    Net::Telnet is not the command line, you can't specify the port with a space just because it works from the shell...

    my $telnet = Net::Telnet::Cisco->new( Host => '1.2.3.4', Port => 2005, Timeout => 2, Errmode => 'die', );

    Which is, of course, how the documentation would have told you to do it.


    We're not surrounded, we're in a target-rich environment!
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Telneting to a 2511 Access Server Device on line 2005
by Fletch (Bishop) on Mar 29, 2005 at 20:49 UTC

    You want to add Port => 2005 after your Host option to specify a port. See the docs for Net::Telnet, specifically the documentation for the new method.

      Ok so I have been able to get this to work* - somewhat.

      * - Not literal because its not how i envisioned it.

      Here is what I got, I created an input.log to well capture the input in a log so that I can see in better detail as to what is going on. It turns out the log file produces the output I was looking for the perl script to output.

      Here is what the current code looks like:
      #!/usr/bin/perl no strict; #### Define Library Locations #### #use lib '/root/Net-Telnet-3.03/blib/lib/Net/'; use lib '/root/Net-Telnet-Cisco-1.10/blib/lib/'; #### Define Modules to use #### #use Net::Telnet; use Net::Telnet::Cisco; #### Create New Telnet Connection #### my $telnet = Net::Telnet::Cisco->new(Host=>'1.2.3.4', Port=>2005, Inpu +t_log => "input.log", Timeout=>5, Errmode=>'die'); $telnet->waitfor('/Username:/') or die('FAILED'); $telnet->print('XXX'); $telnet->waitfor('/Password:/') or die('FAILED'); $telnet->print('XXX'); $telnet->cmd("show ver");

      Here is the output of input.log:

      User Access Verification

      Username: XXX
      Password:


      ^MDIAG> show ver
      ^M BLAH BLAH BLAH VERSION NUMBER BLAH
      ^MDIAG>

      Now my question is deeper, why is this not outputing via the print @output; statement, it seems like @output isnt even being pushed the output of "..cmd(show ver)"?

      My second question is why do i continue to get a command-timed out line ## - in reference to the cmd(show ver); line, even though the output.log shows it executed correctly? Is there a way to kill the cmd() before my 2 second timeout?

      Also does the ^M have anything to do with either question?

      Thanks for the help,