六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 38|回复: 0

用java程序获得IP地址和MAC地址

[复制链接]

升级  76%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
38
 楼主| 发表于 2013-1-28 19:26:39 | 显示全部楼层 |阅读模式
import java.net.InetAddress;import java.io.InputStream;import java.io.BufferedInputStream;import java.io.IOException;import java.text.ParseException;import java.util.StringTokenizer;public final class NetworkInfo {private final static String getMacAddress() throws IOException {String os = System.getProperty("os.name");try {if (os.startsWith("Windows")) {return windowsParseMacAddress(windowsRunIpConfigCommand());} else if (os.startsWith("Linux")) {return linuxParseMacAddress(linuxRunIfConfigCommand());} else {throw new IOException("unknown operating system: " + os);}} catch (ParseException ex) {ex.printStackTrace();throw new IOException(ex.getMessage());}}/* * Linux stuff */private final static String linuxParseMacAddress(String ipConfigResponse)throws ParseException {String localHost = null;try {localHost = InetAddress.getLocalHost().getHostAddress();} catch (java.net.UnknownHostException ex) {ex.printStackTrace();throw new ParseException(ex.getMessage(), 0);}StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");String lastMacAddress = null;while (tokenizer.hasMoreTokens()) {String line = tokenizer.nextToken().trim();boolean containsLocalHost = line.indexOf(localHost) >= 0;// see if line contains IP addressif (containsLocalHost && lastMacAddress != null) {return lastMacAddress;}// see if line contains MAC addressint macAddressPosition = line.indexOf("HWaddr");if (macAddressPosition <= 0)continue;String macAddressCandidate = line.substring(macAddressPosition + 6).trim();if (linuxIsMacAddress(macAddressCandidate)) {lastMacAddress = macAddressCandidate;continue;}}ParseException ex = new ParseException("cannot read MAC address for "+ localHost + " from [" + ipConfigResponse + "]", 0);ex.printStackTrace();throw ex;}private final static boolean linuxIsMacAddress(String macAddressCandidate) {// TODO: use a smart regular expressionif (macAddressCandidate.length() != 17)return false;return true;}private final static String linuxRunIfConfigCommand() throws IOException {Process p = Runtime.getRuntime().exec("ifconfig");InputStream stdoutStream = new BufferedInputStream(p.getInputStream());StringBuffer buffer = new StringBuffer();for (;;) {int c = stdoutStream.read();if (c == -1)break;buffer.append((char) c);}String outputText = buffer.toString();stdoutStream.close();return outputText;}/* * Windows stuff */private final static String windowsParseMacAddress(String ipConfigResponse)throws ParseException {String localHost = null;try {localHost = InetAddress.getLocalHost().getHostAddress();} catch (java.net.UnknownHostException ex) {ex.printStackTrace();throw new ParseException(ex.getMessage(), 0);}StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");String lastMacAddress = null;while (tokenizer.hasMoreTokens()) {String line = tokenizer.nextToken().trim();// see if line contains IP addressif (line.endsWith(localHost) && lastMacAddress != null) {return lastMacAddress;}// see if line contains MAC addressint macAddressPosition = line.indexOf(":");if (macAddressPosition <= 0)continue;String macAddressCandidate = line.substring(macAddressPosition + 1).trim();if (windowsIsMacAddress(macAddressCandidate)) {lastMacAddress = macAddressCandidate;continue;}}ParseException ex = new ParseException("cannot read MAC address from ["+ ipConfigResponse + "]", 0);ex.printStackTrace();throw ex;}private final static boolean windowsIsMacAddress(String macAddressCandidate) {// TODO: use a smart regular expressionif (macAddressCandidate.length() != 17)return false;return true;}private final static String windowsRunIpConfigCommand() throws IOException {Process p = Runtime.getRuntime().exec("ipconfig /all");InputStream stdoutStream = new BufferedInputStream(p.getInputStream());StringBuffer buffer = new StringBuffer();for (;;) {int c = stdoutStream.read();if (c == -1)break;buffer.append((char) c);}String outputText = buffer.toString();stdoutStream.close();return outputText;}/* * Main */public final static void main(String[] args) {try {System.out.println("Network infos");System.out.println(" Operating System: "+ System.getProperty("os.name"));System.out.println(" IP/Localhost: "+ InetAddress.getLocalHost().getHostAddress());System.out.println(" MAC Address: " + getMacAddress());} catch (Throwable t) {t.printStackTrace();}}} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表