最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年10月21日 11:16:28   作者:百變小超  
這篇文章主要介紹了python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

import sys
import paramiko
import config_reader
from check_utils import standout_print, parse_remainsize_response_lines, error_out_print
from time import time


class 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)

報(bào)錯(cuò)1:

remote command error info : 
sudo: sorry, you must have a tty to run sudo

是由于

self.s.exec_command(command, get_pty=True)

沒(méi)有設(shè)置

get_pty=True

報(bào)錯(cuò)2:

會(huì)卡死在

stdout.readlines()

是由于 SSH在等待輸入用戶名的密碼

stdin.write("pwd"+"\n")
stdin.flush()

該種方式進(jìn)行交互,注意必須要換行"\n",和前面必須不能有空格等其他字符,確保密碼正確

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 升級(jí)python導(dǎo)致Yum崩潰的解決辦法

    升級(jí)python導(dǎo)致Yum崩潰的解決辦法

    這篇文章主要介紹了升級(jí)python導(dǎo)致Yum崩潰的三種解決辦法,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-11-11
  • python調(diào)用API實(shí)現(xiàn)智能回復(fù)機(jī)器人

    python調(diào)用API實(shí)現(xiàn)智能回復(fù)機(jī)器人

    這篇文章主要為大家詳細(xì)介紹了python調(diào)用API實(shí)現(xiàn)智能回復(fù)機(jī)器人,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 七個(gè)生態(tài)系統(tǒng)核心庫(kù)[python自學(xué)收藏]

    七個(gè)生態(tài)系統(tǒng)核心庫(kù)[python自學(xué)收藏]

    無(wú)論你是想快速入手Python,還是想成為數(shù)據(jù)分析大神或者機(jī)器學(xué)習(xí)大佬,亦或者對(duì)Python代碼進(jìn)行優(yōu)化,本文的python庫(kù)都能為你提供一些幫助
    2021-08-08
  • 使用Python和Scrapy實(shí)現(xiàn)抓取網(wǎng)站數(shù)據(jù)

    使用Python和Scrapy實(shí)現(xiàn)抓取網(wǎng)站數(shù)據(jù)

    Scrapy是一個(gè)功能強(qiáng)大的網(wǎng)絡(luò)爬蟲框架,允許開發(fā)者輕松地抓取和解析網(wǎng)站內(nèi)容,這篇文章主要為大家介紹了如何使用Python的Scrapy庫(kù)進(jìn)行網(wǎng)站數(shù)據(jù)抓取,需要的可以參考一下
    2023-05-05
  • Python根據(jù)文件名批量轉(zhuǎn)移圖片的方法

    Python根據(jù)文件名批量轉(zhuǎn)移圖片的方法

    今天小編就為大家分享一篇Python根據(jù)文件名批量轉(zhuǎn)移圖片的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 樹莓派4B安裝Tensorflow的方法步驟

    樹莓派4B安裝Tensorflow的方法步驟

    這篇文章主要介紹了樹莓派4B安裝Tensorflow的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • python手寫均值濾波

    python手寫均值濾波

    這篇文章主要為大家詳細(xì)介紹了python手寫均值濾波的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 使用Python和OpenCV檢測(cè)圖像中的物體并將物體裁剪下來(lái)

    使用Python和OpenCV檢測(cè)圖像中的物體并將物體裁剪下來(lái)

    這篇文章主要介紹了使用Python和OpenCV檢測(cè)圖像中的物體并將物體裁剪下來(lái),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 使用python-Jenkins批量創(chuàng)建及修改jobs操作

    使用python-Jenkins批量創(chuàng)建及修改jobs操作

    這篇文章主要介紹了使用python-Jenkins批量創(chuàng)建及修改jobs操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Python 中拼音庫(kù) PyPinyin 用法詳解

    Python 中拼音庫(kù) PyPinyin 用法詳解

    很多朋友問(wèn)小編怎樣把一批中文文件轉(zhuǎn)拼音命名呢?下面就讓我們來(lái)了解 Python 的一個(gè)庫(kù) PyPinyin 吧,感興趣的朋友跟隨小編一起看看吧
    2021-05-05

最新評(píng)論

丰都县| 宜良县| 阿鲁科尔沁旗| 阜城县| 兴宁市| 平利县| 金寨县| 遂川县| 宜黄县| 闸北区| 右玉县| 湟源县| 邢台县| 甘孜| 准格尔旗| 福海县| 瑞昌市| 兴海县| 电白县| 和顺县| 邛崃市| 增城市| 无极县| 嘉禾县| 济南市| 弥勒县| 临江市| 台山市| 沭阳县| 水城县| 方正县| 仁化县| 民丰县| 喀喇沁旗| 绥宁县| 靖远县| 铜陵市| 雷山县| 绥芬河市| 佛冈县| 美姑县|