1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Java获取局域网中所有ip和Mac地址

Java获取局域网中所有ip和Mac地址

时间:2020-08-03 17:10:39

相关推荐

Java获取局域网中所有ip和Mac地址

Java获取局域网中所有ip和Mac地址

定义一个Util

public class IpAndMacUtil {/*** 获取本机Mac地址* @param ia* @return* @throws SocketException*/private static volatile int num = 0;public static String getLocalMac(InetAddress ia) throws SocketException {//获取网卡,获取地址byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();String MAC="";for (int i = 0; i < mac.length; i++) {if (i != 0) {MAC += "-";}//字节转换为整数int temp = mac[i] & 0xff;String str = Integer.toHexString(temp);if (str.length() == 1) {MAC = MAC+"0"+str;} else {MAC += str;}}return MAC;}/*** 获得局域网所有的ip* @return* @throws IOException*/public static List<String> getAllIp() throws IOException {InetAddress host=InetAddress.getLocalHost();String hostAddress=host.getHostAddress();int pos=hostAddress.lastIndexOf(".");//获得本机ip的网段String bigNet=hostAddress.substring(0,pos+1);List<String> ips= Collections.synchronizedList(new ArrayList<>());for (int i=0;i<=225;i++){String ip=bigNet+i;new Thread(()->{try {Process process = Runtime.getRuntime().exec("ping "+ip+" -w 280 -n 1");InputStream inputStream=process.getInputStream();InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"GBK");BufferedReader bufferedReader=new BufferedReader(inputStreamReader);String line=null;String isActive=null;while((line=bufferedReader.readLine()) != null) {if(!line.equals("")){isActive=bufferedReader.readLine().substring(0,2);break;}}if(isActive.equals("来自")){ips.add(ip);}}catch (Exception e){e.printStackTrace();}num++;}).start();}while (num!=225) {}return ips;}/*** 根据ip,获取mac地址* @param ip* @return* @throws Exception*/public static String getMacAddress(String ip) throws Exception{String result = command("ping "+ip+" -n 2");if(result.contains("TTL")){result = command("arp -a "+ip);}String regExp = "([0-9A-Fa-f]{2})([-:][0-9A-Fa-f]{2}){5}";Pattern pattern = pile(regExp);Matcher matcher = pattern.matcher(result);StringBuilder mac = new StringBuilder();while (matcher.find()) {String temp = matcher.group();mac.append(temp);}return mac.toString();}private static String command(String cmd) throws Exception{Process process = Runtime.getRuntime().exec(cmd);process.waitFor();InputStream in = process.getInputStream();StringBuilder result = new StringBuilder();byte[] data = new byte[256];while(in.read(data) != -1){String encoding = System.getProperty("sun.jnu.encoding");result.append(new String(data,encoding));}return result.toString();}}

service调用util

public List<PadMACReq> queryMAC(){List<PadMACReq> padMACReq = new ArrayList<>();try {//获取本机ip,MAC地址InetAddress ia = InetAddress.getLocalHost();String ip=ia.toString().split("/")[1];PadMACReq localMAC = new PadMACReq(ip, IpAndMacUtil.getLocalMac(ia));padMACReq.add(localMAC);//获取局域网ip,MAC地址List<String> allIp = IpAndMacUtil.getAllIp();for(int i = 0 ; i < allIp.size() ; i++){String macAddress = IpAndMacUtil.getMacAddress(allIp.get(i));if(macAddress.isEmpty()){continue;}PadMACReq macAndIp = new PadMACReq(allIp.get(i),macAddress);padMACReq.add(macAndIp);System.out.println("正在获取ip,MAC地址");}} catch (Exception e) {e.printStackTrace();}return padMACReq;}

返回类

public class PadMACReq {/** 终端名称ip*/private String name;/** mac地址*/private String mac;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。