Java 擷取電腦參數[IPvsMAC]

來源:互聯網
上載者:User

標籤:java-ip   java-mac   電腦參數   

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. . . . . . . . : NoWireless 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. . . . . . . . : EnabledEthernet 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. . . . . . . . : 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-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 . . . . : YesTunnel 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 . . . . : 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=========================本機IP:192.168.0.83本機MAC:D8-D3-85-10-CA-9E本機名稱:SD-20150114OHCZ作業系統名稱:Windows 7作業系統版本:6.1使用者名稱:Administrator電腦名稱:SD-20150114OHCZ電腦網域名稱:SD-20150114OHCZ

Java 擷取電腦參數[IPvsMAC]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.