Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
>ssh -o StrictHostkeyChecking=no user@sol8server
Could not create directory '/appl/user/.ssh'.
Failed to add the host to the list of known hosts (/appl/user/.ssh/known_hosts).
user@sol8server's password:
Last login: Tue Mar 20 13:43:00 2012 from xxx
Could not chdir to home directory /home/user: No such file or directory
MANAGEMENT APPROVED PURPOSES ONLY
**** SYSTEM ACTIVITIES MAY BE MONITORED ****
sol8server% ls
blah blah blah etc lost+found op sbin vol
sol8server% logout
Connection to sol8server closed.
>
My expect code spawns the ssh command all ok and it complete the login and password ok.My Code:
my $expTimeOut = 6; # 6 secs
my $expPty = 1; # pty mode
my $expDebug = 3; # Expect debug level
# Build Expect object
######################################################
$sshFW = Expect->new; # create Expect object
$sshFW->debug($expDebug); # debugging
$sshFW->log_file($expFWLogFile, "w"); # open Expect log with overwrite mode
$sshFW->raw_pty($expPty);
######################################################
# Spaw a ssh session
######################################################
$cmd = "ssh -o StrictHostkeyChecking=no $usr\@$FWHost";
print "\nSpawing command [$cmd]\n\n";#debug
if (!$sshFW->spawn($cmd)) {
# spawn failed
$msg = "Error,Spawn cmd [$cmd] failed for device [$FWHost].\n";
print $msg;
$sshFW->hard_close; # release object
die $msg; #abort program!
} # endif
$msg = "\nSuccessful spawn cmd [$cmd]\n\n";
print $msg;
######################################################
# OK, we got a good spawn! now try login sequence
######################################################
$expFWStatus = 0;
$expFWmatchString = "";
@exp_stat = $sshFW->expect($expTimeOut,
[qr /password: $/ => # got password prompt
sub {
my $self = shift;
$self->send("$passw\r");
print "Sent password!\n";#debug
exp_continue; # responded to password prompt, continue
} # end sub
],
# Timeout trap
[timeout =>
sub {
$expFWStatus = 1;
$expFWmatchString = $sshFW->match;
print "Error,Timeout while waiting $expTimeOut secs for login on [$FWHost] user[$usr]. Error:" . $sshFW->exp_error() . "\n";
}
],
# EOF trap
[eof =>
sub {
$expFWStatus = 2;
$expFWmatchString = $sshFW->match;
print "Error,Premature EOF during login for [$FWHost] user[$usr]. Error:" . $sshFW->exp_error() . "\n";
}
],
'-re', qr /% $/, # wait for device prompt '%' and exit expect; all logged in now
); # End expect for Login processing
print "\nFinished processing FW login expect expFWstatus[$expFWStatus]\n\n"; # debug
if ($expFWStatus) { # check results of login attempt
$msg = "Login failed, returning. expFWStatus[$expFWStatus]. Last string matched was [$expFWmatchString]\n"; # debug
print $msg;
die $msg; # abort ptogram
} # endif
$msg = "\nInfo,Login OK for [$FWHost] user[$usr]\n\n";
print $msg;
print Dumper(\@exp_stat);#debug
#############################################
print "\nSending ls command!\n\n";
$sshFW->send("ls\r");
@exp_stat = $sshFW->expect($expTimeOut,
# Timeout trap
[timeout =>
sub {
$expFWStatus = 1;
$expFWmatchString = $sshFW->match;
print "Error,Timeout while waiting $expTimeOut secs for ls command on [$FWHost] user[$usr]. Error:" . $sshFW->exp_error() . "\n";
}
],
# EOF trap
[eof =>
sub {
$expFWStatus = 2;
$expFWmatchString = $sshFW->match;
print "Error,Premature EOF during ls command for [$FWHost] user[$usr]. Error:" . $sshFW->exp_error() . "\n";
}
],
'-re', qr /% $/, # wait for device prompt '%' and exit expect; all logged in now
); # End expect
print "\nContinuing\n\n";
print Dumper(\@exp_stat);#debug
print "\nClosing connection to FW Host [$FWHost].\n\n";
$sshFW->send("logout\r");
if (!$sshFW->expect($expTimeOut, 'closed.')) {
print "Warning,Did not get connection closed on [$FWHost]. Error:" . $sshFW->exp_error() . "\n";
print "Warn: Issue with closing FW Host [$FWHost].\n";
} # endif
else {
# ok, we got the logout comfirmation....
print "Info,Connection closed: [$FWHost].\n";
}
$sshFW->soft_close;
print "\nConnection closed to FW Host [$FWHost].\n\n";
exit;
Spawing command [ssh -o StrictHostkeyChecking=no user@sol8server]
Spawned 'ssh -o StrictHostkeyChecking=no user@sol8server'
spawn id(3)
Pid: 29798
Tty: /dev/pts/1
at /export/core/perl5/5.14.2/lib/site_perl/5.14.2/Expect.pm line 181
Expect::spawn('Expect=GLOB(0x65ca28)', 'ssh -o StrictHostkeyChecking=no user@sol8server') called at ./fwlogin.pl line 35
Successful spawn cmd [ssh -o StrictHostkeyChecking=no user@sol8server]
Starting EXPECT pattern matching...
at /export/core/perl5/5.14.2/lib/site_perl/5.14.2/Expect.pm line 561
Expect::expect('Expect=GLOB(0x65ca28)', 6, 'ARRAY(0x9ad490)', 'ARRAY(0x9ad9e8)', 'ARRAY(0x9bbb88)', '-re', 'Regexp=REGEXP(0x9bc050)') called at ./fwlogin.pl line 73
Could not create directory '/appl/user/.ssh'.
Failed to add the host to the list of known hosts (/appl/user/.ssh/known_hosts).
user@sol8server's password: Sent password!
Last login: Thu Mar 22 15:00:26 2012 from xxx
Could not chdir to home directory /home/user: No such file or directory
MANAGEMENT APPROVED PURPOSES ONLY
**** SYSTEM ACTIVITIES MAY BE MONITORED ****
sol8server%
Finished processing FW login expect expFWstatus[0]
Info,Login OK for [sol8server] user[user]
$VAR1 = [
4,
undef,
'% ',
'
Last login: Thu Mar 22 15:00:26 2012 from xxx
Could not chdir to home directory /home/user: No such file or directory
MANAGEMENT APPROVED PURPOSES ONLY
**** SYSTEM ACTIVITIES MAY BE MONITORED ****
sol8server',
'',
bless( \*Symbol::GEN0, 'Expect' )
];
Sending ls command!
Starting EXPECT pattern matching...
at /export/core/perl5/5.14.2/lib/site_perl/5.14.2/Expect.pm line 561
Expect::expect('Expect=GLOB(0x65ca28)', 6, 'ARRAY(0x9ad6a0)', 'ARRAY(0x9bc308)', '-re', 'Regexp=REGEXP(0x9ad418)') called at ./fwlogin.pl line 109
Error,Timeout while waiting 6 secs for ls command on [sol8server] user[user]. Error:1:TIMEOUT
Continuing
$VAR1 = [
undef,
'1:TIMEOUT',
undef,
'',
undef,
undef
];
As you can see directly above, for some reason my expect info is nulled out????Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.