package com.thinkgem.jeesite.common.utils;

import com.google.common.collect.Lists;
import com.suyun.rfts.modules.constant.StringConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @Description:
 * @Author: leo.xiong
 * @CreateDate: 2019/07/30 09:15
 * @Email: leo.xiong@suyun360.com
 * @Version:
 */
public class PrintUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PrintUtil.class);

    public static List<InternetInfo> getIPAndHostNames() {
        List<InternetInfo> internetInfoList = Lists.newArrayList();
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        String readLine = null;
        try {
            Process process = Runtime.getRuntime().exec("arp -a");
            is = process.getInputStream();
            isr = new InputStreamReader(is, "GBK");
            br = new BufferedReader(isr);
            while ((readLine = br.readLine()) != null) {
                if (StringUtils.isNotEmpty(readLine)) {
                    if (readLine.contains("接口: ")) {
                        InternetInfo internetInfo = new InternetInfo();
                        internetInfo.setIP(readLine.split("接口: ")[1].trim().split(" --- ")[0].trim());
                        internetInfo.setType("动态");
                        internetInfo.setMac(getLocalMac());
                        internetInfoList.add(internetInfo);
                    } else if (readLine.contains("动态") || readLine.contains("静态")) {
                        InternetInfo internetInfo = new InternetInfo();
                        String[] IPInfos = null;
                        IPInfos = getIPInfos(readLine, IPInfos, 14);
                        String[] macInfos = IPInfos[1].split(" {5}");
                        internetInfo.setIP(IPInfos[0].trim());
                        internetInfo.setMac(macInfos[0].trim().replace(StringConstant.SPILTSTR_TRANSVERSE_LINE, StringConstant.DEFAULT_COLON));
                        internetInfo.setType(macInfos[1].trim());
                        internetInfoList.add(internetInfo);
                    }
                }
            }
        } catch (IOException e) {
            LOGGER.error("读取局域网信息异常", e);
        } finally {
            closeStream(is, isr, br);
        }
        return internetInfoList;
    }

    private static String getLocalMac() {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            Process process = Runtime.getRuntime().exec("ipconfig -all");
            is = process.getInputStream();
            isr = new InputStreamReader(is, "GBK");
            br = new BufferedReader(isr);
            String readLine = br.readLine();
            while (readLine != null) {
                readLine = new String(readLine.getBytes("GBK"), "GBK");
                if (readLine.contains("物理地址. . . . . . . . . . . . . :")) {
                    return readLine.split("物理地址. . . . . . . . . . . . . :")[1].trim().replace(StringConstant.SPILTSTR_TRANSVERSE_LINE, StringConstant.DEFAULT_COLON);
                }
                readLine = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeStream(is, isr, br);
        }
        return null;
    }

    private static void closeStream(InputStream is, InputStreamReader isr, BufferedReader br) {
        try {
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String[] getIPInfos(String readLine, String[] IPInfos, int symbolLen) {
        StringBuffer symbolSb = new StringBuffer();
        for (int i = 0; i < symbolLen; i++) {
            symbolSb.append(" ");
        }
        IPInfos = readLine.split(symbolSb.toString());
        if (IPInfos.length != 1) {
            return IPInfos;
        }
        return getIPInfos(readLine, IPInfos, --symbolLen);
    }

    public static String getHostNameByIP(String IP) {
        InetAddress addr = null;
        try {
            addr = InetAddress.getByName(IP);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return addr.getHostName();
    }

    public static class InternetInfo {
        private String hostName;
        private String IP;
        private String mac;
        private String type;
        private String port;

        public String getHostName() {
            return hostName;
        }

        public void setHostName(String hostName) {
            this.hostName = hostName;
        }

        public String getIP() {
            return IP;
        }

        public void setIP(String IP) {
            this.IP = IP;
        }

        public String getMac() {
            return mac;
        }

        public void setMac(String mac) {
            this.mac = mac;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port = port;
        }
    }
}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