现在又怎么一个需求
用java去读取liunx的网口
一般读取出来都是etc0 , ect1....
可是服务器的网口是多个 绑定成了一个逻辑网口(bond)
小弟 我对liunx不是很了解 看api也没发现怎么去读取bond这样的网口 有知道的大神 麻烦告诉咱下 小弟在这里谢谢了啊..
用java去读取liunx的网口
一般读取出来都是etc0 , ect1....
可是服务器的网口是多个 绑定成了一个逻辑网口(bond)
小弟 我对liunx不是很了解 看api也没发现怎么去读取bond这样的网口 有知道的大神 麻烦告诉咱下 小弟在这里谢谢了啊..
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Formatter;
import java.util.List;
import java.util.zip.CRC32;public class GetMACAddress { public static void main(String[] args) {
System.out.println(calculateMacIdentifier());
} private final static String calculateMacIdentifier() {
List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
try {
interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); Collections.sort(interfaces, new Comparator<NetworkInterface>() {
@Override
public int compare(NetworkInterface o1, NetworkInterface o2) {
try {
if (o1.isLoopback() && !o2.isLoopback()) {
return 1;
}
if (!o1.isLoopback() && o2.isLoopback()) {
return -1;
}
} catch (SocketException e) {
return 0;
}
return o1.getName().compareTo(o2.getName());
}
}); for (NetworkInterface iface : interfaces) {
if (iface.getHardwareAddress() != null) {
// get the first not null hw address and CRC it
System.out.println("MAC:" + bytesToHexString(iface.getHardwareAddress()));
CRC32 crc = new CRC32();
crc.update(iface.getHardwareAddress());
return Long.toHexString(crc.getValue());
}
} if (interfaces.isEmpty()) {
}
return "";
} catch (SocketException e) {
return "";
}
} public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2); Formatter formatter = new Formatter(sb);
for (byte b : bytes) {
formatter.format("%02x", b);
} return sb.toString();
}
}
有没有懂网络的 麻烦告诉小白我一下呢
各网口的数据是通过shell命令获取的。
下面是bash#!/bin/bash
#netdev=eth0
#netdev=eth1
netdev=bond
grep $netdev: /proc/net/dev你可以先看看/proc/net/dev有没你要的网口(Ubuntu下物理网口、逻辑网口的信息都在这里)