// Home | Go Back //
import com.ireasoning.util.ParseArguments;
import com.ireasoning.protocol.snmp.*;
import com.ireasoning.protocol.TimeoutException;
import java.io.IOException;
public class snmpget extends snmp
{
static int _getMethod = 1;
public static void main(String[] args)
{
snmpget s = new snmpget();
s.parseOptions(args, "snmpget");
if(_getMethod == 1)
{ s.get1();
}
else if(_getMethod == 2)
{ s.get2();
}
}
private void get1()
{
try
{
SnmpSession session = new SnmpSession(_host, _port, _community,
_community, _version, _transportLayer);
session.setTimeout(5000); session.setRetries(0); if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword,
_privProtocol, _privPassword, _context, null);
}
SnmpPdu retPdu = session.snmpGetRequest(_oids);
print(retPdu);
session.close();
}
catch(TimeoutException timeout)
{
System.out.println( "time out");
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
}
}
private void get2()
{
try
{
SnmpSession session = new SnmpSession(_host, _port, _community,
_community, _version, _transportLayer);
if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword, _privProtocol, _privPassword, _context, null);
}
SnmpVarBind[] vblist = new SnmpVarBind[_oids.length];
for (int i = 0; i < vblist.length ; i++)
{
vblist[i] = new SnmpVarBind(_oids[i]);
}
SnmpPdu pdu = new SnmpPdu(SnmpConst.GET, vblist);
SnmpPdu retPdu = session.send(pdu);
print(retPdu);
session.close();
}
catch(TimeoutException timeout)
{
System.out.println( "time out");
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
}
}
protected void moreOptions()
{
System.out.println( "-w {1|2} which get method to use, default is 1");
}
protected void printExample(String programName)
{
System.out.println( "java " + programName + " localhost .1.3.6.1.2.1.1.3.0");
System.out.println( "java " + programName + " localhost sysUpTime");
System.out.println( "java " + programName +
" localhost -v 3 -u newUser -A abc12345 -X abc12345 .1.3.6.1.2.1.1.3.0" );
}
protected void parseOptions(String[] args, String program)
{
super.parseOptions(args, program);
_getMethod = Integer.parseInt(_parseArgs.getOptionValue('w', "1"));
}
protected void printMoreOptions()
{
System.out.println( "get-method =\t\t" + _getMethod);
}
protected ParseArguments newParseArgumentsInstance(String[] args)
{
return new ParseArguments(args, "?ho", "utvaAXxcpmnrwk");
}
}