无法使用 Net::SSH::Expect在远程机器中执行命令



我想建立远程连接并自动更改该远程机器中任何用户帐户的密码,所以我正在使用Perl Net::SSH::Expect模块。我的连接正在发生,但我无法在远程计算机上执行任何命令。我是Perl语言的新手,所以我的代码中可能存在一些错误。我尝试搜索这个,但找不到与之相关的内容。

最初,我尝试结合使用Net::OpenSSH和Expect模块,但无法实现用例。

#!/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 $uNamen";
#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("oldPasswordn");
$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( 的更改密码用例不是。

#!/usr/bin/perl
use strict;
use warnings;
#use Net::OpenSSH;
#use Expect;
use Net::SSH::Expect;
#
# You can do SSH authentication with user-password or without it.
#
my $uName= "admin1";
# Making an ssh connection with user-password authentication
# 1) construct the object
my $ssh = Net::SSH::Expect->new (
    host => "192.168.56.101",
    password=> "Passw0rd1",
    user => "admin1",
    raw_pty => 1
);
# 2) logon to the SSH server using those credentials.
# test the login output to make sure we had success
my $login_output = $ssh->login();
if ($login_output !~ /Welcome/) {
    die "Login has failed. Login output was $login_output";
}
print "Connected to $uNamen";

这是我尝试执行的新简单命令

my $who = $ssh->exec("who");
print ($who);

我的旧用例从 send(( 中删除了模具

$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("Passw0rd1n");
$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$

根据文档:

void send($string( - 将$string发送到 SSH 服务器,不返回任何内容
将字符串发送到 SSH 服务器。

所以不要检查返回值,只需删除send()后的or die ..部分:

$ssh->send("passwd");   # <-- no error code is returned here..

相关内容

  • 没有找到相关文章

最新更新