Paramiko
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='peng', port=22, username='peng', key=private_key) ? # 執行命令 stdin, stdout, stderr = ssh.exec_command('df') # 獲取命令結果 result = stdout.read() ? # 關閉連接 ssh.close() |
SaltStack
1. 安裝和配置
master:
""" 1. 安裝salt-master ? ? yum install salt-master 2. 修改配置文件:/etc/salt/master ? ? interface: 0.0.0.0 ? ?# 表示Master的IP? 3. 啟動 ? ? service salt-master start """ |
minion:
""" 1. 安裝salt-minion ? ? yum install salt-minion 2. 修改配置文件 /etc/salt/minion ? ? master: 192.168.1.100 ? ? ? ? ?# master的地址 ? ? 或 ? ? master: ? ? ? ? - 192.168.1.4 ? ? ? ? - 192.168.1.5 ? ? random_master: True ? ? id: peng ? ? ? ? ? ? ? ?# 客戶端在salt-master中顯示的唯一ID 3. 啟動 ? ? service salt-minion start """ |
2. 授權
master:
""" 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進行遠程操作:
1)基于shell命令
salt 'peng' cmd.run ?'ifconfig'
2)基于salt API的方式
import salt.client
local = salt.client.LocalClient()
result = local.cmd('peng', 'cmd.run', ['ifconfig'])
轉載于:https://blog.51cto.com/pengai/1977740