Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Perl "expect" script, capturing output not working

by FirtyFree (Initiate)
on Jan 26, 2012 at 14:30 UTC ( #950094=perlquestion: print w/replies, xml ) Need Help??
FirtyFree has asked for the wisdom of the Perl Monks concerning the following question:

Hi all I've written a Perl based expect "script" that utilises SSH to connect to a SBC and execute various commands that I want to capture in order to provide monitoring of the appliance. Presently the only way I can acquire the output from the "Expect" script is to not use the Perl module "Expect", here is the working script:
#!/usr/bin/expect set timeout 10 log_user 0 spawn ssh expect "?*" send "show sessions\r" log_user 1 expect "?*" send "exit\r"
This produces output that I can use but I want to use Perl to do this, here is my Perl script so far:
#!/usr/bin/perl -w # use strict; use expect; # Vars our ($timeout,$output,$process); $timeout = $output = $process = ""; # SSH vars my $sshUsername = "user"; my $sshHostname = ""; my $sshTimeout = 10; my $sshCommand = "/usr/bin/ssh"; # SBC vars my $sbcCommand = "show sessions"; my $sbcExitcmd = "exit"; # Diagnostic options #$Expect::Exp_Internal = 1; # Turn on debugging #$Expect::Log_Stdout = 0; # Turn off debugging # Initialize expect based SSH session to remote host my $exp = Expect->spawn($sshCommand, $sshHostname, $sshUsername); # Lets ensure that we accept the initial key request if we have not lo +gged in before #$exp->expect($timeout, # ["Are you sure you want to continue connecting", sub { +my $self = shift; $self->send("yes\n");}] # ); # Wait for SBC prompt print "About to execute the command\n"; $exp->expect($timeout, ["."]); # Send SBC command $exp->send("$sbcCommand\n"); # Wait for subsequent prompt (any prompt, we just want the command out +put) $exp->expect($timeout, ["."]); # Send exit command to quit the SBC shell $exp->send("$sbcExitcmd\n"); # Capture output (THIS DOES NOT WORK, ONLY SHOWS COMMANDS EXECUTED **N +OT** THE COMMAND OUTPUT) $output = $exp->before(); print "output is: $output\n"; # Destroy the expect object $exp->soft_close(); # Output stdout #print "output is: @output\n";
The perl script just prints the commands I want to run without the actual output from the commands on the SBC themselves!! Does any one have any ideas what I'm doing wrong in the Perl script? Much appreciate any help you can provide

Replies are listed 'Best First'.
Re: Perl "expect" script, capturing output not working
by olibertu (Initiate) on Feb 29, 2012 at 20:42 UTC
    my $ref = $exp->send("$cmd\n"); sleep(1); $ref = $exp->expect(TIMEOUT, [ $shell_prompts ] ); if (!$ref){ $log->warn("Timeout received!"); return -1; } $output = $exp->exp_before(); $output =~ s/^.*\r\n(.*)/$1/;
    for multiple lines you must also split all lines
    my @values = split(/\r\n/, $output); foreach my $val (@values) { print "$val\n"; }
    For debugging all characters in output string please use next code (decode all characters to ASCII):
    sub ENCRYPT_DECRYPT() { my $Str_Message=$_[0]; my $Len_Str_Message=length($Str_Message); my $Str_Encrypted_Message=""; for (my $Position = 0;$Position<$Len_Str_Message;$Position++){ my $Key_To_Use = (($Len_Str_Message+$Position)+1); $Key_To_Use =(255+$Key_To_Use) % 255; my $Byte_To_Be_Encrypted = substr($Str_Message, $Position, 1) +; my $Ascii_Num_Byte_To_Encrypt = ord($Byte_To_Be_Encrypted); print "$Ascii_Num_Byte_To_Encrypt\n"; my $Xored_Byte = $Ascii_Num_Byte_To_Encrypt ^ $Key_To_Use; my $Encrypted_Byte = chr($Xored_Byte); $Str_Encrypted_Message .= $Encrypted_Byte; } return $Str_Encrypted_Message; } my $var=&ENCRYPT_DECRYPT($exp->exp_before());

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-02-27 05:52 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (376 votes). Check out past polls.