[Ssh-sftp-perl-users] Net::SSH::Expect
Brought to you by:
dbrobins
|
From: David M. F. <mf...@tr...> - 2007-12-31 00:10:16
|
Folks,
I want to ssh into my aix server and run the passwd interactive command to
change password automatically.
However, The waitfor never works correctly. It always dies with prompt
'Password:' not found after 1 seconds
Actual ssh session:
Server1[/home/user1]$ sudo passwd user2
Password:
Changing password for "user2"
user2's New password:
Enter the new password again:
Code snippet:
use Net::SSH::Expect;
my $ssh = Net::SSH::Expect->new (
host => "$sship",
port => 22,
password=> "$pass",
user => "$user",
raw_pty => 1
);
# 2) logon to the SSH server using those credentials.
# test the login output to make sure we had success
my $login_output = $ssh->login();
print "Login Output->$login_output<-\n";
if ($login_output !~ /Wlecome/) {
die "Login has failed. Login output was $login_output";
} else {
print "Login Successful!!\n";
}
# Now let's run an interactive command, like passwd.
# This is done combining send() and waitfor() methods together:
print "Sending Command->sudo passwd $userid<-\n";
$ssh->exec("sudo passwd $userid");
$ssh->waitfor('Password:', 1) or die "prompt 'Password:' not found after
1 seconds";
print "Sending Password->$pass<-\n";
$ssh->send("$pass");
$ssh->waitfor('New password:\s*\z', 1) or warn "prompt 'New password:'
not found";
print "Sending Temporary Password->$temp_pass<-\n";
$ssh->exec("$temp_pass");
$ssh->waitfor('new password again:\s*\z', 1) or warn "prompt 'Confirm new
password:' not found";
print "Sending Temporary Password again->$temp_pass<-\n";
$ssh->exec("$temp_pass");
# check that we have the system prompt again.
$ssh->waitfor('$\s*\z', 1) or warn "No System Prompt\n";
$ssh->send("exit");
# closes the ssh connection
$ssh->close();
}
1;
TIA,
Mark
|