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 {
// 通过截取cmd流方式得到计算机的配置信息(不好)
public static List<String> getCmdIpconfigInfo() {
Process p = null;
List<String> address = new ArrayList<String>();
try {
p = new ProcessBuilder("ipconfig", "/all").start();
} catch (Exception e) {
return address;
}
StringBuffer sb = new StringBuffer();
// 读取进程输出值
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;
}
// 得到计算机的信息,包括ip地址和mac地址
public static void getSystemInfo() {
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
String hostName = address.getHostName().toString(); // 获取本机计算机名称
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("本机IP:" + sIP);
System.out.println("本机MAC:" + sMAC);
System.out.println("本机名称:" + hostName);
System.out.println("操作系统名称:" + osname);
System.out.println("操作系统版本:" + osversion);
Map<String, String> map = System.getenv();
System.out.println("用户名:" + map.get("USERNAME"));// 获取用户名
System.out.println("计算机名称:" + map.get("COMPUTERNAME"));// 获取计算机名
System.out.println("计算机域名:" + map.get("USERDOMAIN"));// 获取计算机域名
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
getCmdIpconfigInfo();
System.out.println("=========================");
getSystemInfo();
}
}
运行结果
Windows IP Configuration
Host Name . . . . . . . . . . . . : SD-20150114OHCZ
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Wireless LAN adapter 无线网络连接:
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. . . . . . . . : Enabled
Ethernet adapter 本地连接:
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. . . . . . . . : Enabled
Ethernet 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. . . . . . . . : Enabled
Tunnel adapter isatap.{7FC65EF3-4B94-4874-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 . . . . : Yes
Tunnel adapter 本地连接* 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 . . . . : Yes
Tunnel 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 . . . . : Yes
Tunnel 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
=========================
本机IP:192.168.0.83
本机MAC:D8-D3-85-10-CA-9E
本机名称:SD-20150114OHCZ
操作系统名称:Windows 7
操作系统版本:6.1
用户名:Administrator
计算机名称:SD-20150114OHCZ
计算机域名:SD-20150114OHCZ
时间: 2024-12-28 11:02:24