• 欢迎访问本网站,技术教程,资源分享,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入东蓬莱!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏东部蓬莱吧

【Java】使用Runtime执行cmd命令行的方式获取网卡参数

后台开发 XiaoWang 1年前 (2020-06-19) 744次浏览 0个评论 扫描二维码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PC_Address {
    /* 构造方法 */
    public PC_Address(){ }

    /* 获得网卡物理地址 */
    public static String getMACAddress() {
        String address = "";
        String os = System.getProperty( "os.name" );
        /* String ip = System.getProperty("os.ip"); */
        if ( os != null && os.startsWith( "Windows" ) )
        {
            try { String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec( command );
                BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
                String line;
                while ( (line = br.readLine() ) != null ) {
                    if ( line.indexOf( "Physical Address" ) > 0 ) {
                        int index = line.indexOf( ":" );
                        index += 2;
                        address = line.substring( index );
                        break;
                    }
                }
                br.close();
                return(address.trim() ); } catch ( IOException e ) {
            }
        }
        return(address);
    }
    /* 获得机器IP地址 */
    public static String getIPAddress() {
        String ipaddress = "";
        String os = System.getProperty( "os.name" );
        if ( os != null && os.startsWith( "Windows" ) )
        {
            try { String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec( command );
                BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
                String line;
                while ( (line = br.readLine() ) != null )
                {
                    if ( line.indexOf( "IP Address" ) > 0 )
                    {
                        int index = line.indexOf( ":" );
                        index += 2;
                        ipaddress = line.substring( index );
                        break;
                    }
                }
                br.close();
                return(ipaddress.trim() ); } catch ( IOException e ) { }
        }
        return(ipaddress);
    }
    /* 获得机器子网掩码 */
    public static String getSubnetMask() {
        String SubnetMask = "";
        String os = System.getProperty( "os.name" );
        if ( os != null && os.startsWith( "Windows" ) )
        {
            try { String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec( command );
                BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
                String line;
                while ( (line = br.readLine() ) != null )
                {
                    if ( line.indexOf( "Subnet Mask" ) > 0 )
                    {
                        int index = line.indexOf( ":" );
                        index += 2;
                        SubnetMask = line.substring( index );
                        break;
                    }
                }
                br.close();
                return(SubnetMask.trim() ); } catch ( IOException e ) { }
        }
        return(SubnetMask);
    }
    /* 获得机器默认网关 */
    public static String getDefaultGateway() {
        String DefaultGateway = "";
        String os = System.getProperty( "os.name" );
        if ( os != null && os.startsWith( "Windows" ) )
        {
            try { String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec( command );
                BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
                String line;
                while ( (line = br.readLine() ) != null )
                {
                    if ( line.indexOf( "Default Gateway" ) > 0 )
                    {
                        int index = line.indexOf( ":" );
                        index += 2;
                        DefaultGateway = line.substring( index );
                        break;
                    }
                }
                br.close();
                return(DefaultGateway.trim() ); } catch ( IOException e ) {
            }
        }
        return(DefaultGateway);
    }
    /* 获得DNS */
    public static String getDNSServers() {
        String DNSServers = "";
        String os = System.getProperty( "os.name" );
        if ( os != null && os.startsWith( "Windows" ) ) {
            try { String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec( command );
                BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
                String line;
                while ( (line = br.readLine() ) != null )
                {
                    if ( line.indexOf( "DNS Servers" ) > 0 )
                    {
                        int index = line.indexOf( ":" );
                        index += 2;
                        DNSServers = line.substring( index );
                        break;
                    }
                }
                br.close();
                return(DNSServers.trim() ); } catch ( IOException e ) {
            }
        }
        return(DNSServers);
    }

    public static String getConnectName(){
        String name = "";
        String os = System.getProperty( "os.name" );
        if ( os != null && os.startsWith( "Windows" ) ) {
            try {
                String  command = "cmd.exe /c netsh interface show interface|more +3";
                Process p = Runtime.getRuntime().exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));
                String line;
                while ((line = br.readLine()) != null) {
                    if (!line.contains("VMware") && !line.contains("WLAN") && line.length() > 0) {
                        int i = line.lastIndexOf("  ");
                        String substring = line.substring(i).trim();
                        name = substring;
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return name;
    }
    /* 主函数测试 */
    public static void main( String args[] ) {
        String address = PC_Address.getMACAddress();
        String ipaddress = PC_Address.getIPAddress();
        String SubnetMask = PC_Address.getSubnetMask();
        String DefaultGateway = PC_Address.getDefaultGateway();
        String DNSServers = PC_Address.getDNSServers();
        System.out.println( "机器IP地址:" + ipaddress );
        System.out.println( "网卡MAC地址:" + address );
        System.out.println( "子网掩码:" + SubnetMask );
        System.out.println( "默认网关:" + DefaultGateway );
        System.out.println( "主DNS服务器:" + DNSServers );
        PC_Address.getConnectName();
        //获取环境变量中tomcat(CATALINA_HOME)的路径
        String home = System.getenv("CATALINA_HOME");
        //获取用户目录
        String sysPath = System.getProperty("user.dir");
        System.out.println(home);
        System.out.println(sysPath);
    }
}

东部蓬莱 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:【Java】使用Runtime执行cmd命令行的方式获取网卡参数
喜欢 (0)
[支付宝]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址