Java Get native MAC address/IP address/host name __java

Source: Internet
Author: User
Tags readline
Because of the previous mention of the "qa-qd-61-151" host name problem, so the Java to get the host name of things to learn, and in the way of the Java get MAC address/IP address and so on methods.
According to the two articles in resources, the following is integrated to enable Java to obtain the MAC address/IP address/host name function. The principle of implementation is as follows:
The IP address and host name are invoked directly in the Java.net.InetAddress class, while the MAC address distinguishes Windows and UNIX two operating systems, using "Ipconfig/all" and "Ifconfig eth0" respectively. command to get the output and then parse it to find the location of the MAC address. If the language of the operating system is different, the contents of the parsing also have differences; In addition, if the network card is not eth0, the corresponding changes need to be modified according to their actual application scenarios. The code is as follows:
Import Java.io.BufferedReader; Import java.io.IOException; Import Java.io.InputStreamReader; Import java.net.InetAddress; Import java.net.UnknownHostException;
/** * @className: Systemtool * @description: Some common tool methods related to the system. Currently implemented are: Get MAC address, IP address, host name * @author: Laugh all over the world * @createTime: 2010-11-13 PM 08:03:44/public class Systemtool {
/** * Gets the current operating system name. * Return operating system name such as: Windows Xp,linux. */public static String Getosname () {return System.getproperty ("Os.name"). Tolowerc ASE (); /** * Gets the MAC address of the UNIX network card. * Non-Windows systems call this method by default. If you have a special system, continue to expand the new MAC address method. * @return MAC Address */public static string Getunixmacaddress () {string mac = null; BufferedReader bufferedreader = null; Process process = NULL; try {process = Runtime.getruntime (). EXEC ("ifconfig eth0");//Linux commands, generally take eth0 as the local main network card display information contains MAC address information BufferedReader = New BufferedReader (InputStreamReader (process. getInputStream ())); String line = null; int index =-1; while (line = Bufferedreader.readline ())!= null) {index = Line.tolowercase (). IndexOf ("hwaddr");//Find the Marked string [HWADDR] if (i Ndex >= 0) {//Found Mac = line.substring (index + "hwaddr". Length () + 1). Trim ()//  Remove MAC address and remove 2-side space break;} catch (IOException e) {e.printstacktrace ();} finally {try {if (BufferedReader!= null) {Bufferedreader.close ();}} catch (IOException E1) {e1.printstacktrace ();} BuffEredreader = null; Process = NULL; }
return mac; }
/** * Gets the MAC address of the Widnows network card. * @return MAC Address */public static string Getwindowsmacaddress () {string mac = null; BufferedReader bufferedreader = null; Process process = NULL; try {process = Runtime.getruntime (). EXEC ("Ipconfig/all");//command under Windows, display information contains MAC address information BufferedReader = new BufferedReader (new InputStreamReader (process. getInputStream ())); String line = null; int index =-1; while (line = Bufferedreader.readline ())!= null) {index = Line.tolowercase (). IndexOf ("Physical Address");/find a marker string [ph Ysical Address] if (index >= 0) {//Found index = Line.indexof (":");//Find position of ":" if (index>=0) {mac = line.substring (i Ndex + 1). Trim ()//Remove MAC address and remove 2-side space} break; (IOException e) {e.printstacktrace ()};} finally {try {if (BufferedReader!= null) {bufferedreader.close (); } catch (IOException E1) {e1.printstacktrace ();} BufferedReader = null; Process = NULL; }
return mac; /** * @return Native Host name */public static String GetHostName () {inetaddress ia = null; try {ia = Inetaddress.getlocalhost ( );  catch (Unknownhostexception e) {//TODO auto-generated catch block E.printstacktrace ();} if (IA = = null) {return "some error ...";} else return ia.gethostname (); /** * @return Native IP address */public static String getipaddress () {inetaddress ia = null; try {ia = Inetaddress.getlocalho St ();  catch (Unknownhostexception e) {//TODO auto-generated catch block E.printstacktrace ();} if (IA = = null) {return "some error ...";} else return ia.gethostaddress (); }
/** * Test the Main method. * * @param argc * Run parameters. */public static void Main (string[] argc) {String OS = Getosname (); System.out.println ("OS Type:" +os); if (Os.startswith ("Windows")) {//Local is a Windows String mac = Getwindowsmacaddress (); System.out.println ("MAC address:" +mac);} else{//local non-Windows system is generally UNIX String mac = getunixmacaddress (); System.out.println (MAC); } System.out.println ("HostName:" +gethostname ());            System.out.println ("IPAddress:" +getipaddress ());} This simpler map<string, string> Map = system.getenv ();
String userName = Map.get ("UserName");//Get User name
String computerName = Map.get ("ComputerName");//Get Computer name
String UserDomain = Map.get ("UserDomain");//Get computer domain name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.