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
3e05cfe6

4) script display

[root@jason scripts]# cat adduser.sh 
#!/bin/sh
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions || exit 1
>/tmp/user.txt
for n in $(seq -w 10)
do
 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
 else
        action "adduser test-$n" /bin/false
        echo -e "user:\ttest-$n passwd:$passwd" >>/tmp/fail_user.txt
fi
done

5) script test

#Run script
[root@jason scripts]# sh adduser.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
test-01:x:533:533::/home/test-01:/bin/bash
test-02:x:534:534::/home/test-02:/bin/bash
test-03:x:535:535::/home/test-03:/bin/bash
test-04:x:536:536::/home/test-04:/bin/bash
test-05:x:537:537::/home/test-05:/bin/bash
test-06:x:538:538::/home/test-06:/bin/bash
test-07:x:539:539::/home/test-07:/bin/bash
test-08:x:540:540::/home/test-08:/bin/bash
test-09:x:541:541::/home/test-09:/bin/bash
test-10:x:542:542::/home/test-10:/bin/bash

Keywords: Linux

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