Create users in batches and add random passwords

1. Create users user01-user10 in batches

#seq -w 10|sed -r "s/(.*)/useradd user\1/g"|bash

 

2. Analysis of -r and \1

-r, --regexp-extended

        use extended regular expressions in the script

Indicates that extended regex can be used

 

\1 matches the first group in the regular, that is, matches the content in the first ()

 

The following case study

#vi test.txt

 

sxz23749237492384

zxs379427493279

SXZ932574534

 

#sed -n 's/^\([a-z A-Z]\{3\}\)\([0-9]\{3,\}\)/\2/p' test.txt

23749237492384

379427493279

932574534

 

Because special characters add escape characters, adding -r will cause an error instead

#sed -r -n's/^\([a-z A-Z]\{3\}\)\([0-9]\{3,\}\)/\2/p' test.txt

sed: -e expression #1, char 41: invalid reference \2 on `s' command's RHS

 

Just remove the escape characters

#sed -r -n 's/^([a-z A-Z]{3})([0-9]{2,})/\2/p' test.txt

23749237492384

379427493279

932574534

 

#tail -10 /etc/passwd

user01:x:5680:5680::/home/user01:/bin/bash

user02:x:5681:5681::/home/user02:/bin/bash

user03:x:5682:5682::/home/user03:/bin/bash

user04:x:5683:5683::/home/user04:/bin/bash

user05:x:5684:5684::/home/user05:/bin/bash

user06:x:5685:5685::/home/user06:/bin/bash

user07:x:5686:5686::/home/user07:/bin/bash

user08:x:5687:5687::/home/user08:/bin/bash

user09:x:5688:5688::/home/user09:/bin/bash

user10:x:5689:5689::/home/user10:/bin/bash

 

3. Add a random password for the user

3.1, random numbers

generate random numbers

RANDOM

 

exp:

Output 3 to 5 random numbers

echo $((RANDOM))

 

Generate seven random characters as letters and numbers

#cat /dev/urandom | head -1 | md5sum | head -c 7

7564fde

 

Generate seven random characters as alphanumeric plus special characters

#cat /dev/urandom | strings -n 7 | head -n 1

lAA'\H6z

 

3.2. Method 1

Modify user password using seven random characters, alphanumeric and special characters

#seq -w 10|sed -r 's#(.*)#key=`cat /dev/urandom|strings -n 7|head -n 1` ;echo $key|passwd --stdin user\1;echo "user\1:$key">> key.log#g'|bash

Changing password for user user01.

passwd: all authentication tokens updated successfully.

Changing password for user user02.

passwd: all authentication tokens updated successfully.

Changing password for user user03.

passwd: all authentication tokens updated successfully.

Changing password for user user04.

passwd: all authentication tokens updated successfully.

Changing password for user user05.

passwd: all authentication tokens updated successfully.

Changing password for user user06.

passwd: all authentication tokens updated successfully.

Changing password for user user07.

passwd: all authentication tokens updated successfully.

Changing password for user user08.

passwd: all authentication tokens updated successfully.

Changing password for user user09.

passwd: all authentication tokens updated successfully.

Changing password for user user10.

passwd: all authentication tokens updated successfully.

 

#cat key.log 

user01: N> E} V3'z

user02:-,8W@c

user03: 3 {.h: 7V

user04: [SKIB $ &

user05: \: IsW @ c

user06:h"wlAtW#

user07: Pgo: t \)

user08:H_Xtj[\

user09: g)T#\V<

user10: eKDRJ0 = $

 

3.3. Method 2: Use chpasswd to change passwords in batches

chpasswd will batch read username and password pairs from standard input and use this information to update an existing set of

user

#echo user{01..10}:$((RANDOM))|tr " " "\n" > key.log

 

tr is to replace the preceding output spaces with newlines

 

#cat key.log 

user01:12193

user02:32124

user03:26258

user04:4415

user05:24293

user06:10100

user07:13753

user08:3257

user09:24749

user10:15593

 

#chpasswd<key.log

 

Whether the test was successful

#su - user01

[user01@server ~]$ su - user02

Password: 

[user02@server ~]$ 

 

3.4. The for loop command realizes batch deletion of users

#for USER in `cut -d: -f 1 key.log`;do userdel -r $USER;done

 

3.5. Script to create and modify passwords in batches for users

#vi useradd.sh 

 

key=`cat /dev/urandom |strings -n 7 | head -n 1`

#!/bin/bash

#batch add users and passwd

 

for i in $(seq -w 10)

do

        useradd user$i

        key=`cat /dev/urandom |strings -n 7 | head -n 1`

        echo $key|passwd --stdin user$i

        echo "user$i:$key" >> key.log

done

 

#sh useradd.sh 

Changing password for user user01.

passwd: all authentication tokens updated successfully.

Changing password for user user02.

passwd: all authentication tokens updated successfully.

Changing password for user user03.

passwd: all authentication tokens updated successfully.

Changing password for user user04.

passwd: all authentication tokens updated successfully.

Changing password for user user05.

passwd: all authentication tokens updated successfully.

Changing password for user user06.

passwd: all authentication tokens updated successfully.

Changing password for user user07.

passwd: all authentication tokens updated successfully.

Changing password for user user08.

passwd: all authentication tokens updated successfully.

Changing password for user user09.

passwd: all authentication tokens updated successfully.

Changing password for user user10.

passwd: all authentication tokens updated successfully.

 

#cat key.log 

user01:@S_b~)(

user02: {WYci {) `

user03:yklE<&M

user04: O ~ I; q6k

user05: M*/X $ ioe;

user06:t$?|[aR_

user07:6`$chs=g>x

user08:5JGT4ydN+

user09:) FX (from |

user10:'R/rW)w

 

refer to:

http://blog.51cto.com/asmboy001/182290

http://bbs.chinaunix.net/thread-1387809-1-1.html

https://www.linuxidc.com/Linux/2015-08/122112.htm

http://blog.51cto.com/jackdady/1661781


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324975345&siteId=291194637
Recommended