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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding User Script? 1

Status
Not open for further replies.

zeevam

Programmer
Feb 27, 2002
11
US
Is there a way to create a unix user/passwd script that reads from a flat file? I am unsure if it would be possible to add the passwd since you are prompted to add the new password and then confirming the new password. For example the flat text file would have a user and password column:
login1:password1
login2:password2
..etc

When executing the script it reads off that file and does a useradd login1 and passwd login1 for each entry. Any help on this would be greatly appreciated.
 
#!/bin/ksh

while IFS=":" read user pass junk
do
useradd <whatever parameters with ${user} and ${pass}>
done < myUserPassFile.txt vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Where does the passwd command get called?
 
I don't understand the question...
Pls, elaborate! vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I'm sorry, I am looking to have a flat file with usernames and passwords. Upon running a script we want to be able to add those usernames and password to our server. Basically we rebuilt our development server and want to create a user account for each user that is on our production box. Instead of manually doing this we want a script to be able to go through the flat file that contains the username and password information for each user. The useradd command is easy, but I am not sure how a unix script will handle the passwd command since it prompts the user to confirm their password.
 
hmmm.....
Maybe somebody esle could help you out.
You forgot to mention your OS.

Sorry
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
hi,

from the command useradd , i guess your running HP OS

#you can do something like this :-

in your list of usernames and passwords , encrypt your password , and then write the user string to the password file. This is quick guide , you'll have to do the checks
if user already exists . etc..

i.e.
lastid=$(grep -v '^#' /etc/passwd|grep -v &quot;^nobod&quot;|cut -f3 -d:|sort -n|tail -1) # get highest uid
uid=$(($lastid + 1)) # add 1 to uid this is one to use
today=$(date +%d-%m-%Y)
desc=&quot;user for Application&quot;
# if need you can specify the shell
# you can have one password for each user and then force the password change when the user logs in


PASSWORD=&quot;dummy&quot;
PW=$(/usr/bin/hgp/encrypt_password &quot;${PASSWORD}&quot;)

for USER in `cat filename | awk -F : '{print $1}'`
do USERN=&quot;$name:${PW}:$uid:100:$desc,$today:$home:/usr/bin/sh
&quot;
passwd -f -x 28 $user # force password to change on next
login
print $USERN >> /tmp/tempfile
done

# copy original password file

cat /tmp/tempfile >> /etc/passwd



if you want to set password for each user from your file you create a loop to encrypt each password and then use that to create the users

HTH
 
Or just automate it using expect.
Much simpler. If you want a code sample
write back and I'll post one.
 
Yes can you please post a code example. I am not familiar with expect. Thanks.
 
#!/usr/bin/expect


###########init#########################
set timeout 5000
#global secondary prompt
set secondp &quot;\[Rr\].*enter ne.*&quot;

if {![llength $argv]} {
error &quot;Fatal: Unable to read name of user.&quot;
}
if {[string compare $env(USER) root] != 0} {
puts &quot;Warning: You are not logged in as root and may have trouble changing passwords.&quot;
}

send_user &quot;Would you like to debug this change(y/n): &quot;
set ans [gets stdin]
if {&quot;$ans&quot; == &quot;y&quot;} {
exp_internal 1
}

#proc determines status of change prompt
proc final {line} {
global secondp
puts &quot;Comparing $line and $secondp&quot;
if {[regexp $secondp $line]} {
return &quot;1&quot;
}
return &quot;0&quot;
}

#############end init##########

spawn -noecho passwd [lindex $argv 0]
set id $spawn_id

expect {

-re &quot;(.*)passwor.*&quot; {
if {![final $expect_out(1,string)] == 1} {
puts &quot;final read [final $expect_out(1,string)].&quot;
send_user &quot;Password: &quot;
stty -echo
set pass [gets stdin]
send &quot;$pass\r&quot;
exp_continue
} else {
send &quot;$pass\r&quot;
expect -re &quot;.*assword change.*&quot; {
send_user &quot;Successful change for [lindex $argv 0].&quot;
catch {close ; wait}
stty echo
exit
}
}
}

eof {stty echo ; send_user &quot;Program exited::last read was $expect_out(buffer).\n&quot; }
timeout {stty echo ; send_user &quot;Proggy timed out, default time is $timeout.&quot; }
}

This is a partly automated version.
Make sure that tcl/expect is installed and then run it
against a test user.

For a fully automated version I would have to see the
file format of your flat file and then code another
procedure using code similar to that above for each user:
passwd pair. Not complicated.

If you want just let me know.



 
Why don't you just use useradd -p option? This allows you to specify a password when you are adding the user. You could read the list of usernames and passwords from the file and then do a &quot;useradd <user> -p <passwd>&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top