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()
  • 相关阅读:
    NX 调试批处理文件
    NXOpen 更改UI对话框宽度
    NXOpen 遍历体 移动图层
    NXOpen 设置工作图层 一键开关图层
    NXOpen Block UI弹出另一个对话框实例
    NXOpen 座标UI获取 (原点 矩阵变换)
    [StackExchange]Redis 的几种类型的操作(string,hash,lists,set,sorted set)
    IIS 并发连接 设置与测试
    [StackExchage]Redis 的连接与操作(跨机器)
    Redis 事务
  • 原文地址:https://www.cnblogs.com/autopenguin/p/6133017.html
Copyright © 2011-2022 走看看