Professional Documents
Culture Documents
This paper presents two Java SNMP programs using two different java libraries, so that it
makes easy to develop SNMP programs in JAVA. Though lot of JAVA examples
available in web, mostly they discuss about getting a parameter from remote device using
SNMP GET operation, but not many examples exists on setting a parameter on device
using SNMP SET operation. This paper gives standalone APIs for both SET and GET
operations.
• The first example uses SNMPv1CommunicationInterface to communicate with
remote device. All other classes related can be downloaded from the following
URL and then snmp.jar file can be created.
http://www.koders.com/java/fid74D17902013A1C2446ECDEB05B271DB2B8B
B0AB2.aspx.
• The second example uses SNMP4J library. The snmp4j.jar can be downloaded
from the link: http://www.snmp4j.org/html/download.html.
Both examples are confined to SNMP SET and GET operations only and the SNMP
version is 1.
1) SNMPv1CommunicationInterface
The program listed below illustrates the use of the communication interface class to open
a connection to a remote APC UPS device. The program has two important functions
named as snmpSet and snmpGet. These methods can be used as standalone methods.
In main method
• snmpSet is called to turn ON, UPS outlet group1 by setting Value=1
• snmpGet is called to get the basic state of UPS device.
import java.net.*;
import snmp.*; //snmp.jar file
if(strstate!=null&&strstate.length()!=0)
{
m_bOnBattery = (strstate.charAt(1) == '0') ? false : true;
m_bOnLine = (strstate.charAt(3) == '0') ? false : true;
m_bReplaceBattery = (strstate.charAt(4) == '0') ? false : true;
}
//////////////////////////////////////////////////////////////////////////////
//Set Value=1 to trun ON UPS OUTLET Group1
//Value=2 to trun OFF UPS OUTLET Group1
//////////////////////////////////////////////////////////////////////////////
int Value = 1;
objSNMP.snmpset(strIPAddress, WRITE_COMMUNITY, mSNMPVersion,
OID_UPS_OUTLET_GROUP1,Value);
}
/**
* This method set the Value on the device
* community=WRITE_COMMUNITY
*/
public void snmpSet(String strIPAddress, String community, int iSNMPVersion,
String strOID, int Value)
{
try
{
InetAddress hostAddress = InetAddress.getByName(strIPAddress);
SNMPv1CommunicationInterface comInterface = New
SNMPv1CommunicationInterface(iSNMPVersion,
hostAddress, community);
SNMPv1CommunicationInterface comInterface =
new SNMPv1CommunicationInterface(iSNMPVersion, hostAddress,
community);
System.out.println("strOID =" + strOID);
// returned Vector has just one pair inside it.
SNMPVarBindList newVars = comInterface.getMIBEntry(strOID);
// extract the object identifier from the pair; it's the first element in the sequence
SNMPObjectIdentifier snmpOID =
(SNMPObjectIdentifier)pair.getSNMPObjectAt(0);
2) SNMP4J
The program listed below uses SNMP4J library to communicate with a remote UPS
device. This program also got two important methods called as snmpSet and snmpGet.
In main method
• snmpSet is called to turn OFF, UPS outlet group1 by setting Value=2
• snmpGet is called to get the battery capacity of UPS.
import java.net.InetAddress;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
int Value = 2;
objSNMP.snmpSet(strIPAddress, WRITE_COMMUNITY,
OID_UPS_OUTLET_GROUP1, Value);
//////////////////////////////////////////////////////////
//Get Basic state of UPS
/////////////////////////////////////////////////////////
String batteryCap =objSNMP.snmpGet(strIPAddress,
READ_COMMUNITY,
OID_UPS_BATTERY_CAPACITY);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
* The following code valid only SNMP version1. This
* method is very useful to set a parameter on remote device.
*/
public void snmpSet(String strAddress, String community, String strOID, int Value)
{
strAddress= strAddress+"/"+SNMP_PORT;
Address targetAddress = GenericAddress.parse(strAddress);
Snmp snmp;
try
{
TransportMapping transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(5000);
target.setVersion(SnmpConstants.version1);