这是我的测试 bash 脚本。我无法让它工作。我看到两个错误:
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
这是我的脚本:
#!/bin/bash
sudo adduser myuser << ENDX
password
password
First Last
Y
ENDX
exit 0
Run Code Online (Sandbox Code Playgroud)
这是输出:
me@mycomputer$ ./adduser.sh
Adding user `myuser' ...
Adding new group `myuser' (1001) ...
Adding new user `myuser' (1001) with group `myuser' ...
Creating home directory `/home/myuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: Retype new …
Run Code Online (Sandbox Code Playgroud) 我找到了一个指南,解释了如何设置用户密码。我正在尝试使其自动化并向用户发送电子邮件,例如:
userid created with password XYZ.
request to change the initial password.
Run Code Online (Sandbox Code Playgroud)
根据上面的文档,需要使用 Python 创建一个加密的密码并将其输入到usermod
命令中,如下所示:
usermod -p "<encrypted-password>" <username>
Run Code Online (Sandbox Code Playgroud)
有没有其他更简单的方法来做到这一点?我不想下载任何特殊的实用程序来做到这一点;它应该尽可能地概括。
编辑:即使上面链接中给出的方法对我来说似乎也不起作用:
bash-3.00# python
Python 2.4.6 (#1, Dec 13 2009, 23:43:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt; print crypt.crypt("<password>","<salt>")
<sOMrcxm7pCPI
>>> ^D
bash-3.00# useradd -g other -p "sOMrcxm7pCPI" -G bin,sys -m -s /usr/bin/bash mukesh2
UX: useradd: ERROR: project sOMrcxm7pCPI does not exist. Choose another.
UX: useradd: sOMrcxm7pCPI …
Run Code Online (Sandbox Code Playgroud)