#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
原文地址:http://blog.51cto.com/xiaoxiaozhou/2108257