Mat*_*kin 7 automation scripting python fabric
chpasswd从面料脚本。另一种选择是在Fabric脚本中使用Pexpect。[xxx.xx.xx.xxx] run: echo "johnsmith:supersecretpassw0rd" | chpasswd。我已经使用Fabric创建了一个 Python 脚本来配置一个新构建的Slicehost Ubuntu 切片。如果您不熟悉 Fabric,它会使用Python SSH2 客户端Paramiko来提供“用于应用程序部署或系统管理任务”的远程访问。
我让Fabric脚本做的第一件事就是创建一个新的管理员用户并设置他们的密码。与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)
)
Run Code Online (Sandbox Code Playgroud)
$ 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 root@xxx.xx.xx.xxx... done.
Run Code Online (Sandbox Code Playgroud)
我通常对非交互式密码设置所做的事情是生成一个随机密码并将其设置为一个变量,然后将该变量传递给我的命令。我不必对密码有创意,也不必在纯文本文件中留下标准密码。
就安全性而言,我认为在自己的终端上打印密码并不是什么大问题(使用随机密码,无论如何您都需要它,这样您就可以记下创建的密码) 。
Fabric 对 ssh 的作用有什么不同吗?——无论如何,大多数通过 ssh 传输的内容都应该加密。
| 归档时间: |
|
| 查看次数: |
14406 次 |
| 最近记录: |