已知ip地址,使用java代码实现获取交换机所有端口的详细信息,并且放入list
时间: 2023-08-31 15:35:26 浏览: 164
### 回答1:
如果您想获取交换机所有端口的详细信息,您可以使用 SNMP (Simple Network Management Protocol) 协议来实现。您可以使用 java 来编写代码,通过 SNMP 协议与交换机进行通信,以获取交换机的信息。
下面是一个示例代码:
```
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.Target;
import org.snmp4j.TransportMapping;
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;
public class SnmpUtils {
public static List<String> get(String ipAddress, String community, String oid) throws IOException {
List<String> result = new ArrayList<>();
Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/161");
TransportMapping transport = new DefaultUdpTransportMapping();
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setAddress(targetAddress);
target.setRetries(2);
target.setTimeout(1500);
target.setVersion(SnmpConstants.version2c);
PDU pdu = new PDU();
pdu.add(new VariableBinding(new OID(oid)));
pdu.setType(PDU.GETNEXT);
Snmp snmp = new Snmp(transport);
ResponseEvent responseEvent = snmp.send(pdu, target);
PDU response = responseEvent.getResponse();
if (response == null) {
return result;
}
for (VariableBinding vb : response.getVariableBindings()) {
result.add(vb.getVariable().toString());
}
return result;
}
}
```
在上面的代码中,您可以调用 `SnmpUtils.get` 方法来获取交换机
### 回答2:
要获取交换机所有端口的详细信息,并将其放入列表中,可以使用Java的网络编程和SNMP协议来实现。
首先,你可以使用Java中的`InetAddress`类来获取指定IP地址的主机对象。然后,通过创建一个`SnmpWalk`对象来实现通过SNMP协议来获取详细信息。
以下是一个可能的实现示例:
```java
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.log.LogFactory;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TreeUtils;
public class SwitchPortInfo {
public static void main(String[] args) {
String ipAddress = "192.168.1.1";
String community = "public";
int snmpVersion = SnmpConstants.version2c;
int snmpPort = 161;
List<String> portList = getSwitchPortInfo(ipAddress, community, snmpVersion, snmpPort);
for (String port : portList) {
System.out.println(port);
}
}
public static List<String> getSwitchPortInfo(String ipAddress, String community, int snmpVersion, int snmpPort) {
List<String> portList = new ArrayList<>();
try {
InetAddress targetAddress = InetAddress.getByName(ipAddress);
TransportMapping<?> transport = new DefaultUdpTransportMapping();
Snmp snmp = new Snmp(transport);
transport.listen();
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString(community));
target.setVersion(snmpVersion);
target.setAddress(new UdpAddress(targetAddress, snmpPort));
target.setRetries(2);
PDU pdu = new PDU();
pdu.setType(PDU.GETBULK);
pdu.setMaxRepetitions(10);
OID oid = new OID("1.3.6.1.2.1.2.2.1.2"); // 获取接口名的OID
TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
List<TreeEvent> events = treeUtils.getSubtree(target, oid);
for (TreeEvent event : events) {
if (event == null) {
continue;
}
if (event.isError()) {
System.err.println("Error: " + event.getErrorMessage());
continue;
}
VariableBinding[] varBindings = event.getVariableBindings();
if (varBindings == null || varBindings.length == 0) {
continue;
}
for (VariableBinding varBinding : varBindings) {
if (varBinding == null) {
continue;
}
portList.add(varBinding.toValueString());
}
}
snmp.close();
} catch (Exception e) {
e.printStackTrace();
}
return portList;
}
}
```
请注意,该示例代码使用了第三方库`snmp4j`来实现SNMP操作。你需要添加它作为项目的依赖。
这个示例代码通过SNMP协议获取了交换机上的接口名称,并将其放入了一个`List<String>`对象中。你可以根据实际需求,通过SNMP协议获取更多的接口详细信息。
阅读全文