chpasswd 。 另一个select是从Fabric脚本中使用Pexpect 。 [xxx.xx.xx.xxx] run: echo "johnsmith:supersecretpassw0rd" | chpasswd [xxx.xx.xx.xxx] run: echo "johnsmith:supersecretpassw0rd" | chpasswd 。 我已经使用Fabric创build了一个Python脚本来configuration新build的Slicehost Ubuntu切片。 如果您不熟悉Fabric,则使用Python SSH2客户端Paramiko为应用程序部署或系统pipe理任务提供远程访问。
我使用Fabric脚本的第一件事就是创build一个新的pipe理员用户并设置他们的密码。 与Pexpect不同,Fabric无法处理远程系统上的交互式命令,所以我需要非交互式地设置用户密码。 目前,我正在使用chpasswd命令,它将用户名和密码读取为明文。
# Fabric imports and host configuration excluded for brevity root_password = getpass.getpass("Root's password given by SliceManager: ") admin_username = prompt("Enter a username for the admin user to create: ") admin_password = getpass.getpass("Enter a password for the admin user: ") env.user = 'root' env.password = root_password # Create the admin group and add it to the sudoers file admin_group = 'admin' run('addgroup {group}'.format(group=admin_group)) run('echo "%{group} ALL=(ALL) ALL" >> /etc/sudoers'.format( group=admin_group) ) # Create the new admin user (default group=username); add to admin group run('adduser {username} --disabled-password --gecos ""'.format( username=admin_username) ) run('adduser {username} {group}'.format( username=admin_username, group=admin_group) ) # Set the password for the new admin user run('echo "{username}:{password}" | chpasswd'.format( username=admin_username, password=admin_password) )
$ fab config_rebuilt_slice Root's password given by SliceManager: Enter a username for the admin user to create: johnsmith Enter a password for the admin user: [xxx.xx.xx.xxx] run: addgroup admin [xxx.xx.xx.xxx] out: Adding group `admin' (GID 1000) ... [xxx.xx.xx.xxx] out: Done. [xxx.xx.xx.xxx] run: echo "%admin ALL=(ALL) ALL" >> /etc/sudoers [xxx.xx.xx.xxx] run: adduser johnsmith --disabled-password --gecos "" [xxx.xx.xx.xxx] out: Adding user `johnsmith' ... [xxx.xx.xx.xxx] out: Adding new group `johnsmith' (1001) ... [xxx.xx.xx.xxx] out: Adding new user `johnsmith' (1000) with group `johnsmith' ... [xxx.xx.xx.xxx] out: Creating home directory `/home/johnsmith' ... [xxx.xx.xx.xxx] out: Copying files from `/etc/skel' ... [xxx.xx.xx.xxx] run: adduser johnsmith admin [xxx.xx.xx.xxx] out: Adding user `johnsmith' to group `admin' ... [xxx.xx.xx.xxx] out: Adding user johnsmith to group admin [xxx.xx.xx.xxx] out: Done. [xxx.xx.xx.xxx] run: echo "johnsmith:supersecretpassw0rd" | chpasswd [xxx.xx.xx.xxx] run: passwd --lock root [xxx.xx.xx.xxx] out: passwd: password expiry information changed. Done. Disconnecting from [email protected]... done.
您在执行的命令行中传递密码。 这些可能不仅在terminal中可见,而且可能由其他用户发出'ps'命令(可能取决于系统configuration)。 密码是纯文本。
我不知道Fabric,但是如果它可以通过stdin / stdout(如subprocess.Popen() )与执行的命令进行通信,那么使用它可能是一个更好的select,而不是'echo username:密码| …“。
另外,你可以select在你的Python脚本中创build密码哈希(使用crypt模块和“$ 1 $ XXXXXXXX $”(“X”是随机字符)salt)并将其传递给chpasswd -e 。 纯文本密码将永远不会显示或logging。
我通常为非交互式密码设置做的是我生成一个随机密码,并将其设置为一个variables,然后将该variables传递给我的命令。 我不需要对密码进行创意,也不必在纯文本文件中留下标准密码。
这是一个用于生成随机密码的活动状态配方 。
就安全性而言,我不觉得在自己的terminal上打印密码是一个很大的问题(使用随机密码,无论如何你都要这么做,所以你可以记下创build的密码) 。
Fabric与ssh有什么不同? – 通过ssh旅行的大多数东西都应该被encryption。
如果您在fabric中使用chpasswd,请确保不要使用命令行中的纯文本密码。 这不仅仅是输出密码的标准输出,也会被写入/ var / log / secure。 在织物,你可以使用
with hide('running', 'output', 'error'): <your task here>
并确保您使用散列string与chpasswd命令,以便即使在安全日志中,您将不会看到以纯文本密码。 首先决定要使用的密码,并生成散列string。 你可以使用openssl。
openssl passwd -1
这会提示您input密码并生成MD5哈希。 你可以在脚本中使用它,如下所示。
echo "<user>:<MD5-hash>" | chpasswd -e
上述命令中的-e标志告诉chpasswd密码被encryption。
Fabric即将发布的1.0版本(现在可以从git分支中获得)将允许与远程端进行交互式会话 。
你可以自动configuration你的新片,然后login并立即更改密码? 有时最简单的方法是最好的。
但是,也可以在本地生成encryption密码,然后继续将encryption密码写入/ etc / shadow文件。 这样你就不用纯文本了。
请注意,初次提供初始密码时,您必须信任本地系统。
顺便说一句,你可以做一个你的自动configuration可用的副本,所以我用它而不是从头开始写所有东西? 目前我正在通过bash做所有事情。
像这样的东西?
sed 's/^root:.*$/root:$PASSWD:13063::::::/' /etc/shadow > /etc/shadow