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()
  • 相关阅读:
    【小5聊】腾讯位置服务之小程序简单使用以及显示附近WC步行路线
    前端面试——JS进阶
    【小5聊】腾讯位置服务之小程序简单使用以及显示附近WC步行路线
    Node学习(三)
    HTML5 知识点总结(六)
    uniapp集成unipush监听推送,处理兼容ios在线无法收到推送通知问题以及安卓推送处理
    记一些CSS属性总结(二)
    Python getattr() 函数
    python中的__init__和__new__的区别
    干货报告:八大科技领域,280 页,InfoQ《2020 中国技术发展白皮书》开放下载...
  • 原文地址:https://www.cnblogs.com/autopenguin/p/6133017.html
Copyright © 2011-2022 走看看