public class macaddress { public static void main(String[] args) { macaddress mdd = new macaddress(); String str=mdd.getMacOnWindow(); System.out.println(str); } private static String getMacOnWindow() { String s = ""; try { String s1 = "ipconfig /all"; Process process = Runtime.getRuntime().exec(s1); BufferedReader bufferedreader = new BufferedReader( new InputStreamReader(process.getInputStream())); String nextLine; for (String line = bufferedreader.readLine(); line != null; line = nextLine) { nextLine = bufferedreader.readLine(); if (line.indexOf("Physical Address") <= 0) { continue; } int i = line.indexOf("Physical Address") + 36; s = line.substring(i); break; }
然后利用流去解析结果
代码参考
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class macaddress {
public static void main(String[] args) {
macaddress mdd = new macaddress();
String str=mdd.getMacOnWindow();
System.out.println(str);
}
private static String getMacOnWindow() {
String s = "";
try {
String s1 = "ipconfig /all";
Process process = Runtime.getRuntime().exec(s1);
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String nextLine;
for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
nextLine = bufferedreader.readLine();
if (line.indexOf("Physical Address") <= 0) {
continue;
}
int i = line.indexOf("Physical Address") + 36;
s = line.substring(i);
break;
}
bufferedreader.close();
process.waitFor();
} catch (Exception exception) {
s = "";
}
return s.trim();
}
}
一,也可以用netstat -an获取命令端口,其中就有你的外网IP地址,然后在去解析结果
二,还可以调转到http://www.ip138.com/进行查询IP外网地址,然后抓取外网IP地址结果。。
所有的字符串,然后再自己subString出对自己有用的字符串
package featureTest;import java.net.*;
import java.io.*;public class IPTest
{
public static void main ( String[] args ) throws IOException
{
InetAddress[] inetAdds = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
for(int i = 0 ; i < inetAdds.length; i++){
System.out.print(inetAdds[i].getHostName()+ ":\t");
System.out.println(inetAdds[i].getHostAddress());
}
}
}
第二个就是dynamic ip
这个信息你是不可能通过自己主机的命令了解到的。网关也不会告诉你,除非你自己就是网关。需要外网的其他机器告诉你。比如你用浏览器访问www.ip138.com是,其网页会返回告诉你你在外网上真实的IP信息。那么你可以连接www.ip138.com,获取网页内容,截取关键字,得到本机在外网的IP.