Configure network devices using the SNMP protocol with C# and .NET
The Simple Network Management Protocol (SNMP) is an Internet Standard protocol to manage network devices and can be used to change the behaviour of a network device remotely and help with network monitoring.
Use the more secure SNMP v3 standard for improved performance, flexibility and security to deploy and monitor network devices. In this article, a simpler implementation is demonstrated using SNMP v3 and TFTP file server but a more secure HTTP/S connection is recommended instead of TFTP. We will be using an opensource SNMP library called SnmpSharpNet. This library implements the protocol version 1, 2 and 3 of the SNMP standard.
You will also need a Management Information Base (MIB) information/specifications from the device manufacturer for the corresponding network device. MIB is a database used to manage the entities in a communication network. This demonstration will target Cisco Routers.
Most of the code in the snippets is fairly straightforward to understand. We may have used some logging statements for information purpose and it is assumed that the reader is aware of various logging mechanisms in C# .NET but is not mandatory and can be excluded. It is also assumed that the reader is aware of some of the .NET networking implementations like the System.Net namespace and IpAddress class.
Initial set-up
Step 1: Add the SnmpSharpNet nuget package:
Install-Package SnmpSharpNet -Version 0.9.5
Step 2: Create a new UDP ‘target’ and check if it's reachable:
The SNMP protocol uses UDP as its transport protocol and hence we will need an UdpTarget object to make sure that the target is reachable. We will use a built-in discover mechanism supplied by the library.
Step 3: Add authorization:
If the previous step was successful, we now need to add authorization information to the parameter. Refer the below statement:
The first param, SecurityName is the name of the security/user. The user should be added to the router configuration and should also have Read and Write access for copying new configurations into the router. The second param, Authentication digest is the Hash algorithm used in the authentication password. The third param, is the hashed password which is configured in the router. The fourth param is the privacy encryption protocol that will be used to create the instructions and the fifth param is the privacy secret.
If you are unsure of these parameter values, it is best to discuss with the network administrator to get more information on the network device which has been set up.
Step 4: Create the corresponding MIB string constants definition:
We will be targeting the Cisco router devices and the MIB definitions can be found at cisco.com appendix.
Step 5: Implement a mechanism to send out SNMP commands:
We need a way to send out these SNMP commands to the router and SnmpSharpNet makes it fairly simple.
Below is a sample code that can be used in which we pass the dependencies via the parameters to be sent to the device identified by its IP address.
Step 6: Find a random number and check if it's in use already:
You will now need a random 3 digit number that will be used to further identify your SNMP request sets.
Note: This step may be dependent on the type of network device your are targeting. In this article, the Cisco routers mandate the use of the same 3 digit random number along with every MIB instruction for particular operation set.
It is quite possible that you may receive negative responses and hence a retry mechanism is implemented with a max retry count. If appropriate permissions are not provided, the router will always return a negative status and should be carefully considered while implementing this mechanism.
Below is a way to get a random number. However, you can use your own algorithm.
Copying a new config to the router’s running memory from a TFTP server:
Once the initial set up is completed, we can now trigger commands to instruct the router device to update its running configuration. It is assumed that you have configured a TFTP server within the network and it is reachable from the network device. The new configuration should also be placed in the servers root directory and should be accessible when requested by the router device.
Step 1: Command set to copy the config file to the router’s running memory:
Take a look at the MIB’s for reference.
The <target-ip-string> parameter will be the IP address of the network device. Pass the Pdu (Protocol Data Unit) as the third parameter. Append the generated random number in the previous step with the MIB command separated with a ‘.’ as the fourth parameter. The last parameter is the AsnType parameter to pass the data after encoding it. Based on the type of MIB instruction, the Asn value changes.
The <tftp-server-ip-string> is the IP address of the TFTP server which hosts the config that needs to be copied into the router device.
The <file-name-string> is the name of the file holding the config for the router network device.
Step 2: Save the router’s running-config to startup memory:
Most of the instructions remain the same compared to the previous step except for some Asn instructions which can be referred below.
Copying the router’s running-config to a TFTP server:
To copy the router’s running-config back to a TFTP server follow the above setup mechanism and the commands below.
The <tftp-server-ip-string> is the IP address of the TFTP server which hosts the config that needs to be copied into the router device.
The <file-name-string> is the name of the file that will be created by the router device on the server.
We can also check for the completion of the file copy by getting the copy status from the router. It usually takes a few seconds for the router to fully copy the file back to the server, hence running this instruction in a loop until the success status or a max timeout is reached will be a good way to start.
Conclusion
If you are in need to constantly upgrade your router’s configurations in your organization using an automated method and are using C#.NET as the automation solution, SnmpSharpNet is a good implementation to go along with, its opensource and fairly reliable. This article is based on the article published at cisco.com. and can be referenced further for more details.
The above code snippet is for reference purpose only.