国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Python > 正文

python3模擬實現(xiàn)xshell遠(yuǎn)程執(zhí)行l(wèi)iunx命令的方法

2024-09-09 19:02:19
字體:
供稿:網(wǎng)友

依賴包:pip install paramiko

源碼demo:

from time import *import paramiko# 定義一個類,表示一臺遠(yuǎn)端linux主機(jī)class Linux(object):  # 通過IP, 用戶名,密碼,超時時間初始化一個遠(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:      # 連接過程中可能會拋出異常,比如網(wǎng)絡(luò)不通、鏈接超時      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('連接%s成功' % self.ip)        # 接收到的網(wǎng)絡(luò)數(shù)據(jù)解碼為str        print(self.chan.recv(65535).decode('utf-8'))        return      # 這里不對可能的異常如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í)行較久,通過循環(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ā)送一個查看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)行代碼測試效果圖:

以上這篇python3模擬實現(xiàn)xshell遠(yuǎn)程執(zhí)行l(wèi)iunx命令的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)之家。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昭觉县| 安多县| 玛多县| 乌兰浩特市| 北票市| 永安市| 绥德县| 西安市| 驻马店市| 乐东| 岳西县| 巫山县| 湛江市| 金昌市| 巴青县| 平定县| 泰兴市| 盘山县| 高邑县| 隆林| 望城县| 禹城市| 鄂托克前旗| 安达市| 宽城| 陆河县| 白河县| 乌拉特前旗| 九龙县| 乌兰浩特市| 张家界市| 台东市| 永修县| 抚州市| 苍梧县| 鄂温| 名山县| 江门市| 保德县| 蚌埠市| 壶关县|