Accessing a script from dynamic HTML

There are times when we run a Python script to create dynamic HTML pages (which are based upon certain triggers that we put in the script). These pages can be enhanced to add additional URLs to invoke other scripts when we click on those pages.

As an example, let's say we need to find out how many models of devices are in our network. For this we create a script, schedule it to run every hour with the task scheduler, and after each run a dynamic HTML page is created to show the updated stats or inventory. In a BYOD scenario, this also plays an important role, because each hour we can monitor what devices are on our network, and if we click on any of the discovered devices, we can get additional information such as a detailed show version.

Here's the Python code to create the dynamic HTML:

from pysnmp.hlapi import *

print('Content-Type: text/html')
print('')

def finddevices(ip):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget((ip, 161)),
ContextData(),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))
)

if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
for varBind in varBinds:
xval=(' = '.join([x.prettyPrint() for x in varBind]))
xval=xval.replace("SNMPv2-MIB::sysDescr.0 = ","")
xval=xval.split(",")
return (xval[1])

ipaddress="192.168.255.248,192.168.255.249"
ipaddress=ipaddress.split(",")
tval="<table border='1'><tr><td>IP address</td><td>Model</td></tr>"
for ip in ipaddress:
version=finddevices(ip)
version=version.strip()
ahref="http://localhost/test/showversion.py?ipaddress="+ip
tval=tval+"<tr><td><a href='"+ahref+"' target='_blank'>"+ip+"</a></td>"

tval=tval+"<td>"+version+"</td></tr>"

tval=tval+"</table>"
print (tval)

The output of the preceding code is as follows:

The preceding code creates the dynamic HTML shown in the previous screenshot. It queries the vendor from SNMP for the given IP addresses, and creates the table based upon the values. The blue color in the IP addresses denotes the hyperlink, which, when clicked, will result in the output as shown here:

Here's the Python code for the output of show version:

import cgi
from netmiko import ConnectHandler
import time

form = cgi.FieldStorage()
ipvalue=form.getvalue('ipaddress')

def getoutput(cmd):
global ipvalue
uname="cisco"
passwd="cisco"
device = ConnectHandler(device_type='cisco_ios', ip=ipvalue, username=uname, password=passwd)
output=device.send_command(cmd)
return (output)

print('Content-Type: text/plain')
print('')
print ("Device queried for ",ipvalue)
print (" Output:")
print (getoutput("show version"))

As we can see in the URL, when the user clicked on the main dynamic HTML page, it invoked another script (the one that was listed earlier) that takes the input parameter of the IP address from the URL and, using Netmiko, fetches the version of the device.

Similarly, we can gather other stats, such as CPU, memory, a routing summary, and other tasks for each of the devices quickly using the web framework approach.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.227