使用SNMP生成交换机上的子网列表吗?


Answers:


8

接口/ IP寻址:

如果要查询设备的IP地址,子网掩码和相应的接口,则可以使用IP-MIBIF-MIB MIB中的以下OID :

  • .1.3.6.1.2.1.4.20.1.1 -可以在此OID上找到IP地址

    ~]$ snmptranslate .1.3.6.1.2.1.4.20.1.1
    IP-MIB::ipAdEntAddr
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.4.20.1.1
    IP-MIB::ipAdEntAddr.10.30.46.1 = IpAddress: 10.30.46.1
    IP-MIB::ipAdEntAddr.25.255.25.254 = IpAddress: 25.255.25.254
    IP-MIB::ipAdEntAddr.55.44.33.22 = IpAddress: 55.44.33.22
    IP-MIB::ipAdEntAddr.172.31.10.10 = IpAddress: 172.31.10.10
    


  • .1.3.6.1.2.1.4.20.1.3 -在此OID处找到子网掩码拐杖

    ~]$ snmptranslate .1.3.6.1.2.1.4.20.1.3
    IP-MIB::ipAdEntNetMask
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.4.20.1.3
    IP-MIB::ipAdEntNetMask.10.30.46.1 = IpAddress: 255.255.255.0
    IP-MIB::ipAdEntNetMask.25.255.25.254 = IpAddress: 255.255.255.0
    IP-MIB::ipAdEntNetMask.55.44.33.22 = IpAddress: 255.255.255.0
    IP-MIB::ipAdEntNetMask.172.31.10.10 = IpAddress: 255.255.255.0
    


  • .1.3.6.1.2.1.4.20.1.2-接口索引(ifTable Indexes)是每个接口的唯一整数。

    ~]$ snmptranslate .1.3.6.1.2.1.4.20.1.2
    IP-MIB::ipAdEntIfIndex
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.4.20.1.2
    IP-MIB::ipAdEntIfIndex.10.30.46.1 = INTEGER: 1
    IP-MIB::ipAdEntIfIndex.25.255.25.254 = INTEGER: 5
    IP-MIB::ipAdEntIfIndex.55.44.33.22 = INTEGER: 6
    IP-MIB::ipAdEntIfIndex.172.31.10.10 = INTEGER: 7
    


  • .1.3.6.1.2.1.2.2.1.2-可以在此OID处找到接口友好名称,并...2.1.2.[INDEX]为每个接口附加ifTable索引(例如)。

    ~]$ snmptranslate .1.3.6.1.2.1.2.2.1.2
    IF-MIB::ifDescr
    
    ~]$ snmpwalk -v2c -c cisco 10.30.46.1 .1.3.6.1.2.1.2.2.1.2
    IF-MIB::ifDescr.1 = STRING: FastEthernet0/0
    IF-MIB::ifDescr.2 = STRING: FastEthernet0/1
    IF-MIB::ifDescr.4 = STRING: Null0
    IF-MIB::ifDescr.5 = STRING: Loopback0
    IF-MIB::ifDescr.6 = STRING: Tunnel10
    IF-MIB::ifDescr.7 = STRING: Dialer1
    IF-MIB::ifDescr.8 = STRING: Virtual-Access1
    

您可以手动使用这些OID,以您选择的语言编写脚本,或者使用更聪明的程序/脚本,类似于Tim Peck的答案中提到的内容。

这是一个快速(又脏)的shell示例:

#!/bin/bash
# duct taped by one.time
# Basic interface information collector

##
# Set usage var and getoptions
usage="Usage: interface-info.sh -H <IP Address> -C <snmp community string>

OPTIONS:
  -H Hostname          set IP address or hostname
  -h Help              prints usage options

SNMPv2 OPTIONS:
  -C Community         set SNMPv2 community string
"

while getopts H:C:h option;
do
        case $option in
                H) ipaddress=$OPTARG;;
                C) community=$OPTARG;;
                h) echo "$usage"
                exit $invalid_result;;
        esac
