目录:
[一]、环境参数
[二]、代码示例
1.同步GET 单个OID(v2c)
代码:SnmpGet.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package
com
.
micmiu
.
snmp4j
.
demo1x
;
import
java
.
io
.
IOException
;
import
org
.
snmp4j
.
CommunityTarget
;
import
org
.
snmp4j
.
PDU
;
import
org
.
snmp4j
.
Snmp
;
import
org
.
snmp4j
.
event
.
ResponseEvent
;
import
org
.
snmp4j
.
mp
.
SnmpConstants
;
import
org
.
snmp4j
.
smi
.
Address
;
import
org
.
snmp4j
.
smi
.
GenericAddress
;
import
org
.
snmp4j
.
smi
.
OID
;
import
org
.
snmp4j
.
smi
.
OctetString
;
import
org
.
snmp4j
.
smi
.
VariableBinding
;
import
org
.
snmp4j
.
transport
.
DefaultUdpTransportMapping
;
/**
* 演示: GET单个OID值
*
* blog http://www.micmiu.com
*
* @author Michael
*/
public
class
SnmpGet
{
public
static
final
int
DEFAULT_VERSION
=
SnmpConstants
.
version2c
;
public
static
final
String
DEFAULT_PROTOCOL
=
"udp"
;
public
static
final
int
DEFAULT_PORT
=
161
;
public
static
final
long
DEFAULT_TIMEOUT
=
3
*
1000L
;
public
static
final
int
DEFAULT_RETRY
=
3
;
/**
* 创建对象communityTarget
*
* @param targetAddress
* @param community
* @param version
* @param timeOut
* @param retry
* @return CommunityTarget
*/
public
static
CommunityTarget
createDefault
(
String
ip
,
String
community
)
{
Address
address
=
GenericAddress
.
parse
(
DEFAULT_PROTOCOL
+
":"
+
ip
+
"/"
+
DEFAULT_PORT
)
;
CommunityTarget
target
=
new
CommunityTarget
(
)
;
target
.
setCommunity
(
new
OctetString
(
community
)
)
;
target
.
setAddress
(
address
)
;
target
.
setVersion
(
DEFAULT_VERSION
)
;
target
.
setTimeout
(
DEFAULT_TIMEOUT
)
;
// milliseconds
target
.
setRetries
(
DEFAULT_RETRY
)
;
return
target
;
}
public
static
void
snmpGet
(
String
ip
,
String
community
,
String
oid
)
{
CommunityTarget
target
=
createDefault
(
ip
,
community
)
;
Snmp
snmp
=
null
;
try
{
PDU
pdu
=
new
PDU
(
)
;
// pdu.add(new VariableBinding(new OID(new int[]
// {1,3,6,1,2,1,1,2})));
pdu
.
add
(
new
VariableBinding
(
new
OID
(
oid
)
)
)
;
DefaultUdpTransportMapping
transport
=
new
DefaultUdpTransportMapping
(
)
;
snmp
=
new
Snmp
(
transport
)
;
snmp
.
listen
(
)
;
System
.
out
.
println
(
"-------> 发送PDU <-------"
)
;
pdu
.
setType
(
PDU
.
GET
)
;
ResponseEvent
respEvent
=
snmp
.
send
(
pdu
,
target
)
;
System
.
out
.
println
(
"PeerAddress:"
+
respEvent
.
getPeerAddress
(
)
)
;
PDU
response
=
respEvent
.
getResponse
(
)
;
if
(
response
==
null
)
{
System
.
out
.
println
(
"response is null, request time out"
)
;
}
else
{
// Vector<VariableBinding> vbVect =
// response.getVariableBindings();
// System.out.println("vb size:" + vbVect.size());
// if (vbVect.size() == 0) {
// System.out.println("response vb size is 0 ");
// } else {
// VariableBinding vb = vbVect.firstElement();
// System.out.println(vb.getOid() + " = " + vb.getVariable());
// }
System
.
out
.
println
(
"response pdu size is "
+
response
.
size
(
)
)
;
for
(
int
i
=
0
;
i
&
lt
;
response
.
size
(
)
;
i
++
)
{
VariableBinding
vb
=
response
.
get
(
i
)
;
System
.
out
.
println
(
vb
.
getOid
(
)
+
" = "
+
vb
.
getVariable
(
)
)
;
}
}
System
.
out
.
println
(
"SNMP GET one OID value finished !"
)
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
System
.
out
.
println
(
"SNMP Get Exception:"
+
e
)
;
}
finally
{
if
(
snmp
!=
null
)
{
try
{
snmp
.
close
(
)
;
}
catch
(
IOException
ex1
)
{
snmp
=
null
;
}
}
}
}
/**
*
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
String
ip
=
"192.168.8.254"
;
String
community
=
"public"
;
String
oidval
=
"1.3.6.1.2.1.1.1.0"
;
SnmpGet
.
snmpGet
(
ip
,
community
,
oidval
)
;
}
}
|
运行结果:
1
2
3
4
5
6
7
8
9
|
--
--
--
-
&
gt
;
发送
PDU
&
lt
;
--
--
--
-
PeerAddress
:
192.168.8.254
/
161
response
pdu
size
is
1
1.3.6.1.2.1.1.1.0
=
H3C
ICG2000
H3C
Comware
Platform
Software
Comware
Software
Version
5.20
,
Release
2104P02
Copyright
(
c
)
2004
-
2010
Hangzhou
H3C
Technologies
Co
.
,
Ltd
.
SNMP
GET
one
OID
value
finished
!
|
2.同步GET 多个OID(v2c)
代码:SnmpGetList.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package
com
.
micmiu
.
snmp4j
.
demo1x
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
org
.
snmp4j
.
CommunityTarget
;
import
org
.
snmp4j
.
PDU
;
import
org
.
snmp4j
.
Snmp
;
import
org
.
snmp4j
.
event
.
ResponseEvent
;
import
org
.
snmp4j
.
mp
.
SnmpConstants
;
import
org
.
snmp4j
.
smi
.
Address
;
import
org
.
snmp4j
.
smi
.
GenericAddress
;
import
org
.
snmp4j
.
smi
.
OID
;
import
org
.
snmp4j
.
smi
.
OctetString
;
import
org
.
snmp4j
.
smi
.
VariableBinding
;
import
org
.
snmp4j
.
transport
.
DefaultUdpTransportMapping
;
/**
* 演示:GET 多个OID值
*
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
SnmpGetList
{
public
static
final
int
DEFAULT_VERSION
=
SnmpConstants
.
version2c
;
public
static
final
String
DEFAULT_PROTOCOL
=
"udp"
;
public
static
final
int
DEFAULT_PORT
=
161
;
public
static
final
long
DEFAULT_TIMEOUT
=
3
*
1000L
;
public
static
final
int
DEFAULT_RETRY
=
3
;
/**
* 创建对象communityTarget
*
* @param targetAddress
* @param community
* @param version
* @param timeOut
* @param retry
* @return CommunityTarget
*/
public
static
CommunityTarget
createDefault
(
String
ip
,
String
community
)
{
Address
address
=
GenericAddress
.
parse
(
DEFAULT_PROTOCOL
+
":"
+
ip
+
"/"
+
DEFAULT_PORT
)
;
CommunityTarget
target
=
new
CommunityTarget
(
)
;
target
.
setCommunity
(
new
OctetString
(
community
)
)
;
target
.
setAddress
(
address
)
;
target
.
setVersion
(
DEFAULT_VERSION
)
;
target
.
setTimeout
(
DEFAULT_TIMEOUT
)
;
// milliseconds
target
.
setRetries
(
DEFAULT_RETRY
)
;
return
target
;
}
/**
*
* @param ip
* @param community
* @param oid
*/
public
static
void
snmpGetList
(
String
ip
,
String
community
,
List
&
lt
;
String
&
gt
;
oidList
)
{
CommunityTarget
target
=
SnmpUtil
.
createDefault
(
ip
,
community
)
;
Snmp
snmp
=
null
;
try
{
PDU
pdu
=
new
PDU
(
)
;
for
(
String
oid
:
oidList
)
{
pdu
.
add
(
new
VariableBinding
(
new
OID
(
oid
)
)
)
;
}
DefaultUdpTransportMapping
transport
=
new
DefaultUdpTransportMapping
(
)
;
transport
.
listen
(
)
;
snmp
=
new
Snmp
(
transport
)
;
System
.
out
.
println
(
"------->发送消息<-------"
)
;
pdu
.
setType
(
PDU
.
GET
)
;
ResponseEvent
respEvent
=
snmp
.
send
(
pdu
,
target
)
;
System
.
out
.
println
(
"PeerAddress:"
+
respEvent
.
getPeerAddress
(
)
)
;
PDU
response
=
respEvent
.
getResponse
(
)
;
if
(
response
==
null
)
{
System
.
out
.
println
(
"response is null, request time out"
)
;
}
else
{
System
.
out
.
println
(
"response pdu size is "
+
response
.
size
(
)
)
;
for
(
int
i
=
0
;
i
&
lt
;
response
.
size
(
)
;
i
++
)
{
VariableBinding
vb
=
response
.
get
(
i
)
;
System
.
out
.
println
(
vb
.
getOid
(
)
+
" = "
+
vb
.
getVariable
(
)
)
;
}
}
System
.
out
.
println
(
"SNMP GET List OID value finished !"
)
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
)
;
System
.
out
.
println
(
"SNMP GetList Exception:"
+
e
)
;
}
finally
{
if
(
snmp
!=
null
)
{
try
{
snmp
.
close
(
)
;
}
catch
(
IOException
ex1
)
{
snmp
=
null
;
}
}
}
}
/**
*
* @param args
*/
public
static
void
main
(
String
[
]
args
)
{
String
ip
=
"192.168.8.254"
;
String
community
=
"public"
;
List
&
lt
;
String
&
gt
;
oidList
=
new
ArrayList
&
lt
;
String
&
gt
;
(
)
;
oidList
.
add
(
".1.3.6.1.2.1.1.1.0"
)
;
oidList
.
add
(
".1.3.6.1.2.1.1.3.0"
)
;
oidList
.
add
(
".1.3.6.1.2.1.1.5.0"
)
;
SnmpGetList
.
snmpGetList
(
ip
,
community
,
oidList
)
;
}
}
|
运行结果:
1
2
3
4
5
6
7
8
9
10
11
|
--
--
--
-
&
gt
;发送消息
&
lt
;
--
--
--
-
PeerAddress
:
192.168.8.254
/
161
response
pdu
size
is
3
1.3.6.1.2.1.1.1.0
=
H3C
ICG2000
H3C
Comware
Platform
Software
Comware
Software
Version
5.20
,
Release
2104P02
Copyright
(
c
)
2004
-
2010
Hangzhou
H3C
Technologies
Co
.
,
Ltd
.
1.3.6.1.2.1.1.3.0
=
39
days
,
21
:
25
:
37.36
1.3.6.1.2.1.1.5.0
=
HX_ICG2000
SNMP
GET
List
OID
value
finished
!
|
3.异步GET (v2c)
代码:SnmpGetAsyn.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package
com
.
micmiu
.
snmp4j
.
demo1x
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
concurrent
.
CountDownLatch
;
import
java
.
util
.
concurrent
.
TimeUnit
;
import
org
.
snmp4j
.
CommunityTarget
;
import
org
.
snmp4j
.
PDU
;
import
org
.
snmp4j
.
Snmp
;
import
org
.
snmp4j
.
event
.
ResponseEvent
;
import
org
.
snmp4j
.
event
.
ResponseListener
;
import
org
.
snmp4j
.
mp
.
SnmpConstants
;
import
org
.
snmp4j
.
smi
.
Address
;
import
org
.
snmp4j
.
smi
.
GenericAddress
;
import
org
.
snmp4j
.
smi
.
OID
;
import
org
.
snmp4j
.
smi
.
OctetString
;
import
org
.
snmp4j
.
smi
.
VariableBinding
;
import
org
.
snmp4j
.
transport
.
DefaultUdpTransportMapping
;
/**
* 演示:异步GET OID值
*
* blog http://www.micmiu.com
*
* @author Michael
*
*/
public
class
SnmpGetAsyn
{
public
static
final
int
DEFAULT_VERSION
=
SnmpConstants
.
version2c
;
public
static
final
String
DEFAULT_PROTOCOL
=
"udp"
;
public
static
final
int
DEFAULT_PORT
=
161
;
public
static
final
long
DEFAULT_TIMEOUT
=
3
*
1000L
;
public
static
final
int
DEFAULT_RETRY
=
3
;
/**
* 创建对象communityTarget
*
* @param targetAddress
* @param community
* @param version
* @param timeOut
* @param retry
* @return CommunityTarget
*/
public
static
CommunityTarget
createDefault
(
String
ip
,
String
community
)
{
Address
address
=
GenericAddress
.
parse
(
DEFAULT_PROTOCOL
+
":"
+
ip
+
"/"
+
DEFAULT_PORT
)
;
CommunityTarget
target
=
new
CommunityTarget
(
)
;
target
.
setCommunity
(
new
OctetString
(
community
)
)
;
|