public class DeviceScannImp implements DeviceScann{
private Snmp snmp = null;
private Address targetAddress = null;
private CommunityTarget target = null;
/**
* 获取子树中的全部内容
* @param ip ip地址
* @param community 团体名称 ,我们才用的是public OceteString 八进制字符串
* @param oid 的字符串
* @return 返回到List中。
*/
@Override
public void getTree(String ip, OctetString community, String oidStr) {
//构造目的IP和端口
targetAddress = GenericAddress.parse("udp:" + ip + "/161");
try {
DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
snmp = new Snmp(transportMapping);
transportMapping.listen();
target = new CommunityTarget(targetAddress, community);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
OID oid = new OID(oidStr);
// OID nextOid = oid.append(".1");
VariableBinding vb = new VariableBinding(oid);
PDU pdu = new PDU();
pdu.add(vb);
pdu.setType(PDU.GET);
// ResponseEvent responseEvent = snmp.send(pdu, target);
ResponseEvent responseEvent = snmp.get(pdu, target);
// while(oid.){
//
// }
PDU pdu2 = responseEvent.getResponse();
@SuppressWarnings("unchecked")
Vector<VariableBinding> vector = (Vector<VariableBinding>) pdu2.getVariableBindings();
for(int i = 0; i < vector.size(); i++){
VariableBinding variableBinding = (VariableBinding)vector.elementAt(i);
System.out.println(variableBinding.getOid() + " : " + variableBinding.getVariable());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DeviceScannImp deviceScannImp = new DeviceScannImp();
deviceScannImp.getTree("127.0.0.1", new OctetString("public"), ".1.3.6.1.2.1.25.1");
}