import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
public class NetUtil {
public static String getIps(){
List<String> ips= new ArrayList<>();
try {
Enumeration<NetworkInterface> allNetInterfaces;
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint()) {
//just print
Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
InetAddress iaddr;
while (inetAddresses.hasMoreElements()) {
iaddr = inetAddresses.nextElement();
if (iaddr != null) {
//ips.add(iaddr.getHostAddress());
System.out.println("aa:" + iaddr.getHostAddress());
}
}
continue;
}
Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();
InetAddress iaddr;
while (inetAddresses.hasMoreElements()) {
iaddr = inetAddresses.nextElement();
if (iaddr != null) {
ips.add(iaddr.getHostAddress());
}
}
}
} catch (Exception ignored) {
}
Collections.sort(ips);
StringBuilder sb=new StringBuilder();
for(String mac:ips){
sb.append(mac);
}
return sb.toString();
}
public static String getMac(){
List<String> macs= new ArrayList<>();
try {
Enumeration<NetworkInterface> allNetInterfaces;
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
byte[] mac;
while (allNetInterfaces.hasMoreElements()) {
NetworkInterface netInterface = allNetInterfaces.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint()) {
mac = netInterface.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
if (sb.length() > 0) {
//macs.add(sb.toString());
System.out.println("aa:" + sb.toString());
}
}
continue;
}
mac = netInterface.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
if (sb.length() > 0) {
macs.add(sb.toString());
}
}
}
} catch (Exception ignored) {
}
Collections.sort(macs);
StringBuilder sb=new StringBuilder();
for(String mac:macs){
sb.append(mac);
}
return sb.toString();
}
public static void main(String[] args) {
String ret = getIps();
ret = getMac();
System.out.println(ret);
}
}