python3模擬實(shí)現(xiàn)xshell遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法
依賴包:pip install paramiko
源碼demo:
from time import * import paramiko # 定義一個(gè)類,表示一臺(tái)遠(yuǎn)端linux主機(jī) class Linux(object): # 通過(guò)IP, 用戶名,密碼,超時(shí)時(shí)間初始化一個(gè)遠(yuǎn)程Linux主機(jī) def __init__(self, ip, username, password, timeout=30): self.ip = ip self.username = username self.password = password self.timeout = timeout # transport和chanel self.t = '' self.chan = '' # 鏈接失敗的重試次數(shù) self.try_times = 3 # 調(diào)用該方法連接遠(yuǎn)程主機(jī) def connect(self): while True: # 連接過(guò)程中可能會(huì)拋出異常,比如網(wǎng)絡(luò)不通、鏈接超時(shí) 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() # 如果沒有拋出異常說(shuō)明連接成功,直接返回 print('連接%s成功' % self.ip) # 接收到的網(wǎng)絡(luò)數(shù)據(jù)解碼為str print(self.chan.recv(65535).decode('utf-8')) return # 這里不對(duì)可能的異常如socket.error, socket.timeout細(xì)化,直接一網(wǎng)打盡 except Exception as e1: if self.try_times != 0: print('連接%s失敗,進(jìn)行重試' % self.ip) self.try_times -= 1 else: print('重試3次失敗,結(jié)束程序') exit(1) # 斷開連接 def close(self): self.chan.close() self.t.close() # 發(fā)送要執(zhí)行的命令 def send(self, cmd): cmd += '\r' result = '' # 發(fā)送要執(zhí)行的命令 self.chan.send(cmd) # 回顯很長(zhǎng)的命令可能執(zhí)行較久,通過(guò)循環(huán)分批次取回回顯,執(zhí)行成功返回true,失敗返回false while True: sleep(0.5) ret = self.chan.recv(65535) ret = ret.decode('utf-8') result += ret return result ''' 發(fā)送文件 @:param upload_files上傳文件路徑 例如:/tmp/test.py @:param upload_path 上傳到目標(biāo)路徑 例如:/tmp/test_new.py ''' def upload_file(self,upload_files,upload_path): try: tran=paramiko.Transport(sock=(self.ip, self.port)) tran.connect(username=self.username, password=self.password) sftp = paramiko.SFTPClient.from_transport(tran) result=sftp.put(upload_files, upload_path) return True if result else False except Exception as ex: print(ex) tran.close() finally: tran.close() # 連接正常的情況 if __name__ == '__main__': host = Linux('192.168.16.57', 'root', '+B*A15*EFpKG') # 傳入Ip,用戶名,密碼 host.connect() # result = host.send('ls') # 發(fā)送一個(gè)查看ip的命令 def input_cmd(str): return input(str) tishi_msg="輸入命令:" while True: msg=input(tishi_msg) if msg=="exit": host.close() break else: res=host.send(msg) data=res.replace(res.split("\n")[-1],"") tishi_msg=res.split("\n")[-1] print(res.split("\n")[-1] + data.strip("\n"))
運(yùn)行代碼測(cè)試效果圖:
以上這篇python3模擬實(shí)現(xiàn)xshell遠(yuǎn)程執(zhí)行l(wèi)inux命令的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python人工智能tensorflow常用激活函數(shù)Activation?Functions
這篇文章主要為大家介紹了python人工智能tensorflow常用激活函數(shù)Activation?Functions的匯總介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python運(yùn)行錯(cuò)誤異常代碼含義對(duì)照表
這篇文章主要介紹了Python運(yùn)行錯(cuò)誤異常代碼含義對(duì)照表,需要的朋友可以參考下2021-04-04pyqt實(shí)現(xiàn).ui文件批量轉(zhuǎn)換為對(duì)應(yīng).py文件腳本
今天小編就為大家分享一篇pyqt實(shí)現(xiàn).ui文件批量轉(zhuǎn)換為對(duì)應(yīng).py文件腳本,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06windows下python模擬鼠標(biāo)點(diǎn)擊和鍵盤輸示例
這篇文章主要介紹了windows下python模擬鼠標(biāo)點(diǎn)擊和鍵盤輸示例,需要的朋友可以參考下2014-02-02Python使用ntplib庫(kù)同步校準(zhǔn)當(dāng)?shù)貢r(shí)間的方法
NTP網(wǎng)絡(luò)時(shí)間協(xié)議其實(shí)大家平時(shí)或多或少都能接觸到,包括Windows在內(nèi)的操作系統(tǒng)中的很多Internet時(shí)間同步功能都是在NTP的基礎(chǔ)上來(lái)做,這里我們來(lái)看一下Python使用ntplib庫(kù)同步校準(zhǔn)當(dāng)?shù)貢r(shí)間的方法2016-07-07詳解python執(zhí)行shell腳本創(chuàng)建用戶及相關(guān)操作
這篇文章主要介紹了python執(zhí)行shell腳本創(chuàng)建用戶及相關(guān)操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Pytorch 搭建分類回歸神經(jīng)網(wǎng)絡(luò)并用GPU進(jìn)行加速的例子
今天小編就為大家分享一篇Pytorch 搭建分類回歸神經(jīng)網(wǎng)絡(luò)并用GPU進(jìn)行加速的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01