编写脚本以使用预定义的密码创建多个用户 [英] Write script to create multiple users with pre-defined passwords
问题描述
所以我想制作一个脚本,从运行的users.txt创建用户
So I would like to make a script that create users from users.txt running
useradd -m -s /bin/false users_in_the_users.txt
并两次输入passwords.txt中的密码(以确认密码)
and fill the password from passwords.txt twice (to confirm the passwords)
这是脚本
#!/bin/bash
# Assign file descriptors to users and passwords files
exec 3< users.txt
exec 4< passwords.txt
exec 5< passwords.txt
# Read user and password
while read iuser <&3 && read ipasswd <&4 ; do
# Just print this for debugging
printf "\tCreating user: %s with password: %s\n" $iuser $ipasswd
# Create the user with adduser (you can add whichever option you like)
useradd -m -s /bin/false $iuser
# Assign the password to the user, passwd must read it from stdin
passwd $iuser
done
问题是,它没有填写密码.还有一件事,我希望脚本两次填充密码.
The problem is, it does not fill the passwords. And 1 more thing, I want the script to fill the passwords twice.
有什么建议吗?
推荐答案
您必须在stdin上提供密码.替换:
You have to supply the password on stdin. Replace:
passwd $iuser
具有:
passwd "$iuser" <<<"$ipasswd
$ipasswd"
或者,如mklement0建议的那样:
or, as suggested by mklement0:
passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"
咒语<<<
创建一个 here-string .在<<<
之前的命令中,标准提供了<<<
之后的字符串.在这种情况下,我们提供 passwd
命令想要的密码的两个副本.
The incantation <<<
creates a here-string. The string that follows the <<<
is provided as standard in to the command which precedes the <<<
. In this case we provide the two copies of the password that the passwd
command wants.
(脚本从纯文本文件中读取这些密码.我认为您的情况是某些特殊情况,这种情况没有通常的危险.)
(The script reads these passwords from a plain text file. I will assume that your situation is some special case for which this is not as dangerous as it normally would be.)
这篇关于编写脚本以使用预定义的密码创建多个用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!