- 論壇徽章:
- 0
|
程序代碼如下:
#!/usr/bin/perl
use strict;
use Expect;
my $hostname;
my $newpass;
my $host;
my $pass;
my $port;
open (FH, "</home/script/hostlist.txt") or die "cannot open file $!";
while (<FH>) {
chomp;
if ($_ !~ /^#/) {
($hostname, $host, $pass, $newpass, $port) = split;
print "$host, $pass, $newpass\n";
my $exp = Expect->new;
$exp->log_stdout(0);
if (defined $port) {
$exp = Expect->spawn("ssh -l root -p $port $host");
} else {
$exp = Expect->spawn("ssh -l root $host");
}
$exp->log_file("ssh_host.log","a");
$exp->expect(3,[
qr/connecting \(yes\/no\)/i,
sub {
my $self = shift;
$self->send("yes\n");
exp_continue;
}
],
[
qr/password:/i,
sub{
my $self = shift;
$self->send("$pass\n");
exp_continue;
}
]
);
open (COMMAN, "/home/script/hostcommand.txt") or die "cannot open file $!";
while (<COMMAN>) {
if ($_ !~ /^#/) {
chomp;
if ($exp->expect(undef, '#')){
$exp->send("$_\n");
$exp->expect(3,[
qr/password:/i,
sub {
my $self = shift;
$self->send("$newpass\n");
exp_continue;
}
]);
}
}
}
$exp->send("exit\n\n") if ($exp->expect(undef, '#'));
$exp->log_file(undef);
}
}
現(xiàn)在的情況是登錄服務(wù)器成功后, 執(zhí)行passwd 用戶名命令后, 提示輸入密碼, 但看程序是已經(jīng)執(zhí)行成功, 可密碼卻沒有改成功, 哪位幫忙看看, 主要問題應(yīng)該出在執(zhí)行完passwd 用戶名命令后的$exp->expect這部份. |
|