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); } }