shell实现增加删除Linux系统用户脚本(密码为随机)
List \t\t create the userlist 这一步是必须执行的,脚本会识别本地当前目录下的文件
Useradd \t\t useradd the user
Passwd \t\t set the password to userlist
Del \t\t deleate the user 删除用户之前必须要有需要删除用户的列表文件
Sudoers \t\t set the Sudoers
Exit \t\t quit the shell
#!/bin/bash
########################################
#Author:qingbo.song #
#Date:2015.11. #
#E-mail:qingbo.song@apicloud.com #
#Comment: the system useradd #
#Path: /opt/shell/Linux_user_set.sh #
#Version:V 2.0 #
######################################## userfile="./userlist.txt"
Option_Echo()
{
echo -e "
######################################################
List \t\t create the userlist
Useradd \t\t useradd the user
Passwd \t\t set the password to userlist
Del \t\t deleate the user
Sudoers \t\t set the Sudoers
Exit \t\t quit the shell
######################################################"
} Welcome()
{
while [[ == ]]; do
echo "===============Welcome to use Linux_user_set.sh!==============="
echo "The shell to add the system user,and set the password to it! Please chose the option to run the program!"
#显示命令提示信息,调用Option_Echo函数
Option_Echo
#获取用户输入的信息
echo -n ":"
read option
if [[ -n "${option}" ]]; then #判断用户输入的信息不为空
if [[ "${option}" = "List" || "${option}" = "Useradd" || "${option}" = "Passwd" || "${option}" = "Del" || "${option}" = "Sudoers" || "${option}" = "Exit" ]]; then
if [[ "${option}" = "List" ]]; then
#创建用户列表函数调用
UserList
break
elif [[ "${option}" = "Useradd" ]]; then
#创建系统用户函数调用
UserAdd
break
elif [[ "${option}" = "Passwd" ]]; then
#设置用户密码函数调用
Passwd
break
elif [[ "${option}" = "Del" ]]; then
#sudoers函数调用
DelUser
break
elif [[ "${option}" = "Sudoers" ]]; then
Sudoers
break
elif [[ "${option}" = "Exit" ]]; then
#rm -fr ${userfile} #删除userlist.txt文件
echo "The shell is shutdown!"
rm -fr ${userfile}
exit
else
echo "Please input the ture option,Thank you!"
fi
else
echo "Please input the ture option,Thank you!"
fi
else
echo "The option is not NULL!Please input the ture option,Thank you!"
fi
done
} #创建用户列表
UserList()
{
echo "You will create a file of the user list,it's the ${userfile}!The format:"
echo -e "user01\nuser02"
echo "Please input the \"exit\" to quit"
#初始化userlist.txt
cat /dev/null > ${userfile}
while [[ $? == ]]; do
echo -n "Please input the user name:"
read name
if [[ -n "${name}" ]]; then #判断用户输入的信息是否非空
if [[ "${name}" = "exit" || "${name}" = "EXIT" || "${name}" = "Exit" ]]; then
Welcome
else
#将需要创建的用户添加到userlist.txt文件中
echo ${name} >> ${userfile}
echo "The user name \"${name}\" to save the file of ${userfile}!"
fi
else
echo "The name is not NULL!Please input the Ture user name,Thank you!!!"
fi
done
} #创建系统用户
UserAdd()
{
cat ${userfile} |grep -v "#"|while read LINE
do
echo "Create the user of ${LINE}!"
#-d 创建用户登录开始目录;-m 家目录不存在自动创建;-n 不创建相同名字的组;
#mkdir /users
useradd -d /home/${LINE} -m -n ${LINE} >> /dev/null >&
done Welcome
} #设置用户密码
Passwd()
{
ip=`ip addr|grep eth0|tail -|awk '{print $2}'|awk -F "/" '{print $1}'` #centos6.5获取
name=`hostname`
wdfile="./userpwdfile_${ip}_${name}.txt"
#重置user_password_list.txt
cat /dev/null > ${wdfile}
cat ${userfile} |grep -v "#"|while read LINE
do
echo "Produce the user of ${LINE} password:"
password=`</dev/urandom tr -dc '0123456789!@#$%{}<>=*&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | head -c24; echo ""`
echo "${LINE}:${password}" >> ${wdfile}
echo "${LINE}&&password save to the file ${wdfile}"
done
chpasswd < ${wdfile}
if [[ $? -eq ]]; then
echo "The user password to set success!"
else
echo "The user password to set ERROR!Please try again for yourself!"
fi
#返回Welcome界面
Welcome
} #批量删除user
DelUser()
{
echo "Now deleate the ${userfile} list of users:"
cat ${userfile} |grep -v "#"|while read LINE
do
echo "Deleate the user of ${LINE}!"
#-d 创建用户登录开始目录;-m 家目录不存在自动创建;-n 不创建相同名字的组;
userdel -r ${LINE} >> /dev/null >&
done Welcome
} #批量添加sudoers,请谨慎使用该功能
Sudoers()
{
echo "Now add the sudoers to /etc/sudoers:"
if [[ -f ${userfile} ]]; then
cat ${userfile}|grep -v "#"|while read LINE
do
echo "${LINE} ALL=(ALL) ALL" >> /etc/sudoers
echo "${LINE} 用户成功添加到sudoers中"
done
else
echo "${userfile}文件不存在!"
exit
fi Welcome
} #main 函数
Welcome
批量管理Linux系统用户
shell实现增加删除Linux系统用户脚本(密码为随机)的更多相关文章
- Linux系统用户/用户组/文件权限相关
目录一.Linux系统用户/用户组权限相关二.Linux系统文件权限相关 一.Linux系统用户/用户组权限相关 .命令:usermod 用法:usermod [-agGus] user args ‘ ...
- linux系统用户管理(一)
Linux系统用户管理1.用户管理*****2.Linux用户命令****3.用户创建的原理***4.密码管理***5.组命令管理**6.身份切换*****7.sudo提升权限***** 我们现在所使 ...
- 实验三 Linux系统用户管理及VIM配置
项目 内容 这个作业属于哪个课程 班级课程的主页链接 这个作业的要求在哪里 作业要求链接接地址 学号-姓名 17041428-朱槐健 作业学习目标 1.学习Linux系统用户管理 2.学习vim使用 ...
- 实验三:Linux系统用户管理及VIM配置
项目 内容 这个作业属于哪个课程 班级课程的主页链接 这个作业的要求在哪里 作业要求链接地址 学号-姓名 17043133-木腾飞 学习目标 1.学习Linux系统用户管理2.学习vim使用及配置 实 ...
- 重设windows10中的sub linux系统用户密码
原文:重设windows10中的sub linux系统用户密码 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/haiyoung/article/detai ...
- 双系统删除Linux系统
1.首先解决Linux的grub引导问题.电脑先安装了Windows10,然后又安装了Linux,grub直接覆盖了Windows的引导, 所以每次开机都是进入了Linux的grub引导,现在我们需要 ...
- Linux系统忘记管理员密码(CentOS、RHEL、Ubuntu)
Linux系统忘记管理员密码(CentOS.RHEL.Ubuntu) 系统使用过程中,尤其是生产环境中.万一忘记管理员密码,该怎么办?是不是很绝望? 1.RHEL 7.0 重启主机进入引导界面键入e键 ...
- 忘记Linux root用户的密码怎么办?
以前忘记windows密码的时候,要么用工具清除,要么重装系统.假如你忘记了linux系统的root密码,怎么办呢?是像windows一样用工具破解还是重装系统呢?哈哈,都不用.这方法很简单,现在做一 ...
- Linux 普通用户免密码切换到root用户
Linux 普通用户免密码切换到root用户 # 添加用户 useradd user_name # 修改密码 echo "user_name@pwd" | passwd --std ...
随机推荐
- [spark]-Spark2.x集群搭建与参数详解
在前面的Spark发展历程和基本概念中介绍了Spark的一些基本概念,熟悉了这些基本概念对于集群的搭建是很有必要的.我们可以了解到每个参数配置的作用是什么.这里将详细介绍Spark集群搭建以及xml参 ...
- PHP变量的传值和引用
问题: 1.PHP变量的存储.取值方式如何? 2.变量赋值时,普通传值和引用传值分别是什么意思?有何区别? 3.unset被赋值的变量会对两种赋值后原值和新值的影响? 变量的存储.取值形式: 变量 ...
- ROI POOLING 介绍
转自 https://blog.csdn.net/gbyy42299/article/details/80352418 Faster rcnn的整体构架: 训练的大致过程: 1.图片先缩放到MxN的尺 ...
- nc命令的常用参数介绍
nc命令的常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必做运维的应该在网络安全上都对一些开源软件都应该是相当的了解吧,比如tcpdump,namp等神奇,今天要给 ...
- 6.redis的分布式锁
https://www.cnblogs.com/linjiqin/p/8003838.html
- 2016/1/3 Python中的多线程(2):threading模块
之前提了Python多线程的一点使用,今天介绍更好的threading模块,它提供了Thread类和一些比较好用的同步机制. 先介绍Thread类 threading模块中的Thread类有很多thr ...
- mysql 距离函数
要有超级权限 SET GLOBAL log_bin_trust_function_creators = 1;DELIMITER $$CREATE DEFINER=`root`@`localhost` ...
- 【leetcode 简单】 第七十三题 丑数
编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true ...
- R的农场 chebnear
评测传送门 Description最近,R 终于获得了一片他梦寐以求的农场,但如此大的一片农场,想要做好防卫工作可不是一件容易的事.所以 R 购买了 N 个守卫,分别让他们站在一定的位置上(守卫不可移 ...
- 聊聊Java的final关键字
Java的final关键字在日常工作中经常会用到,比如定义常量的时候.如果是C++程序员出身的话,可能会类比C++语言中的define或者const关键字,但其实它们在语义上差距还是挺大的. 在Jav ...