编写脚本以使用预定义密码创建多个用户

2024-04-16

所以我想制作一个从运行的 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

问题是,它没有填写密码。还有一件事,我希望脚本将密码填充两次。

有什么建议么?


您必须在标准输入上提供密码。代替:

passwd $iuser

with:

passwd "$iuser" <<<"$ipasswd
$ipasswd"

或者,按照 mklement0 的建议:

passwd "$iuser" <<<"$ipasswd"$'\n'"$ipasswd"

咒语<<<创建一个这里的字符串。后面的字符串<<<作为标准提供给前面的命令<<<。在这种情况下,我们提供了密码的两份副本passwd命令想要。

(该脚本从纯文本文件中读取这些密码。我假设您的情况是某种特殊情况,这种情况并不像通常那样危险。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

编写脚本以使用预定义密码创建多个用户 的相关文章

随机推荐