设为首页 加入收藏

TOP

通过snmp获取设备每个接口的配置IP地址,网段信息和VLAN接口号(一)
2023-09-23 15:44:24 】 浏览:244
Tags:通过 snmp 地址 段信息 VLAN

第一部分,观察通过snmp OID能获取的信息,对信息进行关联。

1、通过 snmp获取到接口IP地址和掩码信息,发现IP地址作为索引值;

2、每个IP地址的索引,都可以关联到接口的索引

3、每个接口索引,都可以通过snmp获取到接口的名称,

降这个3个数据进行关联,可以得到接口名称和网段信息的关联。

第二部分:通过代码实现。

get_vlan_network.py

import re,os,ipaddress


#get the interface Vlan value
def get_Vlanif_value(host,SNMP_community):


    vlan_dict = {}
    pattern = re.compile(r'(\d+)\s*=\s*STRING:\s*(\S+)')


    cmd = "snmpwalk -v 2c -c " + SNMP_community  +" "+ host + " ifname | grep Vlan" # 进行过滤,仅显示VLAN接口
    tmp = os.popen(cmd).readlines()
    # print("begin:",tmp)


    for i in tmp:
        matches = re.search(pattern, i)


        if matches:
            if_id = matches.group(1) #if_id: interface_snmp_ID
            Vlan_value = re.search(r'\d+', matches.group(2)).group()
            # print(if_id,Vlan_value)
            vlan_dict[if_id] = Vlan_value
   
    return vlan_dict



# VLAN = get_Vlan_value(host)
# print(VLAN)



# get the interface ip address and inetface snmp ID
def get_if_ip(host,SNMP_community):


    if_dict = {}
    pattern = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) = INTEGER: (\d+)'


    cmd = "snmpwalk -v 2c -c " + SNMP_community +" "+ host + " .1.3.6.1.2.1.4.20.1.2"
    tmp = os.popen(cmd).readlines()


    for i in tmp:
        matches = re.search(pattern, i)


        if matches:
            ip_address = matches.group(1)
            if_id = matches.group(2)
            if_dict[ip_address] = if_id


    return if_dict


# IF_value = get_if_ip(host)
# print(IF_value)



def get_network_value(host,SNMP_community):
   
    network_dict = {}
    pattern = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) = IpAddress: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'


    cmd = "snmpwalk -v 2c -c " + SNMP_community +" " + host + " .1.3.6.1.2.1.4.20.1.3 | grep -wv -e 255.255.255.255"
    tmp = os.popen(cmd).readlines()


    for i in tmp:
        matches = re.search(pattern, i)


        if matches:
            ip_address = matches.group(1)
            subnet_mask = matches.group(2)
            network_str = f"{ip_address}/{subnet_mask}"
            network = ipaddress.IPv4Network(network_str, strict=False)


            network_dict[ip_address] = network.with_prefixlen
   
    return network_dict



def get_network_subnet_Vlan(host,SNMP_community):
    # 将网段信息与VLAN ID进行关联


    Netowrk_Vlan ={}
    Vlan_info = get_Vlanif_value(host, SNMP_community)
    If_info =  get_if_ip(host, SNMP_community)
    Network_info = get_network_value(host, SNMP_commun
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【matplotlib基础】--文本标注 下一篇数据分析(以kaggle上的加州房价..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目