没什么好说的,直接上java代码
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.net.InetAddress;
import java.util.Enumeration;
public static String getNetworkInfo() {
try {
List<NetworkInterface> infos = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface info: infos) {
if (!info.getName().equals("wlan0")) {
continue;
}
String ipAddr = "0.0.0.0";
Enumeration<InetAddress> addrs = info.getInetAddresses();
while (addrs.hasMoreElements()){
InetAddress addr = addrs.nextElement();
if(addr.toString().length() <= 16){
ipAddr = addr.toString().subSequence(1, addr.toString().length()).toString();
}
}
byte[] macBytes = info.getHardwareAddress();
List<String> macByteList = new ArrayList<>();
for (Byte byt: macBytes) {
macByteList.add(String.format("%02X", byt));
}
String macAddr = String.join(":", macByteList);
return macAddr + "&" + ipAddr;
}
} catch (Exception e) {
e.printStackTrace();
}
return "00:00:00:00:00:00&0.0.0.0";
}
|