zoukankan      html  css  js  c++  java
  • 基于 Paramiko 的 SSH 通讯类

    # -*- coding: UTF-8 -*-
    import paramiko
    import time

    ##################################################################
    '''类名称: SSHCommunication
    描述:SSH通讯类
    作者:Sid Zhang(autopenguin)'''
    ##################################################################
    class SSHCommunication():
    def __init__(self):
    self.client = paramiko.SSHClient()
    ##################################################################
    '''方法名称: Logon
    参数:HostIP->登录主机IP【字符串】
    SSHPort->SSH登录端口号【数字】
    Username->登录用户名【字符串】
    Password->登录密码【字符串】
    返回值:None
    描述:SSH登录方法'''
    ##################################################################
    def Logon(self, HostIP, SSHPort, Username, Password):
    self.client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
    self.client.connect(HostIP, SSHPort, Username, Password)
    self.chan = self.client.invoke_shell()
    self.chan.settimeout(120)
    ##################################################################
    '''函数名称: Send
    参数:Cmds->待发送的命令列表【字符串列表】
    返回值:None
    描述:发送命令方法'''
    ##################################################################
    def Send(self, Cmds = []):
    for Cmd in Cmds:
    while not self.chan.send_ready():
    time.sleep(0.1)
    self.chan.send(Cmd + ' ')
    print 'Execute Command: ' + Cmd
    ##################################################################
    '''函数名称: SendWithoutReceive
    参数:Cmds->待发送的命令列表【字符串列表】
    返回值:None
    描述:发送命令且无须接收结果方法'''
    #################################################################
    def SendWithoutReceive(self, Cmds=[]):
    self.Send(Cmds)
    self.Receive()
    ##################################################################
    '''函数名称: Receive
    参数:None
    返回值:命令执行输出
    描述:接收命令输出方法'''
    ##################################################################
    def Receive(self):
    while not self.chan.recv_ready():
    time.sleep(0.1)
    CommandOut = self.chan.recv(1024000)
    CommandOutAll = CommandOut
    while not (CommandOut.endswith('x1b[mx0f') or CommandOut.endswith('> ') or CommandOut.endswith('$') or CommandOut.endswith('# ')):
    while not self.chan.recv_ready():
    time.sleep(0.1)
    CommandOut = self.chan.recv(102400)
    CommandOutAll = CommandOutAll + CommandOut
    return CommandOutAll
    ##################################################################
    '''函数名称: Logout
    参数:None
    返回值:None
    描述:SSH登出方法'''
    ##################################################################
    def Logout(self):
    self.chan.close()
  • 相关阅读:
    易耗品管理 第三四表 查询的存储过程
    [zz]使用vc编译libsvm
    程序调试小bug
    Ubuntu下安装配置OpenNI, OpenCV
    关于Linux下使用OpenCv读取视频打不开的问题
    jQuery实现图片点击放大
    关于 QtDBus 的种种
    javascript计时器的实现
    [QT]没有选择Debug构建方式.为文件的某行设置断点可能会失败
    linux firefox 不显示英文的解决
  • 原文地址:https://www.cnblogs.com/autopenguin/p/6133017.html
Copyright © 2011-2022 走看看