done

##
# Prevent blank argvars
if [[ -z $ipaddress || -z $community ]]; then
  echo "$usage"
  exit 0
fi

## 
# Set field separator to new line
IFS=$'\n'

##
# Store our IP-MIB info in arrays
ipAdEntAddr=( $(snmpbulkwalk -v2c -c $community  $ipaddress .1.3.6.1.2.1.4.20.1.1 | awk -F ": " '{print $2}') )
ipAdEntNetMask=( $(snmpbulkwalk -v2c -c $community $ipaddress .1.3.6.1.2.1.4.20.1.3 | awk -F ": " '{print $2}') )
ipAdEntIfIndex=( $(snmpbulkwalk -v2c -c $community $ipaddress .1.3.6.1.2.1.4.20.1.2 | awk -F ": " '{print $2}') )

for ((i=0; i<${#ipAdEntAddr[@]}; i++)); do
  ifDescr[$i]=$(snmpwalk -v2c -c $community $ipaddress .1.3.6.1.2.1.2.2.1.2.${ipAdEntIfIndex[$i]} | awk -F ": " '{print $2}')
  echo "${ifDescr[$i]}: ${ipAdEntAddr[$i]} ${ipAdEntNetMask[$i]}"
done

例:

~]$ ./interface-info.sh -H 10.30.46.1 -C cisco
FastEthernet0/0: 10.30.46.1 255.255.255.0
Loopback0: 25.255.25.254 255.255.255.0
Tunnel10: 55.44.33.22 255.255.255.0
Dialer1: 172.31.10.10 255.255.255.0


VLAN:

如果要查找VLAN ID和VLAN名称,则可以使用以下OID:

  • .1.3.6.1.4.1.9.9.46.1.3.1.1.4.1vtpVlanName可以发现(思科设备)在此OID和VLAN-ID可以发现所附,例如:...1.4.1.[VLAN-ID](类似的ifIndex和ifDescr上面的例子)。

    ~]$ snmptranslate  .1.3.6.1.4.1.9.9.46.1.3.1.1.4.1
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1
    
    ~]$ snmpwalk -v2c -c cisco 192.168.0.8 SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1 = STRING: "default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.10 = STRING: "VLAN0010"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.21 = STRING: "VLAN0021"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.100 = STRING: "VLAN0100"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.344 = STRING: "VLAN0344"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.456 = STRING: "iSCSI-TRAFFIC"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1002 = STRING: "fddi-default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1003 = STRING: "token-ring-default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1004 = STRING: "fddinet-default"
    SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.4.1.1005 = STRING: "trnet-default"
    


手动抓取VLAN ID的示例:

    ~]$ snmpbulkwalk -v2c -c cisco 192.168.0.8 1.3.6.1.4.1.9.9.46.1.3.1.1.4 | sed -e 's/.*4.1.\(.*\) =.*/\1/'
    1
    10
    21
    100
    344
    456
    1002
    1003
    1004
    1005

1

如果您想以类似于主机文件的格式从交换机/路由器获取有关接口IP的信息,则可以使用我的bash脚本。我想获得有关我们网络中网络设备的所有IP地址的实际信息。通过SNMP可以访问所有设备。我找不到能做到的工具或脚本,所以我决定自己编写。输出可以简单地添加到主机文件中。接口描述或前缀被写为注释,因此它可以例如在traceroute输出中显示,同时您可以在主机文件中搜索接口描述。

输出格式如下:

ABCD Device_hostname-Interface_name#/前缀#接口说明

10.1.1.1 router_R01-Gi1 / 0/2#28

10.5.1.22 Beijing-router01-WAN-Tu611#24#上网端口

192.168.24.254 Firewall2-eth5#24

该脚本可从http://network-linux.webnode.cz/news/bash-script-for-gathering-ip-addresses-of-interfaces-through-snmp获得


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.