python通过snmp获取交换机端口状态

from pysnmp.entity.rfc3413.oneliner import cmdgen


class NumberStatus:
    def __int__(self, host):
        self.host = host

    def walk(self, dswitch, commVlan, oid):
        errorIndication, errorStatus, errorIndex, \
        generic = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('my-agent', commVlan, 1),
                                                    cmdgen.UdpTransportTarget((dswitch, 161)), oid, maxRows=10000)
        try:
            if errorIndication or errorStatus or errorIndex:
                print(errorIndication, errorStatus, errorIndex, generic)
        except:
            return errorIndication
        return generic

    # 获取出oid和接口编号的对应关系
    def get_number(self):
        ifnumber = self.walk(self.host, "community", (1, 3, 6, 1, 2, 1, 2, 2, 1, 2))  # 通过oid获取端口编号
        inter_desc = {}
        for i in ifnumber:
            oid_ifnumber = ''
            for j in i:
                # print(j)
                oid_ifnumber += str(j)
            oid_ifnumber = oid_ifnumber.replace("SNMPv2-SMI::mib-2.2.2.1.2", "")
            oid_ifnumber = oid_ifnumber[oid_ifnumber.find('.') + 1:]
            oid_ifnumber = oid_ifnumber.split(' = ')
            port = oid_ifnumber[0]
            num = oid_ifnumber[1]
            inter_desc[port] = num
            #字典中排除非物理接口,如null0,管理口,捆绑口等
            for key in list(inter_desc):
                if 'GigabitEthernet' not in inter_desc[key] and 'Ten-GigabitEthernet' not in inter_desc[key]:
                    del inter_desc[key]
            for key2 in list(inter_desc):
                if 'M-GigabitEthernet' in inter_desc[key2]:
                    del inter_desc[key2]
        return inter_desc
        # print(inter_desc)

    # 获取出oid和接口状态的对应关系
    def get_status(self):
        ifstatus = self.walk(self.host, "community", (1, 3, 6, 1, 2, 1, 2, 2, 1, 8))  # 通过oid获取端口状态
        inter_status = {}
        for i in ifstatus:
            oid_ifstatus = ''
            for j in i:
                # print(j)
                oid_ifstatus += str(j)
            oid_ifstatus = oid_ifstatus.replace("SNMPv2-SMI::mib-2.2.2.1.8", "")
            oid_ifstatus = oid_ifstatus[oid_ifstatus.find('.') + 1:]
            #状态为1得修改为up,状态为2得修改为down
            if '= 1' in oid_ifstatus:
                oid_ifstatus = oid_ifstatus.replace(' = 1', ' = up')
            if '= 2' in oid_ifstatus:
                oid_ifstatus = oid_ifstatus.replace(' = 2', ' = down')
            oid_ifstatus = oid_ifstatus.split(' = ')
            port = oid_ifstatus[0]
            state = oid_ifstatus[1]
            inter_status[port] = state
        return inter_status
        # print(inter_status)

    def num_status(self):
        numstauts = {}
        port_status = self.get_status() #提前获取状态信息,避免每次for循环都需要调用获取状态信息得函数,导致运行很慢
        for port, number in self.get_number().items():
            numstauts[number] = port_status[port]
            # print(number, result.get_status()[port])
        return numstauts


result = NumberStatus()
result.host = '10.0.253.1'
port_status = result.num_status()
print(port_status)

 

posted @ 2023-02-27 17:31  小馒头TT  阅读(728)  评论(0编辑  收藏  举报