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