A friend asked me how to create users and set passwords in batches. I simply wrote two scripts for him to let him know and modify the scripts based on the actual situation, after all, the username and password he needs are in the actual production environment.
There are two cases: one is to test the for loop to add users in batches, and the other is to add users in batches according to the actual production environment. Write the following cases respectively:
1. for, add users of the specified type and set random passwords.
Script role: Batch add user1-10 users and set random 8-digit random passwords
#! /Bin/bash
# Test add user and set passwd
For n in 'seq 10'
Do
Useradd user $ n;
Echo user $ n | passwd 'echo user $ n | md5sum | cut-c1-8'
Done
2. Batch add users and set corresponding passwords in the actual production environment
Script: add specified users in batch in the production environment and set the corresponding password.
Train of Thought: put all the usernames and passwords to be added into an array, set a function to intercept the usernames and passwords, add users, and set passwords. Finally, you can use a loop to call this function.
#! /Bin/bash
# Adduser and set passwd
Userlist = (
User21: 123456
User22: 456789
User23: 876543
)
Function adduser_setpasswd (){
User = 'echo $1 | awk-F: '{print $1 }''
Passwd = 'echo $1 | awk-F: '{print $2 }''
Useradd-m-s/bin/bash $ user
Echo "$ user: $ passwd" | chpasswd
}
For n in $ {userlist [@]}
Do
Adduser_setpasswd $ n
Done