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

Hi Guys, I'm writing a script to configure a NetGear switch through its telnet port. I know how to login, how to issue commands, but now I has a problem. The switch has a menu first after we login it, and then by sending different key shortcuts, we can enter different sub-menus, or get in the Command Line Interface. Below is the sample of the main menu:
FSM726 Management Switch Main Menu a. System b. Status c. Set-up d. Tools e. Security f. Advanced
Below is the sample of the Advanced menu:
FSM726 Management Switch Advanced a. Port Mirroring b. Port Trunking c. Virtual Cable Tester d. Advanced Security e. Advanced Tools f. Traffic Managment g. VLANS h. Spanning Tree i. MAC j. Multimedia Support k. SNMP l. Command Line
Now what I want is that I want to get all the sub-menu's name of a certain main menu, for example, I want to get all the sub menu of Advanced menu, please help me figure out how to do it? Note, this is not the same as the CLI style.

Replies are listed 'Best First'.
Re: How to grab the telnet screen output?
by glide (Pilgrim) on Jan 28, 2008 at 11:05 UTC
    Hi,

    You'll need to use the waitfor and print (Net::Telnet) to process the initial menus. Something like this ...

    $telnet->waitfor(Match => m{$last_line_of_the_1st_menu}smx); $telnet->print(q{a}); $telnet->waitfor(Match => m{$last_line_of_the_2nd_menu}smx); $telnet->print(q{b});
    For debugging, and you'll need it, use the input_log, to see all the interaction with the NetGear switch.
      Thanks:-)

      I forgot to mention that I was using Net::Telnet module. Now the problem is kind of fixed, but some wired problem just remain :-(

      I use get to get the content of sub menu, and remove the telnet control sequences, then have the clean and tidy text manipulated.

      The problem is that I can not use getline, or getlines, they offer nothing to me. And I can not use waitfor because the last several lines of the control menu are all same, so...

      And also, I found that Net::Telnet may have some problem in dealing with TCP packets, sometime it just mis-treat a packet end as a EOF and return the incomplete content to me when there are still some packets to read. The timeout value is enough for it, I am sure.

        Hi,

        Normally, I use the waitfor to match a temporary changed "prompt", like a --more--. And, as you can read in the Net::Telnet with

        ($prematch, $match) = $obj->waitfor($matchop); $obj->print(q{a}) if( $prematch =~ m{f\. Advanced}sm ); $obj->print(q{b}) if( $prematch =~ m{l\. Command Line}sm );
        you can use the $prematch to get the context.

        Probably, your problem it's not with the TCP packets and the Net::Telnet, but with a premature match. Use the dump_log to inspect the flow of the session, and last, but not the least, use the perl -d to check each telnet interaction.

Re: How to grab the telnet screen output?
by moritz (Cardinal) on Jan 28, 2008 at 09:08 UTC
    You could try to grab the contents with Net::Telnet and parse them. Send an a to the server, parse the response, go back to the main menu, send a b, parse the response and so on.