我是Java的新手,我使用运行时exec在linux中使用java运行命令。我期待找到一个命令让所有网络适配器在每个适配器上找到ip并将它们全部带到数组。
我该怎么做?
谢谢!
答案 0 :(得分:1)
您显然可以运行ifconfig
并解析输出,但为什么要这样做?
根据示例,使用NetworkInterface
:
public static void main(String args[]) throws SocketException {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets))
displayInterfaceInformation(netint);
}
static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
}
out.printf("\n");
}
输出(我有很多虚拟接口):
Display name: Software Loopback Interface 1
Name: lo
InetAddress: /127.0.0.1
InetAddress: /0:0:0:0:0:0:0:1
Display name: WAN Miniport (SSTP)
Name: net0
Display name: WAN Miniport (L2TP)
Name: net1
Display name: WAN Miniport (PPTP)
Name: net2
Display name: WAN Miniport (PPPOE)
Name: ppp0
Display name: WAN Miniport (IPv6)
Name: eth0
Display name: WAN Miniport (Network Monitor)
Name: eth1
Display name: WAN Miniport (IP)
Name: eth2
Display name: RAS Async Adapter
Name: ppp1
Display name: Realtek PCIe GBE Family Controller
Name: eth3
InetAddress: /fe80:0:0:0:c433:88be:e447:fc71%eth3
Display name: Realtek PCIe GBE Family Controller #2
Name: eth4
InetAddress: /fe80:0:0:0:958b:9507:bc64:c299%eth4
Display name: Microsoft ISATAP Adapter #2
Name: net3
InetAddress: /fe80:0:0:0:0:5efe:c0a8:78%net3
Display name: Teredo Tunneling Pseudo-Interface
Name: net4
InetAddress: /fe80:0:0:0:0:100:7f:fffe%net4
Display name: WAN Miniport (IKEv2)
Name: net5
Display name: Microsoft ISATAP Adapter #3
Name: net6
Display name: Microsoft 6to4 Adapter
Name: net7
Display name: Bluetooth Device (RFCOMM Protocol TDI)
Name: net8
Display name: Bluetooth Device (Personal Area Network)
Name: eth5
Display name: RangeMax Wireless-N USB Adapter WN111v2
Name: wlan0
Display name: VirtualBox Host-Only Ethernet Adapter
Name: eth6
InetAddress: /192.168.56.1
InetAddress: /fe80:0:0:0:5027:ce97:8aff:1562%eth6
Display name: NETGEAR A6210 WiFi USB3.0 Adapter
Name: wlan1
Display name: NETGEAR A6210 WiFi USB3.0 Adapter
Name: wlan2
InetAddress: /192.168.0.120
InetAddress: /2002:5202:57a2:0:cda8:c106:a5c2:c227
InetAddress: /fd00:0:0:1:cda8:c106:a5c2:c227
InetAddress: /2002:5202:57a2:0:9135:876a:4b3c:8a36
InetAddress: /fd00:0:0:1:9135:876a:4b3c:8a36
InetAddress: /fe80:0:0:0:cda8:c106:a5c2:c227%wlan2
Display name: Microsoft ISATAP Adapter
Name: net9
Display name: Microsoft ISATAP Adapter #4
Name: net10
InetAddress: /fe80:0:0:0:0:5efe:c0a8:3801%net10
Display name: Microsoft ISATAP Adapter #5
Name: net11
Display name: Realtek PCIe GBE Family Controller-VirtualBox NDIS Light-Weight Filter-0000
Name: eth7
Display name: Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000
Name: eth8
Display name: Realtek PCIe GBE Family Controller-WFP LightWeight Filter-0000
Name: eth9
Display name: NETGEAR A6210 WiFi USB3.0 Adapter-Kaspersky Lab NDIS 6 Filter-0000
Name: wlan3
Display name: Realtek PCIe GBE Family Controller #2-VirtualBox NDIS Light-Weight Filter-0000
Name: eth10
Display name: Realtek PCIe GBE Family Controller #2-QoS Packet Scheduler-0000
Name: eth11
Display name: Realtek PCIe GBE Family Controller #2-WFP LightWeight Filter-0000
Name: eth12
Display name: WAN Miniport (IPv6)-Kaspersky Lab NDIS 6 Filter-0000
Name: eth13
Display name: WAN Miniport (IP)-Kaspersky Lab NDIS 6 Filter-0000
Name: eth14
Display name: WAN Miniport (IPv6)-QoS Packet Scheduler-0000
Name: eth15
Display name: WAN Miniport (Network Monitor)-Kaspersky Lab NDIS 6 Filter-0000
Name: eth16
Display name: WAN Miniport (IP)-QoS Packet Scheduler-0000
Name: eth17
Display name: Realtek PCIe GBE Family Controller #2-Kaspersky Lab NDIS 6 Filter-0000
Name: eth18
Display name: WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000
Name: eth19
Display name: VirtualBox Host-Only Ethernet Adapter-VirtualBox NDIS Light-Weight Filter-0000
Name: eth20
Display name: VirtualBox Host-Only Ethernet Adapter-QoS Packet Scheduler-0000
Name: eth21
Display name: VirtualBox Host-Only Ethernet Adapter-WFP LightWeight Filter-0000
Name: eth22
Display name: NETGEAR A6210 WiFi USB3.0 Adapter-Native WiFi Filter Driver-0000
Name: wlan4
Display name: Realtek PCIe GBE Family Controller-Kaspersky Lab NDIS 6 Filter-0000
Name: eth23
Display name: NETGEAR A6210 WiFi USB3.0 Adapter-VirtualBox NDIS Light-Weight Filter-0000
Name: wlan5
Display name: NETGEAR A6210 WiFi USB3.0 Adapter-QoS Packet Scheduler-0000
Name: wlan6
Display name: NETGEAR A6210 WiFi USB3.0 Adapter-WFP LightWeight Filter-0000
Name: wlan7
通过将示例更改为:
,您可以轻松过滤出虚拟的非虚拟接口for (NetworkInterface netint : Collections.list(nets)) {
if (!netint.isVirtual() && netint.isUp()) {
displayInterfaceInformation(netint);
}
}
答案 1 :(得分:0)
我设法使用以下方法解决了这个问题:
for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
for (InetAddress address : Collections.list(ni.getInetAddresses())) {
if (address instanceof Inet4Address) {
String ipadr = address.toString().replaceAll("/", "");
ipadr = ipadr.replaceAll("127.0.0.1", "");
if (!ipadr.equalsIgnoreCase("")) {
ipss.add(ipadr);
}
}
}
}