How do I send a password to a command I start with Perl's Expect.pm?
I have the following script,
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
use Expect;
my $logs = "logs";
open(LOG,'>>',"$logs") or die "can't logs $!\n";
my $domain = 'domain.com';
my @host = qw/host/;
foreach my $host (@host) {
my $cmd = "passwd user1";
my $sshost = join('.', $host, $domain);
my $ssh = Net::SSH::Perl->new("$sshost");
$ssh->login('root');
$ssh->debug();
my ($stdout, $stderr, $exit) = $ssh->cmd($cmd);
print LOG $stdout,"\n";
}
Now my problem is I don't know how to use Expect to send the password after the $cmd
is executed and it's time to key in the p开发者_StackOverflow中文版assword. $stdin
won't work in this case since we're using HPUX.
Appreciate any guidance and sample, reading the Expect docs don't result something for me.
I don't think that's possible unfortunately. However, Net::SSH::Expect seems to be able to do what you want.
I summarize: you need Expect, and the ssh module has no use.
I'll be more precise: if I understand your source code, your requirement, in human terms, is something like this: log in to a collection of Unix hosts and use passwd(1) to update root's password on each. Do I have that right?
I expect there's frustration in all directions, because variations of this question have been answered authoritatively for at least two decades. That's no reflection on you, because I recognize how difficult it is to find the correct answer.
While Net::SSH is a fine and valuable module, it contributes nothing to the solution of what I understand to be your requirements. You need Expect.
As it turns out, the standard distribution of the Tcl-based Expect includes an example which addresses your situation. Look in http://www.ibm.com/developerworks/aix/library/au-expect/ > for the description of passmass.
Identical functionality can be coded in Expect.pm, of course. Before I exhibit that, though, I ask that original questioner lupin confirm I'm on track in addressing his true requirements.
i think i had a similar issue getting into privileged exec mode with cisco routers, which similarly asks for a password when "en" is invoked. i got around it with a special subroutine:
sub enable { my ($expect_session, $password) = @_;
$expect_session->send("en\n");
$expect_session->expect($timeout,
[ qr/[Pp]assword:/,
sub {
my $expect_session = shift;
$expect_session->send("$password","\n");
exp_continue;
} ],
-re => $prompt,
);
}
but i think the issue is that you're not using Perl's Expect as it's intended to be used. An Expect session be created to manage the SSH connection, then commands are sent to through it. you don't need Net::SSH:Perl at all. here's my $expect_session definition:
my $expect_session = new Expect();
$expect_session->log_stdout(0); # let's keep things quiet on screen; we only want command output.
$expect_session->log_file(".expectlog");
$expect_session->spawn("/usr/bin/ssh -o StrictHostKeyChecking=no $username\@$host")
or die ("\nfor some reason we can't establish an SSH session to $host.\n
it's something to do with the spawn process: $!\n");
there might be a few pieces missing, but hopefully this will get you moving in the right direction. it's a complicated module which i don't understand fully. i wish you the best in luck getting it to do what you want.
Net::OpenSSH can be combined with Expect to do that easyly.
Actually the module distribution contains a sample script that does just that!
精彩评论