如何使用Net :: OpenSSH更改远程计算机上的root密码?

时间:2011-05-31 06:54:32

标签: perl ssh root change-password

我想用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";

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

2 个答案:

答案 0 :(得分:4)

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

change_passwd.pl

答案 1 :(得分: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:当然,检查错误,看看发生了什么。在调试时丢弃错误是Bad Practice(tm)。

ETA2:尝试使用--stdin的{​​{1}}选项,看看是否有帮助。 E.g:

passwd