通过读取用户和密码文件自动创建用户脚本
2017-03-09 22:26
686 查看
用户文件/mnt/username 密码文件/mnt/passwdfile
Username文件中每一行代表一个用户,passwdfile中每行对应username中一个密码
通过读取取两个文件,自动创建用户
#!/bin/bash
if [ -z /mnt/username ] //判断username是否存在
then echo ERROR:/mnt/username is not exits
elif [ -z /mnt/passwdfile ] //判断passwdfile是否存在
then echo ERROR:/mnt/passwdfile is not exits
else usernameline=`wc -l /mnt/username | cut -d " " -f 1` //统计行数
passwdfileline=`wc -l /mnt/passwdfile | cut -d " " -f 1`
if [ "$usernameline" = "0" -o "$passwdfileline" = "0" ]; //如果有任意一个文件行数为0则无法正常创建用户
then echo username or passwdfile is empty
elif [ "$usernameline" != "$passwdfileline" ]; //如果都不为空,则判断两个文件行数是否相等,如果不相等也不能创建用户
then echo usernameline and passwdfileline is not equal
else [ "$usernameline" = "$passwdfileline" ]; //如果行数相等,则依次提取每一行
for NUM in $( seq 1 $usernameline )
do
USERNAME=`sed -n ${NUM}p /mnt/username` //username的每一行作为USERNAMN
PASSWD=`sed -n ${NUM}p /mnt/passwdfile` //passwdfile的每一行作为对应密码
USERADD=`useradd $USERNAME &> /dev/null && echo "success"||echo "failed"` //创建用户成功则输出success 否则failed
if [ "$USERADD" = "success" ]; //如果输出success,说明创建用户成功,则为其添加密码
then echo $PASSWD | passwd --stdin $USERNAME
echo $USERNAME create successfully
elif [ "$USERADD" = "failed" ];
then echo $USERNAME already exits //否则提示该用户已经存在。
fi
done
fi
fi
Username文件中每一行代表一个用户,passwdfile中每行对应username中一个密码
通过读取取两个文件,自动创建用户
#!/bin/bash
if [ -z /mnt/username ] //判断username是否存在
then echo ERROR:/mnt/username is not exits
elif [ -z /mnt/passwdfile ] //判断passwdfile是否存在
then echo ERROR:/mnt/passwdfile is not exits
else usernameline=`wc -l /mnt/username | cut -d " " -f 1` //统计行数
passwdfileline=`wc -l /mnt/passwdfile | cut -d " " -f 1`
if [ "$usernameline" = "0" -o "$passwdfileline" = "0" ]; //如果有任意一个文件行数为0则无法正常创建用户
then echo username or passwdfile is empty
elif [ "$usernameline" != "$passwdfileline" ]; //如果都不为空,则判断两个文件行数是否相等,如果不相等也不能创建用户
then echo usernameline and passwdfileline is not equal
else [ "$usernameline" = "$passwdfileline" ]; //如果行数相等,则依次提取每一行
for NUM in $( seq 1 $usernameline )
do
USERNAME=`sed -n ${NUM}p /mnt/username` //username的每一行作为USERNAMN
PASSWD=`sed -n ${NUM}p /mnt/passwdfile` //passwdfile的每一行作为对应密码
USERADD=`useradd $USERNAME &> /dev/null && echo "success"||echo "failed"` //创建用户成功则输出success 否则failed
if [ "$USERADD" = "success" ]; //如果输出success,说明创建用户成功,则为其添加密码
then echo $PASSWD | passwd --stdin $USERNAME
echo $USERNAME create successfully
elif [ "$USERADD" = "failed" ];
then echo $USERNAME already exits //否则提示该用户已经存在。
fi
done
fi
fi
相关文章推荐
- VBS脚本批处理创建域用户【可自动设置用户密码,创建OU】[转]
- Shell脚本进阶篇——编写自动判断自动创建添加用户和用户密码的脚本
- shell script 通过文件创建用户及其组密码
- 【qtp脚本】实现自动创建目录并通过测试写字板生成指定个数的文件
- 通过linux的shell脚本批量创建用户名,用户名所属目录,设置对应密码
- 普通用户利用脚本及runas自动更改IP(无需输入密码)
- 如何在Linux下用shell自动创建用户并设置密码
- 通过FTP自动上传文件到目标机的shell脚本
- 通过创建用户profile文件, 限制用户对数据库资源使用
- 通过linux的shell脚本批量创建用户名,用户名所属目录,设置对应密码
- SilverLight浏览器交互之:SilverLight用户独立存储空间中文件的创建和读取
- 利用Hibernate工具,通过xml文件自动创建表
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库
- 通过Windows批处理脚本实现FTP的多文件自动上传
- C#中通过读取配置文件动态创建泛型对象
- shell脚本从网络获取文件实现自动输入密码
- MySQL命令行 不同端口登录 执行SQL文件 创建用户 赋予权限 修改root密码
- MySQL命令行 不同端口登录 执行SQL文件 创建用户 赋予权限 修改root密码
- 通过Windows批处理脚本实现FTP的多文件自动上传
- linux创建用户、修改密码(自动修改密码)