Perl 无法使用Net::SSH::Expect在远程计算机中执行命令
我想建立远程连接并自动更改远程机器中任何用户帐户的密码,因此我使用Perl Net::SSH::Expect模块。我的连接正在进行,但我无法在远程计算机上执行任何命令。我不熟悉Perl语言,所以我的代码中可能有一些错误。我试着搜索这个,但找不到与之相关的东西 最初,我尝试结合使用Net::OpenSSH和Expect模块,但未能实现用例Perl 无法使用Net::SSH::Expect在远程计算机中执行命令,perl,expect.pm,perlnetssh,Perl,Expect.pm,Perlnetssh,我想建立远程连接并自动更改远程机器中任何用户帐户的密码,因此我使用Perl Net::SSH::Expect模块。我的连接正在进行,但我无法在远程计算机上执行任何命令。我不熟悉Perl语言,所以我的代码中可能有一些错误。我试着搜索这个,但找不到与之相关的东西 最初,我尝试结合使用Net::OpenSSH和Expect模块,但未能实现用例 #!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; use Expect; use Ne
#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;
use Expect;
use Net::SSH::Expect;
my $uName= "user";
my $ssh = Net::SSH::Expect->new (
host => "IPAddress",
password=> "userPassword",
user => $uName,
raw_pty => 1
);
# logon to the SSH server using those credentials.
my $login_output = $ssh->login();
if ($login_output !~ /Welcome/) {
die "Login has failed. Login output was $login_output";
}
#Random line display
print "Connected to $uName\n";
#This is where I was trying the change password use case
$ssh->send("passwd") or die "Cannot execute the passwd command: $!\n";
$ssh->waitfor("Changing password for user.\n
(current) UNIX password: ", 3) or die "prompt 'password' not found after 1 second";
$ssh->send("oldPassword\n");
$ssh->waitfor("\n
Enter new UNIX password: " , 2) or die "prompt 'New password:' not found";
$ssh->send("newPassword");
$ssh->waitfor("\n
Retype new UNIX password: ", 1) or die "prompt 'Confirm new password:' not found";
$ssh->send("newPassword");
#$ssh->send("") or die "Cannot spawn: $!\n";
$ssh->close();
这是我得到的输出
Cannot execute the passwd command
新更新
我尝试执行一个简单的命令,只是为了检查是否有任何命令正在执行。尽管如此,我使用send(passwd)更改密码的用例仍然不是
这是我正在尝试执行的新的简单命令
my $who = $ssh->exec("who");
print ($who);
我的旧用例,其中模具已从send()中移除
现在输出是这样的
admin3@admin3-VirtualBox:~/Desktop$ perl NetSSHExpect.pl
Connected to admin1
admin1 :0 2019-07-19 11:49 (:0)
admin1 pts/1 2019-07-19 12:41 (192.168.56.1)
admin1@admin1-VirtualBox:~$ prompt 'password' not found after 2 second at NetSSHExpect.pl line 36.
admin3@admin3-VirtualBox:~/Desktop$
根据:
void send($string)-将$string
发送到SSH服务器,不返回任何内容将字符串发送到SSH服务器 因此,不要检查返回值,只需在
send()之后移除或die..
部分即可:
$ssh->send(“passwd”)#你的等待模式看起来很奇怪。你有一个换行符和一个文字换行符:你确定两者都需要吗?我也试过了,但输出还是一样。我试过了,但现在我的下一个等待死亡提示。我尝试执行一个简单的命令来检查是否有任何命令正在执行。是的,但是输出仍然是我所期望的。如果您可以检查我的问题的更新,请不要在waitfor()
调用中包含所有文本。如果只有字符串的一部分匹配,那么它仍然会成功,并且字符串中不太可能有与服务器回声稍有不同的内容,然后waitfor
将失败。例如:尝试:$ssh->waitfor(“UNIX密码:”)或die…
谢谢,现在删除一些单词后它就可以工作了。
$ssh->send("passwd");
$ssh->waitfor("Changing password for $uName.\n(current) UNIX password: ", 2) or die "prompt 'password' not found after 2 second";
$ssh->send("Passw0rd1\n");
$ssh->waitfor("\nEnter new UNIX password: " , 2) or die "prompt 'New password:' not found";
$ssh->send("qwerty123");
$ssh->waitfor("\nRetype new UNIX password: ", 1) or die "prompt 'Confirm new password:' not found";
$ssh->send("qwerty123");
$ssh->close();
admin3@admin3-VirtualBox:~/Desktop$ perl NetSSHExpect.pl
Connected to admin1
admin1 :0 2019-07-19 11:49 (:0)
admin1 pts/1 2019-07-19 12:41 (192.168.56.1)
admin1@admin1-VirtualBox:~$ prompt 'password' not found after 2 second at NetSSHExpect.pl line 36.
admin3@admin3-VirtualBox:~/Desktop$
$ssh->send("passwd"); # <-- no error code is returned here..