Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl Expect Command output

by anilhpn (Initiate)
on Aug 27, 2011 at 20:24 UTC ( #922838=perlquestion: print w/ replies, xml ) Need Help??
anilhpn has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, Look at following code:

use strict; use Expect; my $timeout = 10; my $command = 'telnet'; my $params = ("10.123.125.13"); # Create the Expect object my $exp = Expect->spawn($command, $params) or die "Cannot spawn sftp c +ommand \n"; $exp->expect($timeout, ["mpp>"]); $exp->send("enable\n"); $exp->expect($timeout, ["mpp#"]); $exp->send("help\n"); # Help gives list of available commands $exp->expect($timeout, ["mpp#"]); $exp->send("exit\n"); $exp->send("logout\n"); $exp->soft_close();

Here I would like get output after command 'help\n'. I tried with $exp->before and after with no success. Please let me know if anybody knows a good solution (no need end-to-end code atleast any idea should be fine). Thanks! Anil A Kumar

Comment on Perl Expect Command output
Download Code
Re: Perl Expect Command output
by Khen1950fx (Canon) on Aug 27, 2011 at 22:53 UTC
    First, try running it in debug mode
    #!/usr/bin/perl use strict; use warnings; use Expect; $Expect::Debug = 1; my $timeout = 10; ...
    Expect the unexpected:).

    Update: In case you're still stumped, I put help in a callback. Give this a try:

    #!/usr/bin/perl -slw use strict; use Expect; use Data::Dumper::Concise; #$Expect::Debug = 1; my $timeout = 10; my $command = 'telnet'; my $params = '10.123.125.13'; my $exp = Expect->spawn($command, $params) or die "Cannot spawn sftp command"; $exp->expect($timeout, ['mpp>']); $exp->send('enable'); $exp->expect($timeout, ['mpp#']); $exp->send(&help); $exp->expect($timeout, ['mpp#']); $exp->send('exit'); $exp->send('logout'); $exp->soft_close(); sub help { print "help\n\n", "List of available commands\n", " The current commands are: \n", " enable\n", " help\n", " exit\n", " logout\n"; }

      They seem to be asking for the output from a command ('help' in this case) executed on the remote device. before is the way to do it. What debugging steps have you taken already and what did you get? Since you are not error checking your spawn, do you even know if you're successfully connecting?

      --Dave

        Thanks for the info. I had tried with a command which stores the buffer content of 'just before executed command'.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-09-20 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls