代码参考《Visual C++ 开发基于SNMP的网络管理软件》书中的代码,由于这个是用C++实现的用的库也是SNMP++。SNMP4J是参考SNMP4J++ 编写的。网上也没用用SNMP4J实现这个功能,我参考代码写了一个类似的,初步运行没什么问题,可能还有其他问题,后面慢慢修了
1 public class DeviceScannImp implements DeviceScann{
2 //保存OID的链表
3 private ArrayList<String> oidStr;
4 //保存值的链表
5 private ArrayList<String> value;
6 private String err;
7
8 private Snmp snmp = null;
9 private Address targetAddress = null;
10 private CommunityTarget target = null;
11
12 //装载解析MIB文件。暂时不实现
13 public void loadMIBs(){
14
15 }
16
17 public void setTarget(String ip, OctetString community, String oidStr){
18 targetAddress = GenericAddress.parse("udp:" + ip + "/161");
19 try {
20 DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
21 snmp = new Snmp(transportMapping);
22 transportMapping.listen();
23
24
25 target = new CommunityTarget(targetAddress, community);
26 target.setRetries(2);
27 target.setTimeout(1500);
28 target.setVersion(SnmpConstants.version2c);
29
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
34
35 /**
36 * 获取子树中的全部内容
37 * @param ip ip地址
38 * @param community 团体名称 ,我们才用的是public OceteString 八进制字符串
39 * @param oid 的字符串
40 * @return 返回到List中。
41 */
42 @Override
43 public void getTree(String ip, OctetString community, String oidStr) {
44
45 ArrayList<OID> OIDList = new ArrayList<OID>();
46 ArrayList<Variable> variableList = new ArrayList<Variable>();
47
48 //构造目的IP和端口
49 targetAddress = GenericAddress.parse("udp:" + ip + "/161");
50
51 try {
52 DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping();
53 snmp = new Snmp(transportMapping);
54 transportMapping.listen();
55
56 target = new CommunityTarget(targetAddress, community);
57 target.setRetries(2);
58 target.setTimeout(1500);
59 target.setVersion(SnmpConstants.version2c);
60
61 OID baseOID = new OID(oidStr);
62 OID nextOid = new OID(baseOID.toString() + ".1");
63 System.out.println("------------baseOID : " + baseOID);
64 VariableBinding vb = new VariableBinding(baseOID);
65
66 PDU pdu = new PDU();
67
68 pdu.add(vb);
69 pdu.setType(PDU.GET);
70 // ResponseEvent responseEvent = snmp.send(pdu, target);
71 // ResponseEvent responseEvent = snmp.getNext(pdu, target);
72
73 while(baseOID.leftMostCompare(baseOID.size(), nextOid) == 0){
74 System.out.println("执行到这里");
75 ResponseEvent re = snmp.getNext(pdu, target);
76 PDU pdu2 = re.getResponse();
77
78 if(re != null && re.getResponse() != null){
79 System.out.println("进入到if中。。。");
80 //拿到第一个VariableBinding
81 VariableBinding variableBinding = pdu2.get(0);
82 OID replyOID = variableBinding.getOid();
83
84 nextOid = replyOID;
85 System.out.println("replyOID : " + replyOID);
86 System.out.println("nextOid : " + nextOid);
87 Variable replyVariable = variableBinding.getVariable();
88 // System.out.println("获取到的Value: " + replyVariable);
89 //这里主要是判断拿到的值是否为空,有的值为空,那么后面就会出异常,所以判断是否为空后主动为这个值赋一个NULL值
90 //但是Variable是一个借口,并没有提供一个方法或者函数对这个进行复制,这里简单转换了一下。
91 if(replyVariable.equals("") || replyVariable == null){
92 String s = replyVariable.toString();
93 s += "NULL";
94 Variable variable = new OctetString(s);
95 replyVariable = variable;
96 }
97 System.out.println("baseOID : " + baseOID);
98 System.out.println(baseOID.leftMostCompare(baseOID.size(), nextOid));
99 if(baseOID.leftMostCompare(baseOID.size(), nextOid) == 0){
100 System.out.println("进入到第三个if");
101 replyOID.trim();
102 replyVariable.toString().trim();
103 OIDList.add(replyOID);
104 variableList.add(replyVariable);
105
106 System.out.println(replyOID + " : " + replyVariable);
107
108 }
109 }else{
110 return;
111 }
112 System.out.println("设置OID :" + nextOid);
113 vb.setOid(nextOid);
114 // vb.setVariable(null);
115 pdu.add(vb);
116 }
117
118 snmp.close();
119
120 // PDU pdu2 = responseEvent.getResponse();
121 //
122 // @SuppressWarnings("unchecked")
123 // Vector<VariableBinding> vector = (Vector<VariableBinding>) pdu2.getVariableBindings();
124 //
125 // for(int i = 0; i < vector.size(); i++){
126 // VariableBinding variableBinding = (VariableBinding)vector.elementAt(i);
127 // System.out.println(variableBinding.getOid() + " : " + variableBinding.getVariable());
128 // }
129 //
130 } catch (IOException e) {
131 e.printStackTrace();
132 }
133 }
134
135
136 public static void main(String[] args) {
137 DeviceScannImp deviceScannImp = new DeviceScannImp();
138 deviceScannImp.getTree("127.0.0.1", new OctetString("public"), ".1.3.6.1.2.1.1");
139 }
140
141 }