Shell development batch create Linux account and random 8-digit password

Requirements: batch create 10 system accounts, such as test-01 to test-10, and set the password to random 8 digits for each user. At the same time, save the password corresponding to the successfully created account in / tmp/user.txt, and save the password corresponding to the failed account in / tmp/fail_user.txt if it fails

1) create system accounts in batches. There is no special way to directly use useradd in combination with variables
2) the random 8-digit password needs to use the command to generate random numbers. In the demonstration script, the author uses the following three commands in combination, and the operation is as follows:

[root@jason scripts]# echo $(date +%t%N)$RANDOM|md5sum 
142470e2de821875c8e002ba0d2bf4e5  - 
Tip: the author uses date, system environment variable RANDOM and md5sum to generate RANDOM numbers

3) the password is required to be random 8-digit, but the former two commands combined with md5sum will generate a fixed random number of 128 bits. In this paper, the cut command is used to grab the random number of 8 bits. The operation is as follows:

[root@jason ~]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9

4) script display

[root@jason scripts]# cat 
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions || exit 1
for n in $(seq -w 10)
 passwd=`echo $(date +%s%n)$RANDOM|md5sum |cut -c 2-9`
 useradd test-$n >&/dev/null && user_status=$?
 echo "$passwd"|passwd --stdin test-$n >&/dev/null  && pass_status=$?
if [ $user_status -eq 0 -a $pass_status -eq 0 ];then
        action "adduser test-$n" /bin/true
        echo -e "user:\ttest-$n passwd:$passwd" >>/tmp/user.txt
        action "adduser test-$n" /bin/false
        echo -e "user:\ttest-$n passwd:$passwd" >>/tmp/fail_user.txt

5) script test

#Run script
[root@jason scripts]# sh     
adduser test-01                                            [  OK  ]
adduser test-02                                            [  OK  ]
adduser test-03                                            [  OK  ]
adduser test-04                                            [  OK  ]
adduser test-05                                            [  OK  ]
adduser test-06                                            [  OK  ]
adduser test-07                                            [  OK  ]
adduser test-08                                            [  OK  ]
adduser test-09                                            [  OK  ]
adduser test-10                                            [  OK  ]

#View the password corresponding to the successfully created user
[root@jason scripts]# cat /tmp/user.txt 
user:   test-01 passwd:cd2e36b9
user:   test-02 passwd:98ceb8f4
user:   test-03 passwd:813c5067
user:   test-04 passwd:20e3c58e
user:   test-05 passwd:ed0d93f9
user:   test-06 passwd:67aadb88
user:   test-07 passwd:f399ce9b
user:   test-08 passwd:f32be6f9
user:   test-09 passwd:bfa0cc7a
user:   test-10 passwd:25b6553e

#View the / etc/passwd file
[root@jason scripts]# tail -10 /etc/passwd

Keywords: Linux

Added by |Adam| on Sat, 14 Dec 2019 22:39:17 +0200