所以我想制作一个脚本,用于运行来自users.txt的用户
useradd -m -s /bin/false users_in_the_users.txt
并填写passwords.txt的密码两次(以确认密码)
这是脚本
#!/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
问题是,它没有填写密码。还有一件事,我希望脚本能够填写两次密码。
有什么建议吗?
答案 0 :(得分:3)
您必须在stdin上提供密码。替换:
passwd $iuser
使用:
passwd "$iuser" <<<"$ipasswd
$ipasswd"
或者,正如mklement0:
所建议的那样passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"
咒语<<<
会创建一个 here-string 。 <<<
之后的字符串作为标准提供给<<<
之前的命令。在这种情况下,我们提供了passwd
命令所需的密码的两个副本。
(该脚本从纯文本文件中读取这些密码。我将假设您的情况是一些特殊情况,并不像通常那样危险。)
答案 1 :(得分:2)
John1024's answer是正确的 - 他关于从纯文本文件中读取密码的警告值得重复。
让我在上下文中展示解决方案,没有文件描述符杂技(exec 3<
,...):
#!/bin/bash
# NOTE: Be sure to run this script with `sudo`.
# Read user and password
while read iuser ipasswd; 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.
# Password is passed via stdin, *twice* (for confirmation).
passwd $iuser <<< "$ipasswd"$'\n'"$ipasswd"
done < <(paste users.txt passwords.txt)
paste users.txt passwords.txt
从两个文件中读取对应的行,并将它们放在单行上,并以\t
分隔。<(...)
)传送到标准输入。read
从单个来源读取。$\n
是ANSI C-quoted string,可生成(字面)换行符。答案 2 :(得分:0)
#! /bin/bash
for i in {1..100}
do
`sudo mkdir -p /root/Desktop/userm$i`
`sudo useradd -m -d /root/Desktop/userm$i -s /bin/bash userm$i`
echo "userm$i:userm$i" | chpasswd
done
这将创建100个用户。
用户名将是(userm1-userm100)。
主目录将是/ root / Desktop /(userm1-user100)
密码将是(userm1-userm100)
答案 3 :(得分:0)
而不是使用这一行:
useradd -m -s /bin/false $iuser
试试这个:
useradd -m -s /bin/false -p $ipasswd $iuser
你实际上并不需要这个:
passwd $iuser <<< "$ipasswd"$'\n'"$ipasswd"
答案 4 :(得分:0)
以下是使用Python脚本的方法,您可以按如下方式运行:
muliUser.py
阅读vichhaiy的博客Create Multiple Users in Linux using Python script,详细了解user_password.txt
和concurrent
。
答案 5 :(得分:0)
请运行以下脚本。
#!/bin/bash
#purpose: bash script to create multiple users with pre-defined passwords at once.
#Read_Me: The import file should be in two columns, first users name and second passwords.
#author: Bablish Jaiswal
#contact: linux.cnf@gmail.com
read -p "Kindly import/type Users Name-password file with location:- " creation_info
cat $creation_info |while read i p
do
( useradd $i && echo -e "${p}\n${p}" | passwd $i ) > /dev/null 2>&1 && echo $user ${i} created and password is ${p} || echo ${i} failed
done