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 1.1.1.1 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 = "1.1.1.1"; 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

In reply to Perl "expect" script, capturing output not working by FirtyFree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.