批量創建用戶添加隨機密碼

1、批量創建用戶user01-user10

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

 

2、-r和\1的分析

-r, --regexp-extended

        use extended regular expressions in the script

表示可以使用擴展的正則

 

\1正則中匹配第一個group,也就是匹配第一個()裏邊的內容

 

下面案例分析

#vi test.txt

 

sxz23749237492384

zxs379427493279

SXZ932574534

 

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

23749237492384

379427493279

932574534

 

因爲特殊字符添加了轉義字符,所以加上-r反而會出錯

#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

 

去掉轉義字符就可以

#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、爲用戶添加隨機密碼

3.1、隨機數

生成隨機數

RANDOM

 

exp:

輸出3到5個隨機數

echo $((RANDOM))

 

生成七位隨機字符爲字母加數字

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

7564fde

 

生成七位隨機字符爲字母數字加特殊字符

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

lAA'\H6z

 

3.2、方法一

使用七位隨機字符爲字母數字加特殊字符修改用戶密碼

#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、方法二:使用chpasswd批量修改密碼

chpasswd會從標準輸入批量讀取成對的用戶名和密碼,並使用這些信息來更新現有的一組

用戶

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

 

tr是把前面的輸出的空格替換爲換行

 

#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

 

測試是否成功

#su - user01

[user01@server ~]$ su - user02

Password: 

[user02@server ~]$ 

 

3.4、for循環命令實現用戶批量刪除

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

 

3.5、腳本實現用戶批量創建並修改密碼

#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(     z|

user10:'R/rW)w

 

參考:

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章