
很高兴能回答你的问题,根据你的题意,给出如下回答:
echo "PASSWORD_IN_PUT" | passwd --stdin USER多个用户配置一个密码:
#!/bin/shFILE=/home/user.list # 用户名保存文件
while read line
do
echo "PASSWORD_IN_PUT" | passwd --stdin $line
done < $FILE
将如上内容保存到一个文件中,sh file 即可执行。
其中,FILE文件的格式为:
rootlinux
test
ftp
# 等等,每个用户一行
多个用户配置多个密码:(必须有规律,没有规律无法用脚本自动执行)
#!/bin/shFILE=/home/user.lis
for item in `cat $FILE`
do
case $item in
root)
# 用户为 root
echo "PASSWORD_root" | passwd --stdin $item
linux)
# 用户为 linux
echo "PASSWORD_linux" | passwd --stdin $item
*)
# 用户为其他
echo "PASSWORD_other" | passwd --stdin $item
esac
done
以上两个循环中,for、while 都可以使用和互换,只是写法不同,列出两种,供你参考。
case 语句用于进行多分支判断,你可以在 *) 这个默认语句前面再加入其他判断,格式参照上例即可。
归根结底,自动配置密码的核心主要是你会不会 passwd 命令中的 --stdin 选项。
while read linedo
user=`echo $line|cut -d : -f 1`
useradd $user
password = `echo $line | cut -d : -f 2`
echo "$password" | passwd --stdin "$user"
done<userlist.txt
Linux下 Passwd有参数 --stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.
--stdin参数表明passwd可以从标准输入或者管道接收密码。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)