1

我想用 Perl 脚本更改远程 Linux 机器上的 root 密码。我的第一次尝试是以下代码:

use Net::OpenSSH;
my $ssh = Net::OpenSSH->new(
    "linuxpc",
    user                  => "root",
    password              => "root",
    master_stderr_discard => 1
);
my @changepass = $ssh->capture(
    {
        stderr_discard => 1,
        stdin_data     => "newpw123"
    },
    "passwd"
);
print "Done\n";

但不幸的是,它不起作用。有人可以帮我吗?

4

2 回答 2

4

Net::OpenSSH发行版包含一个示例脚本,它完全符合您的要求!

change_passwd.pl

于 2011-05-31T14:11:22.153 回答
0

而不是丢弃你的错误,使用capture2

($output, $errput) = $ssh->capture2(\%opts, @cmd)

captures the output sent to both stdout and stderr by @cmd on the

远程机器。

引自CPAN

此外,可能不相关,但可能使用passwd. 我不确定该capture函数是否添加了换行符,但值得一试:

my @pwd = ("newpw123\n", "newpw123\n");
($output, $errput) = $ssh->capture2( { stdin_data = \@pwd }, "/bin/passwd" );

ETA:当然,检查错误以了解发生了什么。在调试时丢弃错误是不好的做法 (tm)。

ETA2:尝试使用 的--stdin选项passwd,看看是否有帮助。例如:

$ssh->capture2( { stdin_data = \@pwd }, "/bin/passwd --stdin" );
于 2011-05-31T08:44:08.587 回答