Thursday, February 9, 2012

Working with Cisco LMS Database in Python

Cisco has published its CiscoWorks LAN Management Solution (LMS) database schema (4.2) since version 3.2. JDBC and ODBC are supported methods for accessing data LMS has collected. Setting up ODBC is a bit involved especially when I don't have the privilege to upgrade OS in a box. I don't particularly like Java either. So I thought about getting access to the data locally and possibly provide that data remotely through a web-based interface using an easier-to-use Python-based framework, maybe something like web2py.

So I installed Jython on the Windows Server 2008 box which runs my LMS 4.1 installation, copied down some sample Java code from Cisco and translated that into Python. After a few tries to get Jython to find the right Java class path, it actually worked.

So here is the code (of course, userid and password have to be filled in) that prints a list of devices in the database:


#!/usr/bin/env jython
##
##      Requires this: export CLASSPATH='E:\CSCOpx\lib\classpath'
##


import time
import java.sql


import com.sybase.jdbc2.jdbc.SybDriver


conn = java.sql.DriverManager.getConnection("jdbc:sybase:Tds:localhost:43441?SERVICENAME=cmfDb", userid, password)
print('[%s] INFO: Connection successful...' % time.strftime("%c"))
stmt = conn.createStatement()


query = "SELECT Device_Id, Device_Display_Name, Management_IPAddress FROM Network_Devices"
rs = stmt.executeQuery(query)


cnt = 0
while rs.next():
        cnt += 1
        print("%d. Device Id: %s" % (cnt, rs.getString("Device_Id")))
        print("  Display name: %s" % rs.getString("Device_Display_Name"))
        print("  Management IP address: %s" % rs.getString("Management_IPAddress"))

No comments:

Post a Comment