1. 程式人生 > >Python實現分發資料塊到多臺伺服器上

Python實現分發資料塊到多臺伺服器上

程式碼如下:

# 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','[email protected]']] for x in hostArray: host = Linux(x[0], x[1], x[2]) host.sftp_put_dir(local_path, remote_path) #host.close()