Bash script for gathering IP addresses of interfaces through SNMP I

05/12/2014 22:47

    

        This script can help you to have fresh information about used IP addresses at your network devices.

        Script is written in bash that it is not a good tool for larger programming but it can easily run at every linux machine. Script needs a list of L3 network devices (router, switch, firewall, ..) for input. It uses SNMP queries for creating list of all IP interfaces of all network devices. Output of this list is formatted in the same way as hosts file so it can be easily added to your host file. Than the hosts file can serves as a simple tool for showing device name and its interface in output of traceroute command.

 

Example of the result file

A.B.C.D Device_hostname-Interface_name #/Prefix #Interface description
10.1.1.1 router_R01-Gi1/0/2 #28  
10.5.1.22 Beijing-router01-WAN-Tu611 #24 # Port to internet
192.168.24.254 firewall2-eth5 #24  

   

How does a traceroute look like

$ tracert cacti-cn.company.com
Tracing route to cacti-cn.company.com [10.55.12.28]
over a maximum of 30 hops:
  1    10 ms    11 ms    11 ms  firewal1-eth2.547 [10.100.104.99]
  2    12 ms    11 ms    12 ms  location01-sw01-Vl521 [10.100.30.222]
  3    12 ms    11 ms    11 ms  firewal2-eth5 [10.100.30.29]
  4    13 ms    12 ms    12 ms  location01-router02-WAN-Gi2/0.146 [10.100.146.73]
  5   249 ms   246 ms   246 ms  Beijing-router01-WAN-Tu611 [10.110.64.34]
  6   277 ms   278 ms   279 ms  Beijing-sw01-Gi0/1 [10.25.31.139]
  7   279 ms   278 ms   279 ms  cacti.pdc.company.cn  10.55.12.28]

You can see hostnames of routers and its interfaces in this traceroute even if nobody has entered these IPs into DNS. It helps to easily check if routing is correct or not (it suppose you names of your routers contains at least location).

 

How does the script work

  • 1. The script reads lines from input file (Device list). One line = one device. IP address is the first, SNMP parameters (SNMP v.2 or SNMP v.3) follows. Records are separated by spaces.
  • 2. For every device: A SNMP query for getting hostname (OID sysName 1.3.6.1.2.1.1.5) is used. Than SNMP queries go through IP interfaces table (OID ipAddrEntry .1.3.6.1.2.1.4.20.1) and return list of Interface IPs table (OID ipAdEntAddr 1.3.6.1.2.1.4.20.1.1), Network Masks (OID ipAdEntNetMask 1.3.6.1.2.1.4.20.1.3) and their Indexes (OID ipAdEntIfIndex 1.3.6.1.2.1.4.20.1.2). These Indexes are used for getting other records from other tables. IP interfaces table uses an IP address as its index. Examples: 

                    1.3.6.1.2.1.4.20.1.1.127.0.0.1 - IP interface (snmpget command returns 127.0.0.1)                    

                    1.3.6.1.2.1.4.20.1.3.127.0.0.1 - Network Mask (snmpget command returns 255.255.255.255)       

                    1.3.6.1.2.1.4.20.1.2.127.0.0.1 - Interface Index (snmpget command returns 1)

  • 3. Interface name is taken from IfTable (OID ifDescr 1.3.6.1.2.1.2.2.1.2.Index – e.g. FastEthernet0/1) and Interface Description from ifXTable (OID ifAlias 1.3.6.1.2.1.31.1.1.1.18.Index – e.g. Port to Internet). Interface Description is usually defined by device administrator.Results are written to the Result file. Examples: 

                    1.3.6.1.2.1.2.2.1.2.1 - Interface Name (snmpget command returns Loopback1)                    

                    1.3.6.1.2.1.31.1.1.1.18.1 - Interface Description (snmpget command returns Internal loopback)

Standard output is used for displaying informative and debugging information. Standard error output is used for erros (mainly SNMP timeouts). Desired results (IP addresses and router name and interface name) are in the Result file.

 

How to use the script

  • 1. Copy the script to your linux machine and set rights for execution (e.g. chmod +x IP_interfaces_and_its_names.sh).
  • 2. Prepare Input file with list of devices and their SNMP credentials. Example of lines in this file (Input_file.txt):

                192.168.1.1 public   ### It is for SNMP v.1/2, the next for SNMP v.3:

                192.168.1.2 md5 AAAPassphrase authPriv BBBSecurityname 3des CCCPrivpassprase

  • 3. Run the script. First parameter is a name of Input file, the second is a name of the Result file (Output_file.txt). Running the script without any parameters shows basic help. Use absolute path for script name or write ./IP_interfaces_and_its_names.txt if you type the command from directory where the files is. 

                 ./IP_interfaces_and_its_names.sh Input_file.txt Output_file.txt

  • 4. See results from Result file Output_file.txt. Script produces a lot of messages to standard output but these messages are meant for debugging purposes or for information what the script is doing. Results (hostnames and IPs) are in the result file.

 

Download of the script

The script is in the file IP_interfaces_and_its_names.txt (30,1 kB).

This web does not allow .sh extension for attached files so I renamed it with .txt extension.

Topic: Bash script for gathering IP addresses of interfaces through SNMP

| 30/12/2014

New comment