• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Script to add list of users from a file and assign password to it

Hi Folks,

I need your help in developing a Bash script which add a new usernames and password taken from a text file. Let me tell my requirement precisely.
I have 2 files in which the first one contains the usernames (filename: usernamefile) and the 2nd one consists of password details (filename: passwdfile) as shown below.
[root@ ]# cat usernamefile
user1  
user2  
user3  
user4  
....................
...................
so on.........
[root@ ]# cat passwdfile
uiyth8
tyhgy2
ljoiu4
jiut3s
........
........

Now i want to write a script which will take usernames from the first file and assign password to it picking up from the 2nd file.
I tried using the below script
awk '{print "echo \""$0"\" | useradd \""$0"\""}' usernamefile | bash.
It works fine for adding the username but i dont know how to execute the password command which requires input two times. Please help me.


Thanks,
Ashok
0
rdashokraj
Asked:
rdashokraj
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
TintinCommented:
echo pass | passwd --stdin
 
0
 
TintinCommented:
or to be more complete

#!/bin/bash

paste usernamefile passwdfile | while read user pass
do
  useradd $user
  echo $pass | passwd --stdin
done

0
 
TintinCommented:
Sorry,

echo $pass | passwd --stdin

should be

echo $pass | passwd --stdin $user
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ghostdog74Commented:
in awk

awk '
FNR==NR{ user[FNR]=$0;next}
{
     cmd = "useradd -p " $0 " " user[FNR]
     system(cmd)
}
' "fileuser" "filepasswd"
0
 
omarfaridCommented:
Hi All,

I think the provided solutions are for linux and not Solaris, since Solaris is not supporting the passwd options mentioned.

0
 
rdashokrajAuthor Commented:
Tintin, Thanks a lot for the solution, it works fine.

Ghostdog74, Your script works fine as well. But can't get the logic properly. Looks very complicated for me. Anyways it works. Thanks indeed.
0
 
rdashokrajAuthor Commented:
Ghostdog74, Sorry to say that your script doesn't work correctly. Its not executing the password command properly. Hence the creatation of user is half-complete. Given below is the portion of shadow file, in which your script just inserts the password in the 2nd column in unencrypted format.

[root@saic-india-lnx02 script]# tail /etc/shadow
user1:pass1:13800:0:99999:7:::
user2:pass2:13800:0:99999:7:::
user3:pass3:13800:0:99999:7:::
user4:pass4:13800:0:99999:7:::
user5:pass5:13800:0:99999:7:::
0
 
ghostdog74Commented:
I  am using Linux, here's mine.
 # cat /etc/shadow
user1:uiyth8:13801:0:99999:7:::
user2:tyhgy2:13801:0:99999:7:::
user3:ljoiu4:13801:0:99999:7:::
user4:jiut3s:13801:0:99999:7:::
 #                        

doesn't matter really as you have got your solution...cheers
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now