Java .getNetworkInterfaces()无法检测Kali Linux上的wlan0

问题描述 投票:1回答:1

我试图为airmon-ng构建一个gui,我遇到了代码.getNetworkInterface的问题。它可以在windows中检测wlan0接口,但不能在linux中运行此代码时立即检测到。我的Kali Linux可以使用Iwconfig / Ifconfig检测wlan0,但不能使用此代码检测。

ifconfig结果:

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 24189  bytes 1451329 (1.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 24189  bytes 1451329 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether f8:1a:67:0e:4a:55  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我的Java代码

nicsa.setText("Please select a wireless interface...");
        int k=99;
        String[]niclist=new String[k];
        Enumeration<NetworkInterface> eni = null;
        try {
            eni = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException ex) {
            Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
        }
        int i=0;
        for (NetworkInterface ni : Collections.list(eni)) {
            niclist[i]=ni.getName();
            if(niclist[i].matches("^[w,l,a,n]{4}\\d{1}")||niclist[i].matches("^[w,l,a,n]{4}\\d{2}")){
                nic.addItem(niclist[i]);
                i++;
            }else{nicsa.setText("No Wireless Interface found..");
            }
        }
        Process p = null;
        try {
            p = Runtime.getRuntime().exec("ifconfig");
        } catch (IOException ex) {
            Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            p.waitFor();
        } catch (InterruptedException ex) {
            Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
        }
  BufferedReader buf = new BufferedReader(new InputStreamReader(
          p.getInputStream()));
  String line = "";
  String output = "";

        try {
            while ((line = buf.readLine()) != null) {
                output += line + "\n";
            }     } catch (IOException ex) {
            Logger.getLogger(home.class.getName()).log(Level.SEVERE, null, ex);
        }
nicinfo.setText(output);
java
1个回答
1
投票

你的正则表达式看起来有点滑稽:

if(niclist[i].matches("^[w,l,a,n]{4}\\d{1}")||niclist[i].matches("^[w,l,a,n]{4}\\d{2}"))

我会选择以下内容

if(niclist[i].matches("^wlan\\d{1,2}"))

这样你知道你正在寻找一个以wlan开头并以一到两位数字结尾的字符串。

您是否尝试在要返回的字符串上转储调试语句?

你可以在调试器中运行这个程序并逐步完成逻辑吗?


    for (NetworkInterface ni : Collections.list(eni)) {
        niclist[i]=ni.getName();
        if(niclist[i].matches("^wlan\\d{1,2}")){
            // Try dumping here
            System.out.println("NIC Name: "+niclist[i]);
            nic.addItem(niclist[i]);
            i++;
        }else{nicsa.setText("No Wireless Interface found..");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.