#!/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" #### #!/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 logged 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 output) $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 **NOT** 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";