Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 使用use Net::SSH::Expect远程更改用户密码;_Perl - Fatal编程技术网

Perl 使用use Net::SSH::Expect远程更改用户密码;

Perl 使用use Net::SSH::Expect远程更改用户密码;,perl,Perl,我正试图通过以root用户身份登录到远程主机来获取用户密码 但是,$username,$password如果我在代码中给出硬编码的值,那么它似乎不会接受这些值 在命令行上像这样运行: #!/usr/bin/perl use strict; use warnings; my $username = '$ARGV[0]'; my $password = '$ARGV[1]'; use Net::SSH::Expect; my $ssh = Net::SSH::Expect-> new (

我正试图通过以root用户身份登录到远程主机来获取用户密码 但是,
$username,$password
如果我在代码中给出硬编码的值,那么它似乎不会接受这些值

在命令行上像这样运行:

#!/usr/bin/perl

use strict;
use warnings;
my $username = '$ARGV[0]';
my $password = '$ARGV[1]';
use Net::SSH::Expect;
my $ssh = Net::SSH::Expect-> new (
        host => "10.38.228.230",
        password => "lsxid4",
        user => "root",
        raw_pty => 1,
        timeout => 10,
        log_file => "log_file"  
);

my $login_output=$ssh->login();
if ( $login_output =~ /Last/ )
   {
   print "The login for ROOT was successful, Let's see if we can change the password \n";
   $ssh->send("passwd $username");
   $ssh->waitfor ('password:\s*', 10) or die "Where is the first password prompt??";
   $ssh->send("$password");
   $ssh->waitfor ('password:\s*', 10) or die "Where is the Second password promp??";
   $ssh->send("$password");
   $ssh->waitfor('passwd:\s*',5);
   print "The password for $username has been changed successfully \n";
   }
   else
   {
      die "The log in for ROOT was _not_ successful.\n";
   }

如何远程更改用户密码

问题是您在此处使用单引号:

bash-3.00# ./test6.pl rak xyz12   
The login for ROOT was successful, Let's see if we can change the password
Where is the first password prompt?? at ./test6.pl line 22.
bash-3.00#
首先,没有必要这样引用变量。第二,当使用单引号时,内容不是内插的,它只是文本字符串
$ARGV[0]

这应该是:

my $username = '$ARGV[0]';
my $password = '$ARGV[1]';
但更优雅的解决方案是:

my $username = $ARGV[0];
my $password = $ARGV[1];
利用在列表上下文中进行赋值的可能性。或:

my ($username, $password) = @ARGV;

shift
将根据上下文(无论您是否在子例程中)隐式地将参数从
@ARGV
@
中移出。

问题在于您在此处使用单引号:

bash-3.00# ./test6.pl rak xyz12   
The login for ROOT was successful, Let's see if we can change the password
Where is the first password prompt?? at ./test6.pl line 22.
bash-3.00#
首先,没有必要这样引用变量。第二,当使用单引号时,内容不是内插的,它只是文本字符串
$ARGV[0]

这应该是:

my $username = '$ARGV[0]';
my $password = '$ARGV[1]';
但更优雅的解决方案是:

my $username = $ARGV[0];
my $password = $ARGV[1];
利用在列表上下文中进行赋值的可能性。或:

my ($username, $password) = @ARGV;

shift
将根据上下文(无论您是否在子例程中)隐式地将参数移出
@ARGV
@

1。不要不必要地引用变量。2.如果要在引用变量时插入变量,请不要使用单引号。1。不要不必要地引用变量。2.如果在引用变量时希望对变量进行插值,请不要使用单引号。