Paramiko和Saltstack操作
Paramiko
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import paramiko private_key = paramiko.RSAKey.from_private_key_file('/home/auto/.ssh/id_rsa') # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='c1.salt.com', port=22, username='wupeiqi', key=private_key) # 执行命令 stdin, stdout, stderr = ssh.exec_command('df') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close()
SaltStack
1. 安装和配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
""" 1. 安装salt-master yum install salt-master 2. 修改配置文件:/etc/salt/master interface: 0.0.0.0 # 表示Master的IP 3. 启动 service salt-master start """
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
""" 1. 安装salt-minion yum install salt-minion 2. 修改配置文件 /etc/salt/minion master: 10.211.55.4 # master的地址 或 master: - 10.211.55.4 - 10.211.55.5 random_master: True id: c2.salt.com # 客户端在salt-master中显示的唯一ID 3. 启动 service salt-minion start """
2. 授权
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
""" salt-key -L # 查看已授权和未授权的slave salt-key -a salve_id # 接受指定id的salve salt-key -r salve_id # 拒绝指定id的salve salt-key -d salve_id # 删除指定id的salve """
3. 执行命令
在master服务器上对salve进行远程操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
salt 'c2.salt.com' cmd.run 'ifconfig'
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import salt.client local = salt.client.LocalClient() result = local.cmd('c2.salt.com', 'cmd.run', ['ifconfig'])