问题1:批量创建10个系统账号ucode01-ucode10并设置密码(密码不能相同)

#!/bin/bash
for n in $(seq -w 10)
do
      useradd ucode-$n
      echo "$n"|passwd --stdin ucode-$n
done

[root@localhost scripts]# sh adduser01.sh
Changing password for user ucode-01.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-02.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-03.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-04.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-05.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-06.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-07.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-08.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-09.
passwd: all authentication tokens updated successfully.
Changing password for user ucode-10.
passwd: all authentication tokens updated successfully.

[root@localhost scripts]# su - ucode01
su: user ucode01 does not exist
[root@localhost scripts]# su - ucode-01
[ucode-01@localhost ~]$ su - ucode-02
Password:
[ucode-02@localhost ~]$

问题2:批量创建10个系统账号ucode01-ucode10并设置密码(密码为随机8位字符串)

随机数变量$RANDOM和时间$(date +%t%N):

[root@localhost scripts]# echo $RANDOM
30942

[root@localhost scripts]# echo $RANDOM
21755
[root@localhost scripts]# echo $(date +%N)
361278349
[root@localhost scripts]# echo $(date +%t%N)
821466599
[root@localhost scripts]# echo $(date +%t%N)

将两个随机数组合

[root@localhost scripts]# echo $(date +%t%N)$RANDOM
73420786120340

将结果交给md5加密处理一下

