加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 00:35:06 所属栏目:Linux 来源:网络整理
导读:所以我想创建一个脚本,用于运行users.txt中的用户 useradd -m -s /bin/false users_in_the_users.txt 并从passwords.txt填写密码两次(以确认密码) 这是脚本 #!/bin/bash# Assign file descriptors to users and passwords filesexec 3 users.txtexec 4 passw
所以我想创建一个脚本,用于运行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: %sn" $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

问题是,它没有填写密码.还有一件事,我希望脚本能够填写两次密码.

有什么建议?

解决方法

您必须在stdin上提供密码.更换:

passwd $iuser

有:

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

或者,如mklement0所示:

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

咒语<<<创建一个here-string. <<<<<<<<<作为标准提供给<<<<<<<<在这种情况下,我们提供passwd命令所需的密码的两个副本. (该脚本从纯文本文件中读取这些密码.我将假设您的情况是一些特殊情况,而这种情况并不像通常那样危险.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读