Unbrowse Automation 2 : Adding SNMP Agents

Some of our users manage dozens of network devices. Unbrowse SNMP features a scripting interface that allows you to automatically import SNMP agents from any format. You have to write a tiny bit of code in a scripting language like VBScript or Ruby.

This is the second and concluding part of the article. In the first part, we looked at the object model for SNMP Agents in Unbrowse.

The task

You are an organization running CiscoWorks to manage your dozens or hundreds of routers and switches.  You do not want to input the agent information (such as name, ip address, community) by hand – this is just too painful. If you have a flat file with agent information, you can write a simple script to import them into Unbrowse.

In this example, we will import a file in CiscoWorks DCRv3 format into Unbrowse SNMP. This is meant only to illustrate the scripting interface, you can adapt the script to your own file format quite easily.

Without much ado here is the script (in VBScript) :

To run the script, type “cscript impagent.vbs dcrtestfile.txt”

' ------------------------------------------------------------
' IMPORT agents in Ciscoworks DCRv3 format into Unbrowse SNMP
'
'  Usage: impagent
' ------------------------------------------------------------
Const ForReading=1

Set Stdout = WScript.Stdout
Set FSO = CreateObject("Scripting.FileSystemObject")

' -----------------------
' Check usage & arguments
' -----------------------
if WScript.Arguments.Count 1 then
 Stdout.WriteLine "Usage: impagent "
 WScript.Quit
end if
DCRFileName = WScript.Arguments.Item(0)
' ----------------------------------
' Open input file and read all lines
' ----------------------------------
Set InputFile = FSO.OpenTextFile( DCRFileName, ForReading)

InputFileContents = InputFile.ReadAll
If Not Err.Number = 0 Then
  If Err.Number = 424 Then StdOut.WriteLine "Input DCR File not found" & DCRFileName & vbCRLF
  StdOut.WriteLine "Error : " & Err.Description
  WScript.Quit
End If

InputFileLines = Split(InputFileContents,vbCRLF)

' ----------------------------------
' Create the Unbrowse Agent Manager
' ----------------------------------
Set AgentMgr =  CreateObject("UnbrowseSNMP.AgentManager")
AgentMgr.Init()

validLinesBegin = False
dcrCheck = False
For Each sLine In InputFileLines

 Trim(sLine)
 ' Skip comments and zero length
 If Len(sLine) > 0 AND Mid(sLine,1,1)   ";"  Then

  If validLinesBegin Then
   ProcessLine (sLine)
  Else
   If dcrCheck Then
    If InStr(sLine,"management_ip_address") Then
     validLinesBegin = True
    End If
   Else
    If InStr(sLine,"Type=DCRCSV") Then
     dcrCheck = True
    End If
   End If

  End If
 End If
  
Next

If Not dcrCheck Then
 StdOut.WriteLine "The input file is not in CiscoWorks DCRv3 format : filename " & DCRFileName & vbCRLF
End If
' ------------------------------------------------------------------------------
' Process a single line
' Create an agent and set attributes as specified in the input line (CSV format)
' ------------------------------------------------------------------------------
Sub ProcessLine (Line)

 LineFields = Split(Line,",")

 If UBound(LineFields) > 3 Then

  ipaddr = LineFields(0)
  hostname = LineFields(1)
  rcomm = LineFields(8)
  wcomm = LineFields(9)

  Set OneAgent = AgentMgr.NewAgent
  OneAgent.Name = hostname
  OneAgent.IPAddress = ipaddr
  OneAgent.ReadComm = rcomm
  OneAgent.WriteComm = wcomm

  StdOut.WriteLine "Added agent " & hostname & " to Unbrowse repository" & vbCRLF

 End If

End Sub
 

A sample test file is shown below

; This file is generated by DCR Export utilityCisco Systems NM Data import, Source=DCR Export; Type=DCRCSV; Version=3.0

;

;Start of section 0 - Basic Credentials

;

;HEADER:
management_ip_address,host_name,domain_name,device_identity,display_name,sysObjectID,dcr_device_type,mdf_type,snmp_v2_ro_comm_string,snmp_v2_rw_comm_string,user_defined_field_0,user_defined_field_1

;

10.77.202.40,Switch6009,cisco.com,,Switch2,1.3.6.1.4.1.9.1.281,0,268438100,public,private,field0,field1

10.77.202.10,Router7000,cisco.com,,Router1,1.3.6.1.4.1.9.1.8,0,278464493,public,private,field0,field1

10.77.202.30,Switch4006,cisco.com,,Switch1,1.3.6.1.4.1.9.5.46,0,268438086,public,private,field0,field1

10.77.202.20,Router6400,cisco.com,,Router2,1.3.6.1.4.1.9.1.180,0,269214543,public,private,field0,field1
;End of CSV file
 

[tags] SNMP, MIB Browser, Agents, VBScript [/tags]

Author: Vivek Rajagopalan

Vivek Rajagopalan is the a lead developer for Trisul Network Analytics. Prior products were Unsniff Network Analyzer and Unbrowse SNMP. Loves working with packets , very high speed networks, and helping track down the bad guys on the internet.