// Home | Go Back //
import com.ireasoning.protocol.*;
import com.ireasoning.protocol.snmp.*;
import com.ireasoning.util.ParseArguments;
public class snmpgetsubtree extends snmp implements Listener
{
static int _method = 1;
public static void main(String[] args)
{
snmpgetsubtree s = new snmpgetsubtree();
s.parseOptions(args, "snmpgetsubtree");
if(_method == 1)
{ s.getSubtree1();
}
else if(_method == 2)
{ s.getSubtree2();
}
}
public void handleMsg(Object session, Msg msg)
{
SnmpPdu pdu = (SnmpPdu) msg;
if(pdu.isSnmpV3AuthenticationFailed())
{
System.out.println( "AuthFailed");
return;
}
if(!pdu.hasMore())
{
System.out.println( "<End of Subtree Reached>");
((SnmpSession)session).close();
return;
}
print(pdu);
}
private void getSubtree1()
{
try
{
SnmpSession session = new SnmpSession(_host, _port, _community,
_community, _version, _transportLayer);
if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword, _privProtocol, _privPassword, _context, null);
}
session.snmpGetSubtree(_oids[0], this);
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
private void getSubtree2()
{
try
{
SnmpSession session = new SnmpSession(_host, _port, _community,
_community, _version, _transportLayer);
if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword, _privProtocol, _privPassword, _context, null);
}
SnmpError error = new SnmpError();
SnmpVarBind[] varbinds = session.snmpGetSubtree(_oids[0], error);
if(error.isAuthFailed())
{
System.out.println( "Authentication failed");
}
else if(error.getErrorStatus() != 0)
{
System.out.println(SnmpPdu.getErrorStatusString(error.getErrorStatus()));
}
else
{
print(varbinds);
}
session.close();
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
protected void printExample(String programName)
{
System.out.println( "java " + programName + " localhost .1.3.6.1.2.1.2.2.1");
System.out.println( "java " + programName + " localhost iftable");
System.out.println( "java " + programName + " localhost ifTable");
System.out.println( "java " + programName + " localhost system");
System.out.println( "java " + programName + " -o localhost ipRouteTable");
System.out.println( "java " + programName + " localhost mib-2");
}
protected void moreOptions()
{
System.out.println( "-w {1|2} which get method to use, default is 1");
}
protected void parseOptions(String[] args, String program)
{
super.parseOptions(args, program);
_method = Integer.parseInt(_parseArgs.getOptionValue('w', "1"));
}
protected void printMoreOptions()
{
System.out.println( "getSubtree =\t\t" + _method);
}
protected ParseArguments newParseArgumentsInstance(String[] args)
{
return new ParseArguments(args, "?ho", "utvaAXxcpmnrwk");
}
}