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()
溫馨提示:實現上傳下載功能時要保證目錄存在,有可操作權限哦!