Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by TravisC (Novice)
on Mar 15, 2012 at 18:46 UTC ( #959835=note: print w/replies, xml ) Need Help??


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

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!

Replies are listed 'Best First'.
Re^3: Need help getting output from Telnet into a string or array
by jhourcle (Prior) on Mar 15, 2012 at 21:43 UTC

    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!
Re^3: Need help getting output from Telnet into a string or array
by samtregar (Abbot) on Mar 15, 2012 at 19:11 UTC
    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.

    -sam

      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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2019-05-24 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (147 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!