Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Need help getting output from Telnet into a string or array

by samtregar (Abbot)
on Mar 15, 2012 at 17:09 UTC ( #959823=note: print w/ replies, xml ) Need Help??

in reply to Need help getting output from Telnet into a string or array

Here's the lines from Net::Telnet's docs for the cmd() method that explain what's happening:

In a scalar context, the characters read from the remote side are discarded and 1 is returned on success. On time-out, eof, or other failures, the error mode action is performed. See errmode().

In a list context, just the output generated by the command is returned, one line per element. In other words, all the characters in between the echoed back command string and the prompt are returned. If the command happens to return no output, a list containing one element, the empty string is returned. This is so the list will indicate true in a boolean context. On time-out, eof, or other failures, the error mode action is performed. See errmode().

So you need to call it in list context if you want the output (assign the result to @array, not a scalar like $output).


PS: For a brand new coder your code-quality looks great!

Comment on Re: Need help getting output from Telnet into a string or array
Replies are listed 'Best First'.
Re^2: Need help getting output from Telnet into a string or array
by TravisC (Novice) on Mar 15, 2012 at 18:46 UTC

    Thanks for your post. I guess I missed that tidbit of info. I can see it pass the 1 when I query the string. I modified my call line to the following

    @array=$telnet->cmd(string => 'ls', prompt => '/=>/');

    to explain the prompt tag, I found the following tidbit of info

    To run a program and gather its output, use the cmd method. Pass it the string to send; it returns the command output as one line per list element in list context, or as one long line in scalar context. It waits for the Prompt before returning.

    my array is still coming back empty, I'm guessing I'm not passing the information via a list context, so how would I go about doing it? Thanks again for all your help!

      I looks like someone else has done similar work.. In their example, they used:

       Prompt     => '/'.$switch.'.*?# /',

      ... where $switch is the name of the switch ... so the text of the prompt would be the switchname and maybe something else, ending in '# '

      ... but they also had to do extra checking to deal with some other differences between software versions, so this may be a more complex beast to deal with. Odds are, with any piece of networking gear that's out there, someone's already figured out how to interact with in in Perl, even if it means screen scraping HTML GUIs, so it's worth searching for "(hardware name) perl" in your preferred search engine.

        Thanks for the post. Right now I think it has more to do with my inexperience of passing values in a list context to an array than it does with the switch. I've tried creating the same desired result on other prompts (telnet on my local desktop) and got nothing but failure. Like I said in one of my previous posts, I'm able to pass the individual lines to $values but unable to pass the list context values to an @array. Any and all help would be greatly appreciated. Thanks!
      Is the prompt actually /=>/? That's an odd prompt. Maybe you should try '=>'? Or perhaps qr/=>/ if it's expecting a regex.

      I don't know if this will be more distracting than useful, but if I was coding this I would use Expect. It takes a little getting used to but it has great debugging features.


        the prompt for the switch is => fun huh... when I scale it to '=>' I get "ignoring bad Prompt argument "=>": missing opening delimiter of match operator". I'll try the expect when I can. like I've mentioned I'm new to the whole coding world, so I'm taking it one command and module at a time.
        ok with plenty of fiddling around here's what I've been able to decipher. I pass
        my (@array)=$telnet->cmd(string => 'ls', prompt => '/=>/');
        gives be one line, which is blank. Though if I pass this line
        my ($a, $b, $c)=$telnet->cmd(string => 'ls', prompt => '/=>/');
        and print the results I get 3 lines of output so the system will pass in list context, but I fear I'm missing why it won't pass it to an array. below is the output from the a,b,c variables.
        btm.swi secondary.swi cfg/

        Any thoughts?

        thanks for all your input!
Re^2: Need help getting output from Telnet into a string or array
by TravisC (Novice) on Mar 16, 2012 at 19:08 UTC
    Success! Kinda... Working on this for the last 4 hours and I've came up with a result. When I issue my line
    @array=$telnet->cmd(string => 'ls', prompt => '/=>/');
    it STOPS at the first prompt where the ls is being issued. I changed my code to
    @array=$telnet->cmd(string => 'ls', prompt =>'/ls/');
    my output is => (the prompt before the ls). I tested this with another switch which has a different output and had a unique word in it (free) and it produced all info up to that word which is what I believe it's supposed to do. Now I guess what my question is, why is my cmd picking up the first prompt and not the second after the output? Thanks
      If your primary concern here is controlling the switch(es), not just dabbling with Perl for Perl's sake, I recommend looking at Rancid. It offers script wrappers for various vendors and models. You only supply the device command sequence, the scripts deal with login, prompts, output redirection etc.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2015-11-30 09:41 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (769 votes), past polls