[英]How can i get a string array in java from linux command?
I am new in Java and i use runtime exec to run commands in linux using java.我是 Java 新手,我使用运行时 exec 在 linux 中使用 java 运行命令。 I was expecting to find a command to get all network adapters to find the ip on each adapter and bring them all to array.
我期待找到一个命令来让所有网络适配器找到每个适配器上的 ip 并将它们全部放入数组。
How can i do that?我怎样才能做到这一点?
Thanks!谢谢!
You can obviously run ifconfig
and parse the output, but why would you want to?您显然可以运行
ifconfig
并解析输出,但您为什么要这样做?
Just use NetworkInterface
, as per the example:只需使用
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");
}
Output (I have lots of virtual interfaces):输出(我有很多虚拟接口):
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
You can easily filter for non-virtual interfaces that are up by changing the example to:您可以通过将示例更改为:
for (NetworkInterface netint : Collections.list(nets)) {
if (!netint.isVirtual() && netint.isUp()) {
displayInterfaceInformation(netint);
}
}
I managed to solve this using:我设法使用以下方法解决了这个问题:
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.