簡體   English   中英

如何從 linux 命令獲取 java 中的字符串數組?

[英]How can i get a string array in java from linux command?

我是 Java 新手,我使用運行時 exec 在 linux 中使用 java 運行命令。 我期待找到一個命令來讓所有網絡適配器找到每個適配器上的 ip 並將它們全部放入數組。

我怎樣才能做到這一點?

謝謝!

您顯然可以運行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);
    }
}

TL;DR:使用 JDK,Luke!

我設法使用以下方法解決了這個問題:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM