----------------------------------------------------------------------------
1. Using expect package.
#!/usr/bin/expect
set newpassword [lindex $argv 1]
spawn /usr/bin/passwd [lindex $argv 0]
expect "New password:"
send "$newpassword\r"
expect "Re-enter new password:"
send "$newpassword\r"
expect eof
----------------------------------------------------------------------------
2. In case your operating system do not have expect package then you can do it using the perl script.
#!/usr/bin/perl -w
#######################################################################################
# Usage : $0
# Note : This script need to be run as root.
# Author : Rishi Pahuja
# Created : 01 Oct 2007
#######################################################################################
use strict;
my ($usr, $plain) = @ARGV;
my $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
my $salt;
for (1..2) { $salt .= substr $itoa64, rand(length($itoa64)), 1; }
my $password = crypt($plain, $salt);
#print `usermod -p $password $usr`;
# Changing the password
system("usermod -p $password $usr");
if ( $? == 0 ) {
print "\nOS password changed successfully for user $usr to $plain.\n";
}
else {
print "ERROR: Changing password for user $usr.\n";
print " Return Code: $?\n";
print "Exiting with return code of 1.\n";
exit 1;
}
No comments:
Post a Comment