Solved

Issue with Expect 1.21

Posted on 2012-03-23
1
367 Views
Last Modified: 2012-03-23
Hello Folks! Env=SUSE 10.2 Linux, Perl 5.14.2, Expect v1.21
I really need some help - this issue has tied me up 4 days now....
I am trying to use Expect to ssh from a SuSE server to a Solaris 8 server.
The manual dialog for this login is:
>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.
>

Open in new window

My expect code spawns the ssh command all ok and it complete the login and password ok.
Then I try to send the 'ls' command and I never get anything back in response - it is like the send command never happens.
This is driving me crazy. I copied the code from pl scripts I wrote a few years ago
on Sol8 and they all worked perfectly - I just don't understand what the issue is.
Back then I was using Perl 5.8.8 on Solaris8 and Expect 1.15. - never ever had any issues.
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;

Open in new window


######################################################

Screen shots of expect debugs:
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
        ];

Open in new window

As you can see directly above, for some reason my expect info is nulled out????
I would apprecite any help or suggestions - I'm getting desperate now. :(
0
Comment
Question by:STYX757
1 Comment
 

Accepted Solution

by:
STYX757 earned 0 total points
Comment Utility
Resolved! Apparently Solaris 8 likes to have it's responses and commands terminated with \n and not \r.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now