麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯解決

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

這篇文章主要介紹了python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

import sysimport paramikoimport config_readerfrom check_utils import standout_print, parse_remainsize_response_lines, error_out_printfrom time import timeclass RemoteModel:  """ remote options model  execute remote command  """  def __init__(self, host, port=22):    self.hostname = host    self.port = port    self.username, self.password = self.load_conf()    self.s = None    self.session = None    self.init_conn()  def load_conf(self):    """      read config get the login info of remote host machine    :return:      login username and password of SSH login of this host    """    if self.hostname.find("10.179.1.110") != -1:      error_out_print("Error : the remote machine of KOR can not provide. please know")      sys.exit(-1)    username, password = config_reader.read_login_config(self.hostname)    if not username or not password:      error_out_print(        'Error: can not find ssh login info in this host[%s]. check need ' % self.hostname)      sys.exit(-1)    return username, password  def init_conn(self):    """      make a connection with the remote machine    :return:    """    try:      paramiko.util.log_to_file("paramiko_log.log")      self.s = paramiko.SSHClient()      self.s.set_missing_host_key_policy(paramiko.AutoAddPolicy())      self.s.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password)      standout_print('success connect the remote machine [host=%s]' % self.hostname)    except Exception, e:      standout_print(str(e))      standout_print(        'connect failed.in host[%s] user[%s] or pwd[%s] maybe wrong. ' % (          self.hostname, self.username, self.password))      sys.exit(-1)  def close(self):    """    close    if close can not use this connection    :return:    """    if self.s:      self.s.close()      self = None  def execute_command(self, command):    """    :param command:      execute cmd    :return:      the response lines    """    standout_print("Info: execute command [%s]" % command)    stdin, stdout, stderr = self.s.exec_command(command)    stdin.write("pwd"+"/n")    stdin.flush()    response_lines = stdout.readlines()    error_info = stderr.read()    if error_info and error_info.strip():      error_out_print(' remote command error info : %s' % stderr.read())      error_out_print(error_info)      return None    # info_arr = response_info.split('/n')    return response_lines  def remain_space_size(self, directory_path):    """    :param directory_path:    :return:      free size of the directory      unit size : MB    """    cmd = 'sudo df -m %s 1>&2' % directory_path # /usr/local/pgsql/data/ssd1    response_lines = self.execute_command(cmd)    # response_lines = self.execute_command_channel(cmd)    return parse_remainsize_response_lines(response_lines)  def execute(self, command, sudo=False):    feed_password = False    if sudo and self.username != "root":      command = "sudo %s" % command      feed_password = "pwd"    stdin, stdout, stderr = self.s.exec_command(command, get_pty=True)    if feed_password:      stdin.write(self.password + "/n")      stdin.flush()    return {'out': stdout.readlines(),        'err': stderr.readlines(),        'retval': stdout.channel.recv_exit_status()}if __name__ == '__main__':  host = ""  hostname = ""  command = "sudo df -m /data/pgsql94/data"  rm = RemoteModel(host=hostname)  print rm.execute_command(command)  # print rm.execute("df -m /data/pgsql94/data 1>&2", True)
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美日韩在线视频观看 | 欧美国产精品久久 | 日日草夜夜 | 曰批全过程120分钟免费69 | 国产精品一区久久久久 | 精品国产一区二区三区四区阿崩 | 欧美videofree性欧美另类 | 精品国产1区2区3区 免费国产 | 欧美1| 久久精品色 | 亚洲日本韩国在线观看 | 99成人精品视频 | 精品国产成人 | 日本精品久久久一区二区三区 | 亚洲网站免费看 | 久久午夜神器 | 日本精品二区 | 久草手机在线视频 | 国产精品1区,2区,3区 | 黄色网址你懂的 | 一区二区三区视频在线观看 | 久草在线视频中文 | 亚洲人成综合第一网 | 国产精品视频海角社区88 | 鲁丝片一区二区三区免费入口 | 日日夜av | 97人人草 | 九色激情网 | 精品一区二区三区毛片 | 多人乱大交xxxxx变态 | 欧美成人精品欧美一级乱黄 | 国产精品久久久久久影视 | 一区在线视频观看 | 成码无人av片在线观看网站 | 色婷婷久久久亚洲一区二区三区 | 免费一级a毛片免费观看 | 成年免费大片黄在线观看岛国 | 91重口视频 | 欧美日韩激情 | xxxⅹ96日本护士hd | 国产午夜电影在线观看 |