Bash script for gathering IP addresses of interfaces through SNMP II - deployment

21/02/2015 21:31

I have described the script in my previous blog post. Now I want to show how to use this script. It is my best practices.

  1. Run the script automatically by cron (e.g. every day at 2:00 AM)
  2. Rotate older results. They can be useful when the script fails and you get partial results only or no results.
  3. Redirect standard error output to a separated file. You can check the file and find for example SNMP errors (timeouts) and then analyze to find where is the problem like wrong SNMP, failed network connection etc.
  4. Manual irregular coping results to the host file on your OS. I want to avoid failure of the host file caused by some mistake in automatic copying. 
  5. Using - better results from traceroute commands of course. Just use your favorite way of traceroute (tracert in Windows, traceroute in linux, some graphic traceroute tool ...). I work as a network administrator so I sometimes need to check if a planned new connection network has not been used somewhere. Or monitoring systems reports a firewall in branch offices down so I need to connect to the firewall through its public IP instead of its private management address.  So I search host file for firewall hostname (e.g. cat /etc/hosts | grep branch-fw0) and copy its public IP and try to ping it or ssh.

Cron

            The script can run under your account or you can create a special user just for the script. It is not generally recommended to use root account for running scripts like this. Script does not require any special rights - common linux user should be able to run the script without installing any packages, libraries etc.

            Cron settings can be showed by command crontab –l, it can be edited by command crontab –e. You have to use commands from vi editor for editing crontab. The easiest way is opening editing by crontab –e. Press I or I for switching to editor mode and paste text from a clipboard. Press Esc for switching to command mode and then type :wq (Write Quit). Verify correct settings by command crontab –l.

            Example of crontab content (script IP_interfaces_and_its_names-for_cron.sh will run every day at 2:00 AM):

  •                 00 02 * * * /home/netadmin/IP_interfaces_and_its_names-for_cron.sh /home/netadmin/Device_list.txt /home/netadmin/Results.txt

 

Version rotation

            I wrote a short “cron script” for basic file rotation that provides this rotation. This script creates copies of older result files and than call the main script (IP_interfaces_and_its_names.sh).

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

          

Redirecting standard error output

            It is done inside the cron script and partly in main script where I add a checking of result of last SNMP operation. If it is failed a line with info about affected OID will be written to the standard error output. Main script is called with this redirecting: 

  • "$ScriptFile" "$InputFileDeviceList" "$ResultFile" 2>>"$ErrorFile"

            Example of error output (timeout - SNMP community may be wrong, device was down etc). 

  • snmpget: Timeout
  •      snmpwalk .1.3.6.1.2.1.4.20.1.3 has failed for device 10.100.145.253 - Sat Feb 21 03:21:23 CET 2015
  • No log handling enabled - turning on stderr logging