#!/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";
}
I am trying to chenge the users password on a remote host by logging to the host as root
but $username, $password doesn’t seem to take the values instead if i give hard coded values inside the code it works.
running like this on command line :
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#
how can i change the users password remotely
The problem is that you are using single quotes here:
First off, it is quite unnecessary to quote a variable this way. Second, when using single quotes, the content is not interpolated, it is just the literal string
$ARGV[0].This should be:
But a more elegant solution is:
Take advantage of the possibility to make the assignment in list context. Or:
shiftwill implicitly shift arguments off either@ARGVor@_, depending on context (whether you are inside a subroutine or not).