Introduction to Net-SNMP
Net-SNMP is a suite of applications that provide tools to monitor and manage network devices using the Simple Network Management Protocol (SNMP). It is widely used for its robustness, flexibility, and ease of use.
Getting Started with Net-SNMP
To start using Net-SNMP, install it on your system. For example, on a Debian-based system, you can use:
sudo apt-get install snmp snmpd snmp-mibs-downloader
API Examples
1. Snmpget
The snmpget
command is used to query a network entity for a specific piece of information.
snmpget -v 2c -c public localhost system.sysUpTime.0
2. Snmpwalk
The snmpwalk
command retrieves a subtree of management values from a network entity.
snmpwalk -v 2c -c public localhost system
3. Snmpset
The snmpset
command is used to set the value of a MIB object on a network entity.
snmpset -v 2c -c private localhost system.sysContact.0 s "Network Admin"
4. Snmpstatus
The snmpstatus
command is a convenient way to get a quick overview of a network entity.
snmpstatus -v 2c -c public localhost
5. Snmptranslate
The snmptranslate
command converts between numerical or symbolic forms of object identifiers.
snmptranslate -IR -On system.sysUpTime
Sample API Usage in an Application
Below is an example of an application that integrates some of the above APIs to monitor network device uptime and contact information.
#!/usr/bin/env python3 from pysnmp.hlapi import * def get_uptime(host, community): iterator = getCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((host, 161)), ContextData(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysUpTime', 0))) errorIndication, errorStatus, errorIndex, varBinds = next(iterator) 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: print(' = '.join([x.prettyPrint() for x in varBind])) def set_contact(host, community, contact): iterator = setCmd(SnmpEngine(), CommunityData(community), UdpTransportTarget((host, 161)), ContextData(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysContact', 0), contact)) errorIndication, errorStatus, errorIndex, varBinds = next(iterator) if errorIndication: print(errorIndication) elif errorStatus: print('%s at %s' % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or '?')) else: print('Set successful') if __name__ == "__main__": host = 'localhost' community = 'public' get_uptime(host, community) set_contact(host, 'private', 'Network Admin')
This example application demonstrates basic SNMP operations using the Net-SNMP Python library, pysnmp
.
By understanding and utilizing these APIs and examples, you can effectively manage and monitor your network devices using Net-SNMP.
Hash: 9f945026834dcaec9418cf36c37e37ebfaace4d90701043f4753a9ba4789b83d