Java Get IP MAC address

Source: Internet
Author: User
Tags get ip locale

Package cn.aresoft.webapi.jiyu.utils;

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.HashMap;
Import java.util.List;
Import Java.util.Locale;
Import Java.util.Map;
Import java.util.Properties;
public class Osutil {

Computer configuration information is obtained by intercepting the Cmd stream (bad)
public static list<string> getipaddress () {
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 ();
Read 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;
}

public static void Getipconfig () {
map<string,string> map = system.getenv ();
System.out.println (Map.get ("USERNAME"));//Get User name
System.out.println (Map.get ("COMPUTERNAME"));//Get Computer name
System.out.println (Map.get ("UserDomain"));//Get computer domain name
}
Get the computer's IP address and MAC address
public static map<string,string> GetConfig () {
Map<string,string> map=new hashmap<string,string> ();
try{
InetAddress address = Inetaddress.getlocalhost ();
NetworkInterface ni = networkinterface.getbyinetaddress (address);
Ni.getinetaddresses (). Nextelement (). getaddress ();
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);
Map.put ("IP", SIP);
Map.put ("MAC", SMAC);

}catch (Exception e) {
E.printstacktrace ();
}
return map;
}
Get the computer's IP, name, operating system name, operating system version
public static void Config () {
try{
InetAddress addr = Inetaddress.getlocalhost ();
String ip=addr.gethostaddress (). toString (); Get the native IP
String hostname=addr.gethostname (). toString (); Get the native computer name
System.out.println ("Native IP:" +ip+ "\ n Native Name:" +hostname);
Properties props=system.getproperties ();
System.out.println ("Name of the operating system:" +props.getproperty ("Os.name"));
SYSTEM.OUT.PRINTLN ("OS Version:" +props.getproperty ("os.version"));
}catch (Exception e) {
E.printstacktrace ();
}
}


public static void Main (string[] args) {
GetConfig ();
}

}

Java Get IP MAC address

Related Article

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.