[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum
eca0ba74cc88c3accd864250be70aa0a  -
[root@localhost scripts]#

取8位字符串

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

9202fb1d
[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
7e5247a9
[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
67d9b364
[root@localhost scripts]# echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9
808d5c06

编写脚本:

vim userdeluser02.sh   
#!/bin/bash
. /etc/init.d/functions
>/mnt/user.txt
for n in $(seq -w 10)
do
     passwd=`echo $(date +%t%N)$RANDOM|md5sum |cut -c 2-9`
     useradd ucode-$n >&/dev/null && user_status=$?
     echo "$passwd"|passwd --stdin ucode-$n >&/dev/null && passwd_status=$?
     if [ $user_status -eq 0 -a $passwd_status -eq 0 ];then
         action "adduser ucode-$n" /bin/true
         echo -e "user:\tucode-$n pass:\t$passwd" >>/mnt/user.txt
     else
         action "adduser ucode-$n" /bin/false
         echo -e "user:\tucode-$n pass:\t$passwd">>/mnt/fail_user.txt
     fi
done

[root@localhost scripts]# sh  userdeluser02.sh                             
adduser ucode-01                                        [  OK  ]
adduser ucode-02                                        [  OK  ]
adduser ucode-03                                        [  OK  ]
adduser ucode-04                                        [  OK  ]
adduser ucode-05                                        [  OK  ]
adduser ucode-06                                        [  OK  ]
adduser ucode-07                                        [  OK  ]
adduser ucode-08                                        [  OK  ]
adduser ucode-09                                        [  OK  ]
adduser ucode-10                                        [  OK  ]
[root@localhost scripts]# cat /mnt/user.txt
user:   ucode-01 pass: a3410e42
user:   ucode-02 pass: 0e38b5b2
user:   ucode-03 pass: 9512c1a0
user:   ucode-04 pass: 7fd7b5fe
user:   ucode-05 pass: 384f4c6c
user:   ucode-06 pass: 611525b8
user:   ucode-07 pass: cfc69fdb
user:   ucode-08 pass: ddddbbe9
user:   ucode-09 pass: aeb10ac0
user:   ucode-10 pass: 7f64bf48
[root@localhost scripts]# su - ucode-01
[ucode-01@localhost ~]$ su - ucode-02
Password:

拓展:linux生产系统产生随机数的6种方法

方法1:通过系统环境变量($RANDOM)

示例:

[root@localhost scripts]# echo $RANDOM
5016
[root@localhost scripts]#
[root@localhost scripts]# echo $RANDOM
14107
[root@localhost scripts]#

方法2:通过openssl产生随机数

示例:

[root@localhost scripts]# openssl rand -base64 8
9boFTwuEkRM=
[root@localhost scripts]# openssl rand -base64 10
vYsKdKydyVrSrw==
[root@localhost scripts]#

[root@localhost scripts]# openssl rand -base64 10|md5sum 
fe581a8bd8787fa6e85a7afac5f1a66b  -
[root@localhost scripts]#

方法3:通过时间获取随机数(date)

示例:

[root@localhost scripts]# date +%s%N
1437991244124034732
[root@localhost scripts]# date +%s%N
1437991246221217844
[root@localhost scripts]# date +%s%N%m
143799125181633289607
[root@localhost scripts]#

方法4:通过/dev/random设备来获取

说明:/dev/random 设备,存储着系统当前运行的环境实时数据。它可以看做是系统某个时候的唯一数据,因此可以用作随机数元数据。他们可以通过文件读取方式,读得里面数据。/dev/urandom这个设备数据与random里面一样。只是,它是非塞的随机数发生器。读取操作不会产生阻塞。

示例:

[root@localhost scripts]# head /dev/urandom |cksum
3711806028 1786
[root@localhost scripts]# head /dev/urandom |cksum
1097231128 1469
[root@localhost scripts]# head /dev/urandom |cksum
1494175762 2776
[root@localhost scripts]# head /dev/urandom |cksum
176794259 4099
[root@localhost scripts]#

[root@localhost scripts]# head /dev/urandom |cksum|md5sum

885e794c0ff6b821c7a94b6f4623ac76  -

方法5:利用UUID码来获取随机数

说明:UUID码全称是通用唯一识别码(Universally Unique Identifiter,UUID)它是一个软件构建的标准,亦为自由软件基金会(Open Software Foundation,OSF)的组织在分布式计算环境(Distributed Computing Enveronment)领域的一部分。

UUID的目的是让分布式系统中的所有元素都能有唯一的辨识信息,而不需要通过中央控制端来做辨识信息的指定,如此一来,每个人都可以建立不与其他人冲突的UUID。在这样的情况下,就不需要考虑数据创建 时的名称重复问题,它会让网络内任何一台计算机所生成的uuid码都是互联网整个服务器网络中是唯一的。它的原信息会加入硬件,时间,机器当前运行信息等。

UUID的格式是:包含32个16进位数字,以"-"连接号分为五段,形式为8-4-4-4-12的32个字符。范例:550e8400-e29b-41d4-a716-446655440000,所以:UUID理论上的总素为216 x 8 =2128,约等于3.4 x 1038,也就是说若每秒产生1兆个UUID,则要花100亿年才会将所有的UUID用完。

示例:

[root@localhost scripts]# cat /proc/sys/kernel/random/uuid
4e5118d3-e262-48d7-9e19-148b1c7da81f
[root@localhost scripts]# cat /proc/sys/kernel/random/uuid
c2fe4d92-55e7-45da-b4cf-c972483bed02
[root@localhost scripts]# cat /proc/sys/kernel/random/uuid
c002946e-db84-44a9-a170-1f285b3ff117
[root@localhost scripts]#

也可以用md5进行加密

[root@localhost scripts]# cat /proc/sys/kernel/random/uuid |md5sum
4d0e0f6799ad6c15d27bd17f1483bf58  -
[root@localhost scripts]#

方法6:通过expect获取随机数

[root@localhost scripts]# yum -y install expect

mkpasswd -l 8

[root@localhost scripts]# mkpasswd -l 8
qGQ4ee5#
[root@localhost scripts]# mkpasswd -l 8
4lmMx+V2
[root@localhost scripts]# mkpasswd -l 8
sL2jj3I\
[root@localhost scripts]# mkpasswd -l 8
HL9)o0rd
[root@localhost scripts]# mkpasswd -l 8
Nmi0hL2@
[root@localhost scripts]#

或者加参数 -s -l等

[root@localhost scripts]# mkpasswd  -s 0
VTk9gb3em
[root@localhost scripts]# mkpasswd  -s 0
0NQvx8ivg
[root@localhost scripts]# mkpasswd  -s 0
C6kUps5zf
[root@localhost scripts]# mkpasswd  -s 0
9rBbhQn9n
[root@localhost scripts]#

查看mkpasswd参数设置 mkpasswd命令的常用参数含义:

usage: mkpasswd [args] [user]

where arguments are:

-l #      (length of password, default = 7)

指定密码的长度,默认是7位数

-d #      (min # of digits, default = 2)

指定密码中数字最少位数,默认是2位

-c #      (min # of lowercase chars, default = 2)

指定密码中小写字母最少位数,默认是2位

-C #      (min # of uppercase chars, default = 2)

指定密码中大写字母最少位数,默认是2位

-s #      (min # of special chars, default = 1)

指定密码中特殊字符最少位数,默认是1位

-v        (verbose, show passwd interaction)

这个参数在实验的时候报错,具体不知道。

参数:

-l #      (密码的长度定义, 默认是 9)

-d #      (分位数, 默认是 16)

-c #      (小写字符, 默认是 3)

-C #      (大写字符, 默认是 2)

-s #      (特殊字符, 默认是  1)

-v        (详细。。。)

-p prog   (程序设置密码, 默认是 passwd)

代码示例:

#mkpasswd  -l 20 -d 5 -c 5 -C 5 -s 5 root

Z}K7hp0UPJ6v@&,c5{d3

批量生成用户脚本

#!/bin/bash

for n in $(seq -w 3)

do

useradd ucode-$n

passwd=`/usr/bin/mkpasswd -l 16 -c 3 -C 4 -d 4 -s 5`

echo $passwd|passwd --stdin ucode-$n

echo "$passwd:ucode-$n" >> /opt/userlist.txt

done

上面的随机数长短不一,如何统一格式化呢?答:使用md5sum 命令

[root@localhost scripts]# cat /proc/sys/kernel/random/uuid |md5sum |cut -c 1-9
873c50fe2
[root@localhost scripts]# head /dev/urandom |cksum |md5sum |cut -c 1-9
2a2b35b18
[root@localhost scripts]# date +%s%N|md5sum |cut -c 1-9
6687443ee
[root@localhost scripts]# openssl rand -base64 10|md5sum |cut -c 1-9
6d2d2618c
[root@localhost scripts]# echo $RANDOM|md5sum |cut -c 1-9
e7109ef67
[root@localhost scripts]#

[root@localhost scripts]# mkpasswd -s 0|md5sum |cut -c 1-9 
624d2bb67
[root@localhost scripts]#

自定义创建用户账户并生成密码:

[root@Python shell]# sh create.sh adminsys
Changing password for user adminsys.
passwd: all authentication tokens updated successfully.
[root@Python shell]# cat /opt/userlist.txt
+60j9Qv[TCb'<8I/:ricks
username:adminsys ## password:t6+&B)q29[WP9T[w
[root@Python shell]# cat  create.sh
#!/bin/bash
useradd $1
passwd=`/usr/bin/mkpasswd -l 16 -c 3 -C 4 -d 4 -s 5`
echo $passwd|passwd --stdin $1
echo "username:$1 ## password:$passwd" >> /opt/userlist.txt
[root@Python shell]#

转载于:https://blog.51cto.com/ucode/1760783

批量生成多个账户并设置密码相关推荐

  1. 利用脚本批量添加域用户账户

    以下内容摘自笔者编著的<网管员必读--网络管理>(第2版)一书: 2.4.6 利用脚本批量添加域用户账户  以上两种方法是比较常用的批量用户账户添加方法,但是不够灵活(不可以设置密码),而 ...

  2. 短链接API批量生成接口

    1,雨林短网址 网站链接:http://yldwz.cn 雨林短网址采用新浪.腾讯官方API接口,强大的多功能API,简单易用,质量高官 网提供强技术支持,99.9% SLA服务稳定安全可靠的校验机制 ...

  3. 免费ChatGPT自动批量生成文章工具

    要利用ChatGPT批量自动生成文章,最简单的方式就是找到一家接入了chatgpt或者文心一言的软件,[ChatGPT批量文章生成详细如下图]然后输入一些关键词和主题,即可生成文章或者解答你的问题,或 ...

  4. 利用新浪API批量生成t.cn 短链接的接口有哪些?

    新浪提供了长链接转为短链接的API,可以把长链接转为t.cn/xxx这种格式的短链接.短链接对于一些商家来说使用价值非常大,故整理了6个较为好用的t.cn短链接批量生成接口. 1.快鸟短网址 随着移动 ...

  5. 15款最好用的腾讯短链接url批量生成工具 - 值得收藏

    整理了15款国内最好用的腾讯短链接(url.cn)批量生成工具,拿走不谢! 1.青桃短链接 平台官网:http://qturl.cn 青桃短链接是一个老牌的第三方短链接服务平台了.对于其技术支持和服务 ...

  6. 生成批量缩率图_Windows系统实战之:批量生成某类型文件

    在我们的日常工作中,因工作需要批量生成一批文件,今天我就给大家介绍一下如何批量生成我们想要的文件,以文本文件为例. 需要用到的程序有: 1.Microsoft Excel(WPS表格也可以) 2.记事 ...

  7. 将表里的数据批量生成INSERT语句的存储过程 增强版

    原文:将表里的数据批量生成INSERT语句的存储过程 增强版 将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的 ...

  8. R语言使用fs包的path_wd函数基于自定义文件路径规则,批量生成多个文件或者文件夹对应的绝对(absolute)文件路径(constructs absolute path)

    R语言使用fs包的path_wd函数基于自定义文件路径规则,批量生成多个文件或者文件夹对应的绝对(absolute)文件路径(constructs an absolute path from the ...

  9. 将表里的数据批量生成INSERT语句的存储过程 继续增强版

    文章继续 桦仔兄的文章 将表里的数据批量生成INSERT语句的存储过程 增强版 继续增强... 本来打算将该内容回复于桦仔兄的文章的下面的,但是不知为何博客园就是不让提交!.... 所以在这里贴出来吧 ...

最新文章

  1. OpenCV2.4.9 For Android + Android Studio (with gradle)配置教程
  2. 汇编:ZF(zero flag)标志位
  3. (7) ebj学习: jpa 一对一,一对多,多对多
  4. 【T-SQL基础】02.联接查询
  5. 自助bi工具如何搭建数据可视化
  6. paip.PHP zend解密—以SHOPEX4.8.4为例
  7. 在LINUX上,Apache安装记
  8. python pywifi模块——暴力破解wifi
  9. 自实现Regsvr32注册dll功能
  10. HL7 2.6解析转XML(C#版)
  11. Redisson分布式锁学习总结:RedissonMultiLock 如何同时锁住N个资源
  12. 压缩包修改所属目录Linux,LINUX 压缩、解压、打包文件 修改文件所属组
  13. EXCEL实用技巧-多条件求和、多条件计数、多条件查找
  14. mysql on.000002_mysql | 同乐学堂
  15. 单点登录的三种实现方式
  16. google相机android10,三星S10+/S10/S10e谷歌相机移植版下载:支持夜视,体验强大算法...
  17. BICC呼叫建立过程
  18. cmd窗口执行cnpm报错记录:FullyQualifiedErrorId : UnauthorizedAccess或者因为在此系统上禁止运行脚本。有关详细信息,请参阅https。。
  19. VB.Net 解决winForm界面卡死
  20. 涨点利器:推荐系统中对双塔模型的各种改造升级(上)

热门文章

  1. AWK处理日志入门(转)
  2. windows 勾子简介
  3. 约瑟夫问题的数学方法
  4. C++11---之auto
  5. 文字加减前后缀lisp_日本搞笑艺人催泪讲授汉字课堂告诉你文字背后的意义!...
  6. 理光m2554进入维修_理光DX2432C,基士得耶6201供墨检测代码,看完马上解决代码故障...
  7. 计算机信息技术会考操作题,信息技术会考操作题整理.doc
  8. java 全选 反选取值_全选反选以及获取选中的数据
  9. android 分享qq微信朋友圈,H5微信JS-SDK实现分享朋友 朋友圈以及QQ自定义分享
  10. 引入react文件报错_React Native常见问题(一)