Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I auto generate a password with a commend line script?

Status
Not open for further replies.

eli101

IS-IT--Management
Feb 1, 2001
54
US
Hi all,

I trying to auto generate login = password in Unix and the script shuld add it to /etc/shadow

Anyone know of any auto password-gen or how to add the encrypt password to /etc/shadow (with out need to type the passwd commend on the commend line)

Any help is greatly appreciated
Eli
 
echo passwordXY | /usr/lib/makekey, where password is the first eight letters only of the password, and XY are randomly chosen characters in the range [A-Za-z0-9/.]. Annihilannic.
 
Assuming the user exists on the system:

#! /bin/perl
print "Enter a username: ";
chomp ($user = <>);

system &quot;stty -echo&quot;;
print &quot;Enter a password: &quot;;
chomp($entry1 = <>);
print &quot;\n&quot;;

### 5 - Verify entered password
CHECK: {
print &quot;Re-enter the password: &quot;;
chomp($entry2 = <>);
print &quot;\n&quot;;
if ($entry1 ne $entry2){print &quot;Passwords do not match!\n&quot;; redo CHECK;}
}
system &quot;stty echo&quot;;
### 6 - Encrypt the password/Create shadow entry
@SaltVals = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '.', '/' );
$salt = join(&quot;&quot;, map {$SaltVals[rand(scalar(@SaltVals))]} (1,2));
$Encr_Pass = crypt($entry1, $salt);
$days_epoch = int(time/(60*60*24));
$Shadow_Entry=&quot;${user}:${Encr_Pass}:${days_epoch}::::::&quot;;

### 7 - Create a 'HERE' shell script
open(SCRIPT, &quot;>/tmp/.addpasswd.sh&quot;) || die &quot;Can't open script file in /tmp: $!\n&quot;;
print SCRIPT <<HERE;
#! /bin/sh
# NOTE: this does not check if the shadow file is open.
# check for lock file or passwd command in use if desired
{
if egrep &quot;^$user&quot; /etc/passwd > /dev/null 2>&1; then
ed /etc/shadow << EOF > /dev/null 2>&1
/^$user:/
i
$Shadow_Entry
.
/^$user:/
d
w
q
EOF

if [ $? = 0 ] ; then
echo &quot;Updated password for $user.&quot;
else
echo &quot;Update password failed for $user.&quot;
fi

else
echo &quot;The account $user does not exist.&quot;
fi
} > /tmp/.addpasswd.out
HERE
close(SCRIPT);

# Execute/remove the shell script and show results
system('sh /tmp/.addpasswd.sh');
sleep(2);
`rm /tmp/.addpasswd.sh`;
exec('cat /tmp/.addpasswd.out');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top