如何在脚本里面批量添加用户和密码

如何在脚本里面批量添加用户和密码,第1张

#!/bin/shi=0whiletruedoleti=i+1echo"u"$i":123456">>/root/user.txtecho"createinguseru$i.."useraddu$iif[$i-eq100]then/usr/sbin/chpasswd/root/user.txt/bin/rm-f/root/user.txtexitfidone#ifyouwanttodeletealloftheuser,youcan#i=0#whiletrue#do#leti=i+1#echo"deleteinguseru$i.."#userdel-ru$i#if[$i-eq100]#then#exit#fi#done供参考

很高兴能回答你的问题,根据你的题意,给出如下回答:

配置单个用户密码

echo "PASSWORD_IN_PUT" | passwd --stdin USER

多个用户配置一个密码:

#!/bin/sh

FILE=/home/user.list    # 用户名保存文件

while read line

do

    echo "PASSWORD_IN_PUT" | passwd --stdin $line

done < $FILE

将如上内容保存到一个文件中,sh file 即可执行。

其中,FILE文件的格式为:

root

linux

test

ftp

# 等等,每个用户一行

多个用户配置多个密码:(必须有规律,没有规律无法用脚本自动执行)

#!/bin/sh

FILE=/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 line

do

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可以从标准输入或者管道接收密码。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11703995.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-18
下一篇2023-05-18

发表评论

登录后才能评论

评论列表(0条)

    保存