python中paramiko模块的使用
paramiko是python一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接
1、可以远程操作服务器文件
例如:
df:查看磁盘使用情况
mkdir:创建目录
mv/cp/mkdir/rmdir:对文件或目录进行操作
/sbin/service/ xxxservice start/stop/restart:启动、停止、重启某服务
netstat -ntl |grep 8080:查看8080端口的使用情况
或者 nc -zv localhost :查看所有端口的使用情况
find / -name XXX:查找某文件 等等
2、可以实现远程文件的上传,下载(类似于ssh的scp功能)
首先安装paramiko
pip install paramiko
但是安装paramiko需要先安装一个依赖包叫PyCrypto的模块。PyCrypto是python编写的加密工具包,支持的各种加密算法(主要有:MD2 128 bits;MD4 128 bits;MD5 128 bits;RIPEMD 160 bits;SHA1 160 bits;SHA256 256 bits;AES 16, 24, or 32 bytes/16 bytes;ARC2 Variable/8 bytes;Blowfish Variable/8 bytes;CAST Variable/8 bytes;DES 8 bytes/8 bytes ;DES3 (Triple DES) 16 bytes/8 bytes;IDEA 16 bytes/8 bytes ;RC5 Variable/8 bytes等等。)
具体实例代码如下:
1、实现简单的命令操作
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import paramiko 5 6 #创建ssh对象 7 ssh = paramiko.SSHClient() 8 9 #允许连接不在know_host中的主机 10 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 11 12 #连接服务器 13 ssh.connect(hostname='192.168.1.1',port=22,username='user',password='pwd') 14 15 #执行命令 16 stdin, stdout, stderr = ssh.exec_command('cd home;ls -l') 17 18 #执行结果 19 #result = stderr.read() #如果有错误则打印 20 result = stdout.read() 21 print result 22 #关闭连接 23 ssh.close()
2、实现远程上传
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:Eric.yue 4 5 import paramiko,os 6 7 class Paramiko_put(object): 8 def __init__(self,local_dir,remote_dir): 9 self.host = '192.168.1.1' 10 self.username = 'user' 11 self.passwd = 'pwd' 12 self.port = 22 13 self.local_dir = local_dir 14 self.remote_dir = remote_dir 15 self.tt = None 16 17 def pk_connect(self): 18 self.tt = paramiko.Transport((self.host, self.port)) 19 self.tt.connect(username = self.username, password = self.passwd) 20 try: 21 return paramiko.SFTPClient.from_transport(self.tt) 22 except Exception as e: 23 print 'Connect error:',e 24 exit() 25 26 def put_file(self): 27 sftp = self.pk_connect() 28 files = os.listdir(self.local_dir) 29 cnt = 0 30 for file in files: 31 sftp.put(os.path.join(self.local_dir, file), os.path.join(self.remote_dir, file)) 32 cnt += 1 33 34 if cnt == len(files): 35 print str(cnt) +' files put successful' 36 else: 37 print 'put failure' 38 39 def __del__(self): 40 self.tt.close() 41 42 pk = Paramiko_put('/home/mywork/test/day8','/home/mywork/test/day8') 43 pk.put_file()
3、实现远程下载
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author:Eric.yue 4 5 6 import paramiko,os 7 8 class Paramiko_get(object): 9 def __init__(self,remote_dir,local_dir): 10 self.host = '192.168.1.1' 11 self.username = 'user' 12 self.passwd = 'pwd' 13 self.port = 22 14 self.local_dir = local_dir 15 self.remote_dir = remote_dir 16 self.tt = None 17 18 def pk_connect(self): 19 self.tt = paramiko.Transport((self.host, self.port)) 20 self.tt.connect(username = self.username, password = self.passwd) 21 try: 22 return paramiko.SFTPClient.from_transport(self.tt) 23 except Exception as e: 24 print 'Connect error:',e 25 exit() 26 27 def get_file(self): 28 sftp = self.pk_connect() 29 files = sftp.listdir(self.remote_dir) 30 cnt = 0 31 for file in files: 32 sftp.get(os.path.join(self.remote_dir, file),os.path.join(self.local_dir, file)) 33 cnt += 1 34 35 if cnt == len(files): 36 print str(cnt) +' files get successful' 37 else: 38 print 'get failure' 39 40 def __del__(self): 41 self.tt.close() 42 43 pk = Paramiko_get('/home/inf/mywork/day8','/home/mywork/day8') 44 pk.get_file()
温馨提示:实现上传下载功能时要保证目录存在,有可操作权限哦!