Java obtains computer parameters [IPvsMAC]
Import java. io. bufferedReader; import java. io. inputStream; import java. io. inputStreamReader; import java.net. inetAddress; import java.net. networkInterface; import java. util. arrayList; import java. util. formatter; import java. util. list; import java. util. locale; import java. util. map; import java. util. properties; public class SystemUtils {// gets the computer configuration information (poor) by intercepting the cmd stream. public static List
GetCmdIpconfigInfo () {Process p = null; List
Address = new ArrayList
(); Try {p = new ProcessBuilder ("ipconfig", "/all "). start () ;} catch (Exception e) {return address;} StringBuffer sb = new StringBuffer (); // read the process output value InputStream inputStream = p. getInputStream (); BufferedReader br = new BufferedReader (new InputStreamReader (inputStream); String s = ""; try {while (s = br. readLine ())! = Null) {sb. append (s + "\ n") ;}} catch (Exception e) {e. printStackTrace ();} finally {try {inputStream. close ();} catch (Exception e) {e. printStackTrace () ;}} System. out. println (sb); return address;} // obtain the computer information, including the IP address and mac address public static void getSystemInfo () {try {InetAddress address = InetAddress. getLocalHost (); NetworkInterface ni = NetworkInterface. getByInetAddress (address); String ho StName = address. getHostName (). toString (); // obtain the name of the Local Computer. Properties props = System. getProperties (); String osname = props. getProperty ("OS. name "); String osversion = props. getProperty ("OS. version "); byte [] mac = ni. getHardwareAddress (); String sIP = address. getHostAddress (); String sMAC = ""; Formatter formatter = new Formatter (); for (int I = 0; I <mac. length; I ++) {sMAC = formatter. format (Locale. GetDefault (), "% 02X % s", mac [I], (I <mac. length-1 )? "-":""). ToString ();} System. out. println ("local IP:" + sIP); System. out. println ("Local MAC:" + sMAC); System. out. println ("local name:" + hostName); System. out. println ("operating System name:" + osname); System. out. println ("operating system version:" + osversion); Map
Map = System. getenv (); System. out. println ("username:" + map. get ("USERNAME"); // get the user name System. out. println ("computer name:" + map. get ("COMPUTERNAME"); // obtain the computer name System. out. println ("computer domain name:" + map. get ("USERDOMAIN"); // get computer domain name} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {getCmdIpconfigInfo (); System. out. println ("===================="); getSystemInfo ();}}
Running result
Windows IP Configuration Host Name ............: SD-20150114OHCZ Primary Dns Suffix .......: Node Type ............: Hybrid IP Routing Enabled ........: No WINS Proxy Enabled ........: NoWireless LAN adapter wireless network Connection: Connection-specific DNS Suffix.: Description ...........: 802.11n USB Wireless LAN Card Physical Address .........: 00-87-36-03-B3-F9 DHCP Enabled ...........: No Autoconfiguration Enabled ....: Yes Link-local IPv6 Address .....: fe80: 7927: 84a: 4c17: aeb2 % 15 (Preferred) IPv4 Address ...........: 172.24.187.1 (Preferred) Subnet Mask ...........: 255.255.0.0 Default Gateway .........: DHCPv6 IAID ...........: 268470070 DHCPv6 Client DUID ........: 00-01-00-01-1C-47-B7-2D-B4-B5-2F-AE-AE-DA DNS Servers ...........: fec0: 0: 0: ffff: 1% 1 fec0: 0: 0: ffff :: 2% 1 fec0: 0: 0: ffff :: 3% 1 NetBIOS over Tcpip ........: EnabledEthernet adapter local Connection: Connection-specific DNS Suffix.: Description ...........: Realtek PCIe GBE Family Controller Physical Address .........: D8-D3-85-10-CA-9E DHCP Enabled ...........: No Autoconfiguration Enabled ....: Yes IPv6 Address ...........: 2001: 250: 1006: 5072: 3d15: fa6a: 8397: 1b02 (Preferred) Temporary IPv6 Address ......: 2001: 250: 1006: 5072: 3c02: dc92: 42f6: 1da0 (Preferred) Link-local IPv6 Address .....: fe80: 3d15: fa6a: 8397: 1b02% 12 (Preferred) IPv4 Address ...........: 192.168.0.83 (Preferred) Subnet Mask ...........: 255.255.255.0 Default Gateway .........: fe80: 223: 89ff: fe1a: 69a % 12 192.168.0.1 DNS Servers ...........: 61.134.1.4 8.8.8.4 NetBIOS over Tcpip ........: EnabledEthernet adapter VirtualBox Host-Only Network: Connection-specific DNS Suffix.: Description ...........: VirtualBox Host-Only Ethernet Adapter Physical Address .........: 08-00-27-00-10-4B DHCP Enabled ...........: No Autoconfiguration Enabled ....: Yes Link-local IPv6 Address .....: fe80: c1e7: db1f: ce48: 1bbe % 18 (Preferred) IPv4 Address ...........: 192.168.56.1 (Preferred) Subnet Mask ...........: 255.255.255.0 Default Gateway .........: DHCPv6 IAID ...........: 369623079 DHCPv6 Client DUID ........: 00-01-00-01-1C-47-B7-2D-B4-B5-2F-AE-AE-DA DNS Servers ...........: fec0: 0: 0: ffff: 1% 1 fec0: 0: 0: ffff :: 2% 1 fec0: 0: 0: ffff :: 3% 1 NetBIOS over Tcpip ........: EnabledTunnel adapter isatap. {7fc65ef3-4b94-00004-8f98-2611d93d4853}: Media State ...........: Media disconnected Connection-specific DNS Suffix.: Description ...........: Microsoft ISATAP Adapter Physical Address .........: 00-00-00-00-00-00-00-E0 DHCP Enabled ...........: No Autoconfiguration Enabled ....: YesTunnel adapter local connection * 8: Media State ...........: Media disconnected Connection-specific DNS Suffix.: Description ...........: Microsoft Teredo Tunneling Adapter Physical Address .........: 00-00-00-00-00-00-00-E0 DHCP Enabled ...........: No Autoconfiguration Enabled ....: YesTunnel adapter isatap. {46B2F22B-8DC1-4604-AA54-A01C66B7C9A4}: Media State ...........: Media disconnected Connection-specific DNS Suffix.: Description ...........: Microsoft ISATAP Adapter #2 Physical Address .........: 00-00-00-00-00-00-00-E0 DHCP Enabled ...........: No Autoconfiguration Enabled ....: YesTunnel adapter isatap. {DB0446AD-4833-4B88-BCE1-DA18ECC8D756}: Media State ...........: Media disconnected Connection-specific DNS Suffix.: Description ...........: Microsoft ISATAP Adapter #3 Physical Address .........: 00-00-00-00-00-00-00-E0 DHCP Enabled ...........: No Autoconfiguration Enabled ....: Yes ================================ local IP Address: 192.168.0.83 local MAC: D8-D3-85-10-CA-9E local name: SD-20150114OHCZ operating system name: Windows 7 operating system version: 6.1 User name: Administrator computer name: SD-20150114OHCZ computer domain: SD-20150114OHCZ