Python提供了一个Paramiko模块,允许我们通过SSH 对远程系统进行操作,上传和下载文件。他的使用很直观,下面直接看例子。
例1
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author Yuan Li import paramiko # 创建SSH对象 ssh = paramiko.SSHClient() # 允许连接不在know_hosts文件中的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 连接服务器 ssh.connect(hostname='host', port=22, username='root', password='123') # 执行命令 stdin, stdout, stderr = ssh.exec_command('df -hT') # 获取命令结果 result = stdout.read() print(result.decode()) # 关闭连接 ssh.close() ---------- "C:\Program Files\Python3\python.exe" C:/Users/yli/pycharmprojects/Exercise/Week12/paramiko_test.py Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 28G 6.5G 22G 24% / devtmpfs devtmpfs 988M 0 988M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 112M 887M 12% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 169M 329M 34% /boot tmpfs tmpfs 200M 12K 200M 1% /run/user/42 tmpfs tmpfs 200M 0 200M 0% /run/user/0
如果看看ssh.connect()这个方法的源码,我们可以看见他实际上调用的是Transport这个类的方法。因此我们可以直接使用Transport来创建一个session,然后进行连接,效果和上面是一样的。
例2
import paramiko #创建一个transport session transport = paramiko.Transport(('sydnagios', 22)) #连接session transport.connect(username='root', password='Goat2015') ssh = paramiko.SSHClient() ssh._transport = transport #执行命令 stdin, stdout, stderr = ssh.exec_command('df') print (stdout.read().decode()) #创建sftp的对象 sftp=paramiko.SFTPClient.from_transport(transport) #上传 sftp.put('c:\\temp\\aaa.txt','/tmp/aaa.txt') #下载 sftp.get('/tmp/aaa.txt','c:\\temp\\bbb.txt') transport.close() ---------- "C:\Program Files\Python3\python.exe" C:/Users/yli/pycharmprojects/Exercise/Week12/paramiko_test.py Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 28G 6.5G 22G 24% / devtmpfs devtmpfs 988M 0 988M 0% /dev tmpfs tmpfs 998M 80K 998M 1% /dev/shm tmpfs tmpfs 998M 112M 887M 12% /run tmpfs tmpfs 998M 0 998M 0% /sys/fs/cgroup /dev/sda1 xfs 497M 169M 329M 34% /boot tmpfs tmpfs 200M 12K 200M 1% /run/user/42 tmpfs tmpfs 200M 0 200M 0% /run/user/0
例3,我们可以完善一下例2,把这么操作都封装到自定义的类里
import paramiko import uuid class SSHConnection(object): #初始化封装字段 def __init__(self, host='sydnagios', port=22, username='root',pwd='123'): self.host = host self.port = port self.username = username self.pwd = pwd self.__k = None #连接session,执行操作,断开session def run(self): self.connect() pass self.close() #连接 def connect(self): transport = paramiko.Transport((self.host,self.port)) transport.connect(username=self.username,password=self.pwd) self.__transport = transport #断开 def close(self): self.__transport.close() #执行命令 def cmd(self, command): ssh = paramiko.SSHClient() ssh._transport = self.__transport # 执行命令 stdin, stdout, stderr = ssh.exec_command(command) # 获取命令结果 result = stdout.read() return result def upload(self,local_path, target_path): # 连接,上传 sftp = paramiko.SFTPClient.from_transport(self.__transport) # 将location.py 上传至服务器 /tmp/test.py sftp.put(local_path, target_path) ssh = SSHConnection() ssh.connect() r1 = ssh.cmd('df') print(r1.decode()) ssh.upload('c:\\temp\\aaa.txt', "/home/yli/s7.py") ssh.close() ---------- "C:\Program Files\Python3\python.exe" C:/s13课上代码/s13day13课上代码/s13day13_课上代码/paramiko模块_demo.py Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/centos-root 28813572 6760732 22052840 24% / devtmpfs 1011616 0 1011616 0% /dev tmpfs 1021272 80 1021192 1% /dev/shm tmpfs 1021272 113928 907344 12% /run tmpfs 1021272 0 1021272 0% /sys/fs/cgroup /dev/sda1 508588 172604 335984 34% /boot tmpfs 204256 12 204244 1% /run/user/42 tmpfs 204256 0 204256 0% /run/user/0