当前位置: 首页 > news >正文

利用shell实现批量添加用户

 

批量添加用户并设置随机密码,把添加的用户的名字和密码保存到文件中。

[root@lamp scripts]# cat user.sh 
#!/bin/sh

for i in `seq -w 10`
do
  pass=$(echo "`date`$RANDOM"|md5sum|cut -c 1-8)         //取随机数的方法是date结果和random随机数拼接并计算md5值取前8位
  useradd kg$i
  echo "$pass"|passwd --stdin kg$i
  echo -e "kg$i\t$pass">>/tmp/pass.txt
done
[root@lamp scripts]# seq -w 10
01
02
03
04
05
06
07
08
09
10
[root@lamp scripts]# sh user.sh 
Changing password for user kg01.
passwd: all authentication tokens updated successfully.
Changing password for user kg02.
passwd: all authentication tokens updated successfully.
Changing password for user kg03.
passwd: all authentication tokens updated successfully.
Changing password for user kg04.
passwd: all authentication tokens updated successfully.
Changing password for user kg05.
passwd: all authentication tokens updated successfully.
Changing password for user kg06.
passwd: all authentication tokens updated successfully.
Changing password for user kg07.
passwd: all authentication tokens updated successfully.
Changing password for user kg08.
passwd: all authentication tokens updated successfully.
Changing password for user kg09.
passwd: all authentication tokens updated successfully.
Changing password for user kg10.
passwd: all authentication tokens updated successfully.
[root@lamp scripts]# cat /tmp/pass.txt 
kg01    3577b000
kg02    3ddefcfe
kg03    c1ec8c1e
kg04    0bdcdafc
kg05    f172c380
kg06    99471166
kg07    fbe82aeb
kg08    cad21c1c
kg09    5f7cdc56
kg10    b60958ad
[root@lamp scripts]# tail -10 /etc/passwd
kg01:x:504:504::/home/kg01:/bin/bash
kg02:x:505:505::/home/kg02:/bin/bash
kg03:x:506:506::/home/kg03:/bin/bash
kg04:x:507:507::/home/kg04:/bin/bash
kg05:x:508:508::/home/kg05:/bin/bash
kg06:x:509:509::/home/kg06:/bin/bash
kg07:x:510:510::/home/kg07:/bin/bash
kg08:x:511:511::/home/kg08:/bin/bash
kg09:x:512:512::/home/kg09:/bin/bash
kg10:x:513:513::/home/kg10:/bin/bash
[root@lamp scripts]# 

linux中取随机数的几种方法:

[root@lamp scripts]# echo $RANDOM
10474
[root@lamp scripts]# openssl rand -base64 8
C8ZBCmMhlKc=
[root@lamp scripts]# openssl rand -base64 6
BSaZWw8v
[root@lamp scripts]# openssl rand -base64 3
+S4K
[root@lamp scripts]# openssl rand -base64 4
daNeuw==
[root@lamp scripts]# date +%s%N
1448293383949936583
[root@lamp scripts]# date +%s%N
1448293385095579398
[root@lamp scripts]# date +%s%N
1448293386596694243
[root@lamp scripts]# head /dev/urandom|cksum
1336286598 1963
[root@lamp scripts]# head /dev/urandom|cksum
3913907857 2588
[root@lamp scripts]# head /dev/urandom|cksum
321627736 1498
[root@lamp scripts]# cat /proc/sys/kernel/random/uuid 
b8238fed-be13-47ac-8e30-b86ea0f822ff
[root@lamp scripts]# cat /proc/sys/kernel/random/uuid 
0bb0cb5d-157e-45f9-889a-0d6a93d3d328
[root@lamp scripts]# cat /proc/sys/kernel/random/uuid 
526c2cb3-a335-4013-bf71-108e7517c4d9
[root@lamp scripts]# 

 

转载于:https://www.cnblogs.com/along1226/p/4990136.html

相关文章:

  • 关机--小程序
  • Elaticsearch REST API常用技巧
  • eclipse控制台乱码的解决方法
  • QT信号槽机制
  • Nginx 配置详解
  • Monitor traffic to localhost from IE or .NET
  • scala可变长度参数(二)
  • js中replace用法
  • 【转载】强大的命令行工具wmic
  • kendo grid应用经验总结
  • SurfaceView和View区别
  • sql语句之一维二维
  • hdu 游乐场
  • dubbo+sping+zookeeper入门教程
  • struts国际化
  • [译]如何构建服务器端web组件,为何要构建?
  • 【css3】浏览器内核及其兼容性
  • Hibernate【inverse和cascade属性】知识要点
  • Laravel 菜鸟晋级之路
  • PHP的类修饰符与访问修饰符
  • Swift 中的尾递归和蹦床
  • Vue 重置组件到初始状态
  • Vue小说阅读器(仿追书神器)
  • Vultr 教程目录
  • 代理模式
  • 给初学者:JavaScript 中数组操作注意点
  • 计算机在识别图像时“看到”了什么?
  • 检测对象或数组
  • 力扣(LeetCode)357
  • 深度学习中的信息论知识详解
  • 我的面试准备过程--容器(更新中)
  • 我是如何设计 Upload 上传组件的
  • 小程序开发中的那些坑
  •  一套莫尔斯电报听写、翻译系统
  • 找一份好的前端工作,起点很重要
  • 智能合约开发环境搭建及Hello World合约
  • 国内开源镜像站点
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • # .NET Framework中使用命名管道进行进程间通信
  • (04)odoo视图操作
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (二)JAVA使用POI操作excel
  • (附源码)springboot 房产中介系统 毕业设计 312341
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (附源码)计算机毕业设计SSM基于java的云顶博客系统
  • (十六)串口UART
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NET/C# 使用反射调用含 ref 或 out 参数的方法
  • .netcore如何运行环境安装到Linux服务器
  • .Net程序帮助文档制作
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • @Query中countQuery的介绍
  • [\u4e00-\u9fa5] //匹配中文字符