# coding: utf-8 import paramiko import re import os from time import sleep # 定義一個類,表示一台遠端linux主機 # 參考https://www.cnblogs.com/haigege/p/5517422.html wyc class Linux(object): # 通過IP, 用戶名,密碼,超時時間初始化一個遠程Linux主機 def __init__(self, ip, username, password, timeout=3000): self.ip = ip self.username = username self.password = password self.timeout = timeout # transport和chanel self.t = '' self.chan = '' # 鏈接失敗的重試次數 self.try_times = 3 # 調用該方法連接遠程主機 def connect(self): while True: # 連接過程中可能會拋出異常,比如網絡不通、鏈接超時 try: self.t = paramiko.Transport(sock=(self.ip, 22)) self.t.connect(username=self.username, password=self.password) self.chan = self.t.open_session() self.chan.settimeout(self.timeout) self.chan.get_pty() self.chan.invoke_shell() # 如果沒有拋出異常說明連接成功,直接返回 print (u'連接%s成功' % self.ip) # 接收到的網絡數據解碼為str print (self.chan.recv(65535).decode('utf-8')) return # 這里不對可能的異常如socket.error, socket.timeout細化,直接一網打盡 except Exception as e1: if self.try_times != 0: print (u'連接%s失敗,進行重試' %self.ip) self.try_times -= 1 else: print (u'重試3次失敗,結束程序') exit(1) # 斷開連接 def close(self): self.chan.close() self.t.close() # 發送要執行的命令 def send(self, cmd): cmd += '\r' # 通過命令執行提示符來判斷命令是否執行完成 p = re.compile(r':~ #') result = '' # 發送要執行的命令 self.chan.send(cmd) # 回顯很長的命令可能執行較久,通過循環分批次取回回顯 while True: sleep(0.5) ret = self.chan.recv(65535) ret = ret.decode('utf-8') result += ret if p.search(ret): print (result) return result # ------獲取本地指定目錄及其子目錄下的所有文件------ def __get_all_files_in_local_dir(self, local_dir): # 保存所有文件的列表 all_files = list() # 獲取當前指定目錄下的所有目錄及文件,包含屬性值 files = os.listdir(local_dir) for x in files: # local_dir目錄中每一個文件或目錄的完整路徑 filename = os.path.join(local_dir, x) # 如果是目錄,則遞歸處理該目錄 if os.path.isdir(x): all_files.extend(self.__get_all_files_in_local_dir(filename)) else: all_files.append(filename) return all_files def sftp_put_dir(self, local_dir, remote_dir): t = paramiko.Transport(sock=(self.ip, 22)) t.connect(username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport(t) # 去掉路徑字符穿最后的字符'/',如果有的話 if remote_dir[-1] == '/': remote_dir = remote_dir[0:-1] # 獲取本地指定目錄及其子目錄下的所有文件 all_files = self.__get_all_files_in_local_dir(local_dir) # 依次put每一個文件 for x in all_files: filename = os.path.split(x)[-1] remote_filename = remote_dir + '/' + filename print (x) print (remote_filename) print (u'Put文件%s傳輸到%s中...' % (filename,self.ip)) sftp.put(x, remote_filename) if __name__ == '__main__': # host = Linux('172.16.10.40', 'root', 'imlytek!40') # host.connect() # host.send('ls -l') remote_path = r'/imlytek/smpt' local_path = r'D:\TSTP' # host.sftp_put_dir(local_path, remote_path) hostArray=[['172.16.10.40','root','iflstek!40'],['172.16.10.51','root','x1f@2013']] for x in hostArray: host = Linux(x[0], x[1], x[2]) host.sftp_put_dir(local_path, remote_path) #host.close()