// Home | Go Back //
import com.ireasoning.protocol.Listener;
import com.ireasoning.protocol.Msg;
import com.ireasoning.protocol.TimeoutException;
import com.ireasoning.protocol.snmp.*;
import com.ireasoning.util.ParseArguments;
import java.io.IOException;
public class snmpwalk extends snmp implements Listener
{
static int _walkMethod = 1;
public static void main(String[] args)
{
snmpwalk s = new snmpwalk();
s.parseOptions(args, "snmpwalk", false, false);
if(_walkMethod == 1)
{ s.walk1();
}
else if(_walkMethod == 2)
{ s.walk2();
}
else if(_walkMethod == 3)
{ s.walk3();
}
}
private void walk1()
{
try
{
SnmpSession session = new SnmpSession(_host, _port, _community,
_community, _version, _transportLayer);
if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword,
_privProtocol, _privPassword, _context, null);
}
session.snmpWalk(_oids[0], this);
}
catch(TimeoutException timeout)
{
System.out.println( "time out");
}
catch(IOException ie)
{
System.out.println(ie);
ie.printStackTrace();
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
public void handleMsg(Object session, Msg msg)
{
SnmpPdu pdu = (SnmpPdu) msg;
if(pdu.getErrorStatus() != 0)
{
System.out.println( "Error: " + SnmpErrorStatus.ERR_STRINGS[pdu.getErrorStatus()] );
return;
}
if(pdu.isSnmpV3AuthenticationFailed())
{
System.out.println( "Authentication failed");
return;
}
if(!pdu.hasMore())
{
System.out.println( "<End of MIB View Reached>");
((SnmpSession)session).close();
return;
}
print(pdu);
}
private void walk2()
{
try
{
SnmpTarget target = new SnmpTarget(_host, _port, _community, _community, _version);
SnmpSession session = new SnmpSession(target, _transportLayer);
if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword,
_privProtocol, _privPassword, _context, null);
}
SnmpPdu retPdu = session.snmpGetNextRequest(_oids[0]);
if(!retPdu.hasMore())
{
System.out.println("End of mib view reached");
return;
}
print(retPdu);
while (true)
{
retPdu = session.snmpGetNextRequest(retPdu.getFirstVarBind().getName());
if(!retPdu.hasMore())
{
System.out.println("End of mib view reached");
break;
}
print(retPdu);
}
session.close();
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
private void walk3()
{
try
{
SnmpTarget target = new SnmpTarget(_host, _port, _community, _community);
SnmpSession session = new SnmpSession(target, _transportLayer);
if(_isSnmpV3)
{
session.setV3Params(_user, _authProtocol, _authPassword, _privProtocol, _privPassword, _context, null);
}
SnmpVarBind[] vblist = { new SnmpVarBind(_oids[0]) };
SnmpPdu pdu = new SnmpPdu(SnmpConst.GET_NEXT, vblist);
SnmpPdu retPdu = null;
retPdu = session.send(pdu);
if(!retPdu.hasMore())
{
System.out.println("End of mib view reached");
return;
}
print(retPdu);
while (true)
{
pdu.setOID(retPdu.getFirstVarBind().getName());
retPdu = session.send(pdu);
if(!retPdu.hasMore())
{
System.out.println("End of mib view reached");
break;
}
print(retPdu);
}
session.close();
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
protected void moreOptions()
{
System.out.println( "-w {1|2|3} which walk method to use, default is 1");
}
protected void parseOptions(String[] args,
String programName,
boolean allowMultipleOIDs,
boolean allowNoOption)
{
super.parseOptions(args, programName, allowMultipleOIDs, allowNoOption);
_walkMethod = Integer.parseInt(_parseArgs.getOptionValue('w', "1"));
}
protected void printMoreOptions()
{
System.out.println( "walk-method =\t\t" + _walkMethod);
}
protected ParseArguments newParseArgumentsInstance(String[] args)
{
return new ParseArguments(args, "?ho", "utvaAXxcpmnrwk");
}
}