Java.getNetworkInterfaces()无法在Kali Linux上检测到wlan0
我试图为airmon ng构建gui,但我遇到了代码.getNetworkInterface的问题。它可以在windows中检测wlan0接口,但在linux中运行此代码时就无法检测。我的Kali Linux可以使用Iwconfig/Ifconfig检测wlan0,但不能使用此代码 ifconfig结果:Java.getNetworkInterfaces()无法在Kali Linux上检测到wlan0,java,Java,我试图为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
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
lo:flags=73 mtu 65536
inet 127.0.0.1网络掩码255.0.0.0
inet6::1前缀轴128作用域ID 0x10
循环txqueuelen 1(本地环回)
接收数据包24189字节1451329(1.3 MiB)
接收错误0丢弃0超出0帧0
发送数据包24189字节1451329(1.3 MiB)
发送错误0丢弃0溢出0载波0冲突0
wlan0:flags=4099 mtu 1500
乙醚f8:1a:67:0e:4a:55 txqueuelen 1000(以太网)
接收数据包0字节0(0.0B)
接收错误0丢弃0超出0帧0
发送数据包0字节0(0.0B)
发送错误0丢弃0溢出0载波0冲突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);
nicsa.setText(“请选择无线接口…”);
int k=99;
字符串[]niclist=新字符串[k];
枚举eni=null;
试一试{
eni=NetworkInterface.getNetworkInterfaces();
}捕获(SocketException例外){
Logger.getLogger(home.class.getName()).log(Level.SEVERE,null,ex);
}
int i=0;
用于(网络接口ni:Collections.list(eni)){
niclist[i]=ni.getName();
if(niclist[i].匹配(“^[w,l,a,n]{4}\\d{1}”)| | niclist[i].匹配(“^[w,l,a,n]{4}\\d{2}”)){
nic.addItem(niclist[i]);
i++;
}else{nicsa.setText(“未找到无线接口”);
}
}
进程p=null;
试一试{
p=Runtime.getRuntime().exec(“ifconfig”);
}捕获(IOEX异常){
Logger.getLogger(home.class.getName()).log(Level.SEVERE,null,ex);
}
试一试{
p、 waitFor();
}捕获(中断异常例外){
Logger.getLogger(home.class.getName()).log(Level.SEVERE,null,ex);
}
BufferedReader buf=新的BufferedReader(新的InputStreamReader(
p、 getInputStream());
字符串行=”;
字符串输出=”;
试一试{
而((line=buf.readLine())!=null){
输出+=行+“\n”;
}}catch(IOException-ex){
Logger.getLogger(home.class.getName()).log(Level.SEVERE,null,ex);
}
nicinfo.setText(输出);
您的正则表达式看起来有点滑稽:
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开头,以一到两个数字结尾的字符串
您是否尝试对返回的字符串转储调试语句
你能在调试器中运行这个程序并逐步完成逻辑吗
同时,我不确定调试的事情,当我在windows上的netbean上运行它时,它会检测到大多数接口,如wlan0 1 2 3 4 5,但在linux上不会。我试着用谷歌搜索并使用下面的代码,结果还是一样的。字符串[]niclist=新字符串[k];埃尼;eni=NetworkInterface.getNetworkInterfaces();int i=0;for(NetworkInterface ni:Collections.list(eni)){niclist[i]=ni.getName();i++;}for(int p=0;pI在一个名为listnet的新项目中尝试了该代码,并在linux中运行,结果是相同的……最终结果'root@kali:~/Desktop#java-jar ListNet.jarroot@kali:~/Desktop#“String[]niclist=新字符串[k];枚举eni;eni=NetworkInterface.getNetworkInterfaces();int i=0;for(NetworkInterface ni:Collections.list(eni)){niclist[i]=ni.getName();if(niclist[i].matches(^wlan\\d{1,2}){//尝试在此处转储System.out.println(“NIC名称:+niclist[i]);i++;}我在这里遗漏了一些东西-您是否在控制台中看到wlan接口列表?其次,在linux系统上,您是在那里运行开发环境,还是只是将Jar文件推到那里?如果您在程序中遇到困难,我几乎建议将groovy上传到您的目标系统并编写这篇文章在groovy中遇到困难。这样,您就可以将其作为脚本进行测试,而不必在每次修改时构建jar。或者使用远程调试运行。另一个选项是在if/matches条件之前移动System.out.println-这样您就可以转储/all/尝试了为什么还要使用正则表达式?为什么不干脆
如果(niclist[i].startsWith(“wlan”)
?谢谢你的提示,我想我会把它改成这个,完全好的主意。对我的问题有什么想法吗?wlan0
接口真的启动了吗?我相信NetworkInterface.getNetworkInterfaces()将不包括任何已关闭的接口。是的,wlan0接口已打开。@VGR最后,当我首先将wlan0连接到特定的wifi网络,然后使用该.getnetworkintefrece扫描可用的接口时,它才起作用。
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..");
}
}