paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,fabric和ansible内部远程管理就是使用paramiko来实现。
#!/usr/bin/env python # coding=utf-8 import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='23.83.245.218', port=22, username='root', password='xiaoming.note5') stdin, stdout, stderr = ssh.exec_command('free -m') result = stdout.read() print(result.decode()) ssh.close()
#!/usr/bin/env python # coding=utf-8 import paramiko private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='23.83.245.218', port=22, username='root', pkey=private_key) stdin, stdout, stderr = ssh.exec_command('free -m') result = stdout.read() print(result.decode()) ssh.close()
#!/usr/bin/env python # coding=utf-8 import paramiko transport = paramiko.Transport(('23.83.245.218', 22)) transport.connect(username='root', password='xiaoming.note5') sftp = paramiko.SFTPClient.from_transport(transport) sftp.put('/etc/passwd', '/tmp/115.159pwd') sftp.get('/etc/passwd', '/tmp/23.83pwd') transport.close()
#!/usr/bin/env python # coding=utf-8 import paramiko private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') transport = paramiko.Transport(('23.83.245.218', 22)) transport.connect(username='root',pkey=private_key) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put('/etc/passwd', '/tmp/115.159pwd') sftp.get('/etc/passwd', '/tmp/23.83pwd') transport.close()
一个简易执行命令,上传下载文件的小程序
#!/usr/bin/env python # coding=utf-8 import sys import re import paramiko import threading class Hostmanage(object): def __init__(self,cmdtype, user, host, cmd, port=22): self.host = host self.username = user self.port = int(port) self.command = cmd self.cmdtype = cmdtype self.private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa') def run(self): if hasattr(self, cmdtype): getattr(self, cmdtype)() def cmd(self): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=self.host, port=self.port, username=self.username, pkey=self.private_key) stdin, stdout, stderr = ssh.exec_command(self.command) ret, err = stdout.read(), stderr.read() result = ret if ret else err print(self.host) print(result.decode()) ssh.close() def get(self): try: transport = paramiko.Transport((self.host, self.port)) transport.connect(username=self.username, pkey=self.private_key) sftp = paramiko.SFTPClient.from_transport(transport) sftp.get(self.command.split()[0], self.command.split()[1]) transport.close() print('%s 下载 %s 成功' % (self.host, self.command.split()[0])) except: print('%s 下载 %s 失败' % (self.host, self.command.split()[0])) def put(self): try: transport = paramiko.Transport((self.host, self.port)) transport.connect(username=self.username, pkey=self.private_key) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(self.command.split()[0], self.command.split()[1]) transport.close() print('%s 上传 %s 成功' % (self.host, self.command.split()[0])) except: print('%s 上传 %s 失败' % (self.host, self.command.split()[0])) def mythread(cmdtype, user, host, cmd, port=22): start = Hostmanage(cmdtype, user, host, cmd, port) m = threading.Thread(target=start.run) m.start() if __name__ == '__main__': roledefs = { 'www': ['root@23.83.245.218:22', 'root@115.159.193.70:22',], 'game': ['root@23.83.245.218:22',] } cmdtype = sys.argv[1] channel = sys.argv[2] cmd = sys.argv[3] if cmdtype == 'cmd': if channel in roledefs: # 有组名 for entry in roledefs[channel]: user, host, port = re.split('@|:', entry) mythread(cmdtype, user, host, cmd, port) else: if not re.search('(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}', channel): print('错误的主机名') else: try: user, host, port = re.split('@|:', channel) mythread(cmdtype, user, host, cmd, port) except ValueError as e: user, host = re.split('@|:', channel) mythread(cmdtype, user, host, cmd) elif cmdtype == 'put': if channel in roledefs: # 有组名 for entry in roledefs[channel]: user, host, port = re.split('@|:', entry) mythread(cmdtype, user, host, cmd, port) else: # 无组名 # 不是IP地址 if not re.search('(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}', channel): print('错误的主机名') else: try: # 有端口 user, host, port = re.split('@|:', channel) mythread(cmdtype, user, host, cmd, port) except ValueError as e: # 无端口 user, host = re.split('@|:', channel) mythread(cmdtype, user, host, cmd) elif cmdtype == 'get': # 只能获取单个主机的文件 if not re.search('(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}', channel): print('错误的主机名') else: try: user, host, port = re.split('@|:', channel) mythread(cmdtype, user, host, cmd, port) except ValueError as e: user, host = re.split('@|:', channel) mythread(cmdtype, user, host, cmd)
### 作者信息 姓名:hexm email:xiaoming.unix@gmail.com ### 实现功能 简版fabric实现 输入主机或者主机组 可以远程执行命令,上传下载单个文件。 ### 例子 上传/etc/passwd 文件到www主机组并保存为的/tmp/mypass # python3 fab.py put www "/etc/passwd /tmp/mypass" 115.159.193.70 上传 /etc/passwd 成功 23.83.245.218 上传 /etc/passwd 成功 把/etc/passwd上传到23.83.245.218的/tmp目录下并改名mypass # python3 fab.py put root@23.83.245.218 "/etc/passwd /tmp/mypass" 23.83.245.218 上传 /etc/passwd 成功 下载单个文件 # python3 fab.py get root@23.83.245.218 "/etc/passwd /tmp/mypass" 23.83.245.218 下载 /etc/passwd 成功 不能获取主机组的文件 # python3 fab.py get www "/etc/passwd /tmp/mypass" 错误的主机名 执行命令 # python3 fab.py cmd www "hostname" 115.159.193.70 VM_255_164_centos 23.83.245.218 localhost.localdomain