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='c1.salt.com', port=22, username='wupeiqi', key=private_key) # 执行命令 stdin, stdout, stderr = ssh.exec_command('df') # 获取命令结果 result = stdout.read() # 关闭连接 ssh.close()
StaltStack
1.安装和配置
""" 1. 安装salt-master yum install salt-master 2. 修改配置文件:/etc/salt/master interface: 0.0.0.0 # 表示Master的IP 3. 启动 service salt-master start """
1 """ 2 1. 安装salt-minion 3 yum install salt-minion 4 5 2. 修改配置文件 /etc/salt/minion 6 master: 10.211.55.4 # master的地址 7 或 8 master: 9 - 10.211.55.4 10 - 10.211.55.5 11 random_master: True 12 13 id: c2.salt.com # 客户端在salt-master中显示的唯一ID 14 3. 启动 15 service salt-minion start 16 """
2.授权
1 """ 2 salt-key -L # 查看已授权和未授权的slave 3 salt-key -a salve_id # 接受指定id的salve 4 salt-key -r salve_id # 拒绝指定id的salve 5 salt-key -d salve_id # 删除指定id的salve 6 """M
3.执行命令
在master服务器上对salve进行远程操作
salt 'c2.salt.com' cmd.run 'ifconfig'ji
import salt.client local = salt.client.LocalClient() result = local.cmd('c2.salt.com', 'cmd.run', ['ifconfig'])
#补充saltstack的安装
1.执行命令:rpm --import https://repo.saltstack.com/yum/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub 2.在/etc/yum.repos.d/这个目录下创建一个文件:saltstack.repo touch saltstack.repo 3.在文件中粘贴下面的代码: [saltstack-repo] name=SaltStack repo for RHEL/CentOS $releasever baseurl=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest enabled=1 gpgcheck=1 gpgkey=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/SALTSTACK-GPG-KEY.pub