如何使用 Net::OpenSSH 更改远程计算机上的 root 密码?
我想使用 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";
但不幸的是它不起作用。有人可以帮我吗?
I want to change the root password on a remote Linux machine with a Perl script. My first try was the following code:
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";
But unfortunately it won't work. Could somebody help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Net::OpenSSH 发行版包含一个示例脚本,它完全可以满足您的需求!
change_passwd.pl
Net::OpenSSH distribution includes a sample script that does exactly what you want!
change_passwd.pl
不要丢弃错误,而是使用 capture2:
引用自 CPAN
另外,可能不是相关,但也许使用
passwd
的完整路径。我不确定capture
函数是否添加了换行符,但它可能值得尝试:ETA:当然,检查错误以查看发生了什么。在调试时丢弃错误是不好的做法 (tm)。
ETA2:尝试对
passwd
使用--stdin
选项,看看是否有帮助。例如:Instead of discarding your errors, use
capture2
:Quoted from CPAN
Also, might not be relevant, but perhaps use the full path to
passwd
. I am not sure whether a newline is added by thecapture
function, but it could be worth trying:ETA: And of course, check the errors to see what's going on. Discarding errors while debugging is Bad Practice (tm).
ETA2: Try using the
--stdin
option forpasswd
, see if that helps. E.g.: