我可以用freebsd上的pw adduser自动创build密码的用户吗?

我可以用freebsd上的pw adduser自动创build密码的用户吗?

pw useradd [name] [switches] -V etcdir alternate /etc location -C config configuration file -q quiet operation Adding users: -n name login name -u uid user id -c comment user name/comment -d directory home directory -e date account expiry date -p date password expiry date -g grp initial group -G grp1,grp2 additional groups -m [ -k dir ] create and set up home -M mode home directory permissions -s shell name of login shell -o duplicate uid ok -L class user class -h fd read password on fd -H fd read encrypted password on fd -Y update NIS maps -N no update Setting defaults: -V etcdir alternate /etc location -D set user defaults -b dir default home root dir -e period default expiry period -p period default password change period -g group default group -G grp1,grp2 additional groups -L class default user class -k dir default home skeleton -M mode home directory permissions -u min,max set min,max uids -i min,max set min,max gids -w method set default password method -s shell default shell -y path set NIS passwd file path 

据此,我可以。 但我不知道如何…似乎我需要使用文件描述符,但我还没有能够看到如何做到这一点的任何示例。 我有一个bash脚本,需要自动做到这一点,没有用户input…

有任何想法吗?

这是一个使用文件描述符的例子:

 echo password | pw useradd -h 0 user1 

每个Unix进程通常有三个标准的文件描述符:

  • stdin (0)
  • stdout (1)
  • (2)

在这种情况下,我们告诉pw读取fd 0的input,也就是stdin 。 您可能想要查看bash手册页,其中有各种您可以对文件描述符和redirect进行操作的奇特事例。

请注意,此示例存在一些安全问题 – 任何人在正确的时间运行ps命令都将能够看到echo命令的参数。 这可能是也可能不是你的环境中的一个问题。 你可以这样做:

 pw useradd -h 0 user1 <<EOP password EOP 

您将需要确保您的FreeBSD系统安装了以下内容

bash(pkg_add -r bash)expect(pkg_add -r expect)

以下是一个脚本,它将自动生成随机密码并自动将其应用到root帐户。 你可以使这个启动脚本。 我会先制作下列目录。

  1. 从根目录不是root的家庭mkdir .script
  2. 有一个正常的用户帐户
  3. mkdir /var/log/.rec
  4. chown normaluser:normaluser /var/log/.rec
  5. 将passgen.sh脚本放入位于根文件结构中的.script文件中
  6. chmod 777 passgen.sh

    !在/ usr / local / bin目录/ bash的

    PASSGEN = head -c 10 /dev/random | uuencode -m - | tr -d '\n' | cut -c 19-32 head -c 10 /dev/random | uuencode -m - | tr -d '\n' | cut -c 19-32

echo $ PASSGEN> /var/log/.rec/encrypted

chown someuser:someuser /var/log/.rec/encrypted

chmod 666 /var/log/.rec/encrypted

encryption是混淆

LOG =根

PASS = $ PASSGEN

期待<< EOF

spawn passwd $ LOG

期待“新密码:”

发送“$ {PASS} \ r”

预计“重新input新密码:”

发送“$ {PASS} \ r”

期望eof;

EOF

现在把它添加到你的启动rc.local / rc.d / rc.conf,这取决于你的BSD版本。

希望这可以帮助那里的人,看起来这个编辑器是过滤一些哈希值和半报价。

谢谢Sean Hulbert