Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Can't get data from cisco router/switch when telnetting in via perl script.

by regexes (Hermit)
on Aug 21, 2007 at 09:01 UTC ( [id://634029]=note: print w/replies, xml ) Need Help??


in reply to Can't get data from cisco router/switch when telnetting in via perl script.

Hello,
Take a look at the cmd method. Try this and see if it works for you...
@data = $tnet->cmd("show time");
regexes


-------------------------
Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan "press on" has solved and always will solve the problems of the human race.
-- Calvin Coolidge, 30th President of the USA.
  • Comment on Re: Can't get data from cisco router/switch when telnetting in via perl script.
  • Download Code

Replies are listed 'Best First'.
Re^2: Can't get data from cisco router/switch when telnetting in via perl script.
by Newb to Perl (Initiate) on Aug 21, 2007 at 09:35 UTC
    I always receive a timeout error (command timed-out at TestTelnet.pl line 53) when I try the 'cmd' command. I've tested the telnet login and know it works. Assigning and running the command on the switch won't save data to my array.
      The Net::Telnet module must be configured to recognise the input prompt, otherwise it will not know when the last command completed.

      If you are receiving timeouts it is most likely the default prompt matching string is not matching your router's prompt.

      Please read the documentation for Net::Telnet. To make your life easier, specifically look deeper at the likes of:

      $t = new Net::Telnet (Timeout => 10, Prompt => '/bash\$ $/');

      Note that you'll have to change the Prompt regexp to something that matches your router's prompt.. if you provide examples of what your router's prompts look like someone can suggest a suitable regexp.

        I just wanted to add: the default prompt (in the documentation for Net::Telnet) is /[\$%#>] $/ (which probably doesn't match a typical IOS prompt).

        Note that there also exists a Net::Telnet::Cisco module if you don't want to go to the (relatively simple) trouble of configuring your own prompt regexp.

        monarch's comment is spot on. While I use the Net::Telnet::Cisco module I made a quick script just using Net::Telnet. Below is a script that logs into a 3750 switch and performs show clock. the switch's prompt is 3750G>. Without the prompt, the script simply times out.
        use Net::Telnet; # use the telnet module my $host = '3750g'; my $t = new Net::Telnet (Timeout => 10, Prompt => '/3750G\>/', ); $t->open($host); $t->login($user,$pass); @data = $t->cmd("show clock"); print "data is @data"; $t->close;
        Here is the output of the script: data is 19:21:11.575 UTC Tue Aug 21 2007 I hope you can add the Net::Telnet::Cisco module in the future. It greatly simplifies life if you have a bunch of these to keep track of.
      Have you tried running the code with the debugger? (i.e. the -d command line switch?)

      Does the array even contain data?

      e.g
      DB<5> x @data
      What do you receive as output? A list of elements or empty array?

      regexes

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://634029]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found