Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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 = (""); # 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

Replies are listed 'Best First'.
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 = ''; 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?


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://922838]
Approved by koolgirl
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-06-18 19:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.