SNMP Device Control

One of the more interesting things SNMP can do is control a device through the SNMP protocol. Obviously, this creates a significant advantage over using something like Pyexpect (http://sourceforge.net/projects/pexpect/) to control a router, as it is much more straightforward.

For brevity's sake, we will only cover SNMP v1 in the example, but if you are communicating with a device over an insecure network, it should be done via SNMP v3. For this section, it would be good to reference Essential SNMP and Cisco IOS Cookbook by Kevin Dooley and Ian J. Brown (O'Reilly) if you have a Safari account or have bought those books. They include some excellent information about both talking to Cisco devices via SNMP and basic configuration.

Because reloading a Cisco configuration via SNMP is plain cool, it seems like a perfect choice to talk about device control. For this example it is necessary to have a running TFTP server from which the router will pull the IOS file, and the router must be configured to allow read/write access for SNMP. Example 7-6 is what the Python code looks like.

Example 7-6. Upload new switch configuration Cisco router import netsnmp

vars = netsnmp.Varbind(netsnmp.VarList(netsnmp.Varbind(".1.2.6.1.4.1.9.2.10.6.0", "1"), (netsnmp.Varbind("cisco.example.com.1.3.6.1.4.1.9.2.10.12.172.25.1.1", "iso-config.bin")

result = netsnmp.snmpset(vars,

DestHost='cisco.example.com', Community='readWrite')

In this example, we used Net-SNMP's VarList to assign the instruction to first erase the flash for the switch and second load a new IOS image file. This could be the basis for a script that upgrades the IOS of every switch at once in a data center. As with all code in this book, you should test this out in a nonproduction environment before just seeing what happens.

One final thing to point out is that SNMP is often not thought of in terms of device control, but it is a powerful way to programmatically control devices in a data center, as it serves as a uniform specification for device control that has been under development since 1988. The future probably holds a very interesting story for SNMP v3.

0 0

Post a comment

  • Receive news updates via email from this site