使用Python登录网关设备执行命令

发布时间:2020-06-12 12:54:07编辑:admin阅读(2373)

    一、概述

    最近发现办公室有一个感测器网关,偶尔会掉线,重启之后,就正常了。

    手动操作过程如下:

    # telnet 10.212.82.90
    Trying 10.212.82.90...
    Connected to 10.212.82.90.
    Escape character is '^]'.
    (none) login: root
    Password: 
    Please make sure that what you're doing!  If you aren't administrator, please Don't do anything! :)
    # ps
      PID USER       VSZ STAT COMMAND
        1 root      1124 S    init       
        2 root         0 SW   [kthreadd]
        3 root         0 SW   [ksoftirqd/0]
        4 root      3888 S    /usr/app/bin/app

    说明:先telnet进去,输入用户名和密码之后,执行ps命令。如果有/usr/app/bin/app进程,说明设备正常,否则就需要重启。

     

    但是手动操作比较麻烦,需要使用python登录网关设备,检测是否正常。

     

    二、Telnet对象

    Telnet.read_until(expected, timeout=None) : #读取连接服务器后显示的内容,直到遇到同expected相同的字节串。或者等待时间大于timeout时直接向下运行。

    Telnet.read_very_eager() : 读取从上次IO阻断到现在所有的内容,返回的是字节串,需要进行decode()编码。如果连接关闭或者没有可用数据时会抛出EOFError,如果没有其他可用的数据,返回的是b"",除非在IAC中间,否则不会阻碍。

    Telnet.open(host, port=23[, timeout]) : 连接到主机,端口号为第二个可选参数,默认为标准的Telnet端口(23),可选的timeout参数指定连接的超时时间,如果未指定,将使用全局默认超时设置。不要尝试去重新打开一个已经连接的实例对象。

    Telnet.close() : 关闭连接。

    Telnet.write(buffer) : # 将一个字节串(byte string)写进socket,如果连接被阻塞,这也会被阻塞,如果连接关闭,会抛出OSError

    Telnet.interact() : telnet的交互功能,下面用了一个死循环保证用户能够一直输入命令进行某些操作,也可以使用Telnet.interact()这个方法来使所连接终端持久化,不过官网说 (emulates a very dumb Telnet client)直译是一个非常愚蠢的客户端。

     

    官方文档,请参考:

    https://docs.python.org/zh-cn/3/library/telnetlib.html

     

    三、检测脚本

    check_gateway.py

    #!/usr/bin/env python3
    # coding: utf-8
    import telnetlib
    import time
    
    class TelnetClient(object):
        def __init__(self, ip, user, pswd):
            self.tn = telnetlib.Telnet()
            self.host_ip = ip
            self.username = user
            self.password = pswd
            self.last_res = ''  # 记录上次命令执行结果
    
        # 此函数实现telnet登录主机
        def login_host(self):
            try:
                # self.tn = telnetlib.Telnet(host_ip,port=23)
                self.tn.open(self.host_ip)
            except:
                text = '{} 网络连接失败'.format(self.host_ip)
                print(text)
                return False
            # 等待login出现后输入用户名,最多等待0.3秒
            self.tn.read_until(b'Username: ', timeout=0.3)
    
            self.tn.write(self.username.encode('ascii') + b'\n')
            # 等待Password出现后输入用户名,最多等待0.3秒
            self.tn.read_until(b'Password: ', timeout=0.3)
    
            self.tn.write(self.password.encode('ascii') + b'\n')
            # 延时5秒再收取返回结果,给服务端足够响应时间
            # time.sleep(5)
    
            # 获取登录结果
            # read_very_eager()获取到的是的是上次获取之后本次获取之前的所有输出
            command_result = self.tn.read_very_eager().decode('utf-8')
    
            if 'Login invalid' in command_result:  # Cisco交换登录失败提示语
                text = '{} 登录失败,用户名或密码错误'.format(self.host_ip)
                print(text)
                return False
            else:
                text = '{} 登录成功'.format(self.host_ip)
                print(text)
                return True
    
        # 执行某一条命令
        def execute_command(self, command, show_res=False):
            self.tn.write(command.encode() + b'\n')
            time.sleep(3)
    
            # 获取命令结果
            command_result = self.tn.read_very_eager().decode('utf-8')
            # if show_res:
            #     print('命令执行结果:%s' % command_result)
            return command_result
    
        # 退出telnet
        def logout_host(self):
            self.tn.write(b"exit\n")
            # print('本次操作结束,连接断开\n')
    
    def check_gateway(ip):
        # 指定连接的交换机管理IP
        telnet_client = TelnetClient(ip=ip, user='root', pswd='123456')
    
        # 如果登录结果返加True,则执行命令,然后退出
        if not telnet_client.login_host():
            print("错误,ip: %s 登录失败"%ip)
            return False
    
        # 执行指定命令,查看进程
        res1 = telnet_client.execute_command('ps')
        # print(res1, type(res1))
        if not res1:
            print("错误,ip: %s 执行命令ps失败" % ip)
            return False
    
        if "/usr/app/bin/app" in res1:
            print("正常,ip: %s 设备正常" % ip)
            telnet_client.execute_command('exit')  # 退出配置模式
        else:
            print("错误,ip: %s 设备掉线" % ip)
            # 执行重启
            res2 = telnet_client.execute_command('reboot')
    
        # 退出登录
        telnet_client.logout_host()
        return True
    
    if __name__ == '__main__':
        ip = "10.212.82.90"
        check_gateway(ip)

    注意:请根据实际情况,修改ip地址,用户名和密码。

     

    执行脚本

    # python3 check_gateway.py 10.212.82.90 登录成功
    正常,ip: 10.212.82.90 设备正常

     

    本文参考链接:

    https://blog.csdn.net/XYliurui/article/details/103082512


关键字