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

I've written a perl script to telnet into a Cisco 5513 switch, run a simple command, and save the data to an array I can manipulate later.
use Net::Telnet; @data = ''; $tnet = new Net::Telnet(); #Create a new telnet session $tnet->open("$ARGV[0]"); #Telnet to selected device $tnet->login($ARGV[1], $ARGV[2]);#Login to device @data = $tnet->print("show time");#Capture data from command The problem is when I print the data array: print @data;
always returns a "1" to the screen and not something like: Tue Aug 21 2007, 01:36:15, like I would expect. I've seen a net::telnet package specific to the Cisco IOS, but I'm unable to install it at this time (I dont' have permission at this time). Am I missing something or can this not be done? Any insight is appreciated.
  • Comment on Can't get data from cisco router/switch when telnetting in via perl script.
  • Download Code

Replies are listed 'Best First'.
Re: Can't get data from cisco router/switch when telnetting in via perl script.
by roboticus (Chancellor) on Aug 21, 2007 at 11:12 UTC
    Newb to Perl:

    Heh ... it looks like it's working properly then. Check out the documentation for Net::Telnet's print method:

    print - write to object $ok = $obj->print(@list); This method writes @list followed by the output_record_separator t +o the open object and returns 1 if all data was successfully written +. On time-out or other failures, the error mode action is performed. See "errmode()".
    ...roboticus
Re: Can't get data from cisco router/switch when telnetting in via perl script.
by cengineer (Pilgrim) on Aug 21, 2007 at 13:52 UTC
    You can save a log of the input and output of session by defining Dump_Log in your telnet object. You can also save a log of your input to the switch by defining Input_Log. This might help you to better understand what's going on in the telnet session.
    $tnet = Net::Telnet->new( Host => $host, Port => $port, Dump_Log => $dumplog, Input_Log => $inputlog, );
    Take a look at the 'waitfor' method in Net::Telnet to get the response after your print
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
    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.
      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.

        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
A reply falls below the community's threshold of quality. You may see it by logging in.