SnmpTool.java
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.sitech.util;
import org.snmp4j.mp.SnmpConstants;
public class SnmpTool {
public static void getOid(String devIp, String cummunity, int port,
String oid, int version) {
try {
SNMPClient client = new SNMPClient(devIp, port, cummunity, version);
client.connect();
System.out.println("oidValue=" + client.get(oid));
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void walkOid(String devIp, String cummunity, int port,
String oid, int version) {
try {
SNMPClient client = new SNMPClient(devIp, port, cummunity, version);
client.connect();
System.out.println(client.walk(oid));
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
if (args == null || args.length < 6
|| "-h".equals(args[0].toLowerCase())) {
System.out
.println(". ./snmpWalk.sh <SNMP_VERSION> <IP_ADDR> <SNMP_PORT> <COMMUNITY> <OID>");
// java com.sitech.util.SnmpTool walk 2c 172.16.9.234 161 public 1
System.out
.println("java com.sitech.util.SnmpTool <get|walk> <SNMP_VERSION> <IP_ADDR> <SNMP_PORT> <COMMUNITY> <OID>");
System.exit(-1);
}
// get/walk
String type = args[0];
int version = SnmpConstants.version2c;
String ipAddr = args[2];
int port = Integer.parseInt(args[3]);
String community = args[4];
String oid = args[5];
if ("1".equals(args[1])) {
version = SnmpConstants.version1;
} else if ("2".equals(args[1]) || "2c".equals(args[1].toLowerCase())) {
version = SnmpConstants.version2c;
} else if ("3".equals(args[1])) {
version = SnmpConstants.version3;
}
if (type.equals("get")) {
SnmpTool.getOid(ipAddr, community, port, oid, version);
} else if (type.equals("walk")) {
SnmpTool.walkOid(ipAddr, community, port, oid, version);
}
}
}