java使用snmp walk和get代码示例
时间: 2024-06-19 22:02:46 浏览: 208
Java语言可以通过SNMP协议来实现网络设备的监控和管理,其中使用snmp walk和get是常见的操作。以下是Java实现SNMP Walk和Get的示例代码:
SNMP Walk示例代码:
```java
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SnmpWalkExample {
public static void main(String[] args) {
String ipAddress = "192.168.0.1"; // 设备IP地址
String port = "161"; // SNMP端口号,默认为161
String community = "public"; // SNMP共同体名称
int timeout = 1500; // 超时时间,单位为毫秒
int retries = 2; // 重试次数
try {
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(GenericAddress.parse("udp:" + ipAddress + "/" + port));
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(SnmpConstants.version2c);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID("1.3.6.1"))); // 这里以1.3.6.1为例,可以根据需求修改
pdu.setType(PDU.GETNEXT);
boolean finished = false;
while (!finished) {
ResponseEvent respEvent = snmp.send(pdu, target);
PDU response = respEvent.getResponse();
if (response == null) {
System.out.println("Walk结束!");
finished = true;
break;
}
for (int i=0; i<response.size(); i++) {
System.out.println(response.get(i).toString());
OID nextOID = response.get(i).getOid();
if (nextOID == null || !nextOID.startsWith(new OID("1.3.6.1"))) {
finished = true;
break;
}
pdu.setRequestID(new Integer32(0));
pdu.set(0, new VariableBinding(nextOID));
}
}
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
SNMP Get示例代码:
```java
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class SnmpGetExample {
public static void main(String[] args) {
String ipAddress = "192.168.0.1"; // 设备IP地址
String port = "161"; // SNMP端口号,默认为161
String community = "public"; // SNMP共同体名称
int timeout = 1500; // 超时时间,单位为毫秒
int retries = 2; // 重试次数
try {
TransportMapping transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(GenericAddress.parse("udp:" + ipAddress + "/" + port));
target.setRetries(retries);
target.setTimeout(timeout);
target.setVersion(SnmpConstants.version2c);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID("1.3.6.1"))); // 这里以1.3.6.1为例,可以根据需求修改
pdu.setType(PDU.GET);
ResponseEvent respEvent = snmp.send(pdu, target);
PDU response = respEvent.getResponse();
if (response == null) {
System.out.println("Get操作失败!");
return;
}
for (int i=0; i<response.size(); i++) {
System.out.println(response.get(i).toString());
}
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
阅读全文