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

标签 java

我试图为airmon-ng构建一个GUI,但我遇到了代码.getNetworkInterface的问题。它可以在 Windows 中检测到 wlan0 接口(interface),但当我在 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);

最佳答案

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

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..");
        }
    }

关于Java .getNetworkInterfaces() 无法在 Kali Linux 上检测到 wlan0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41416662/

相关文章:

java - IntelliJ IDEA 建议用 foreach 方法替换 for 循环。我应该尽可能这样做吗?

java - 如何在已选择的菜单项旁边添加复选标记?

java - 如何仅通过链接设置 jsp 页面的访问权限

java - 无法创建事务 [Spring Boot + Hibernate]

java - 如何将 URL 中的图像保存到内部或外部存储中?

java - 如何在 Activemq jms 队列中设置消息 ID?

java - 如何重用ByteArrayInputStream?

java - 我可以在 Hazelcast 中使用 LinkedHashMap 吗?

java - AlarmReceiver 从未收到来自 Fragment 的调用

java - 在 Android 中检查文件是否存在不起作用