`
wjt276
  • 浏览: 640183 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

java 根据IP获取客户端Mac地址

    博客分类:
  • Java
阅读更多
需要对用户的 ip 和 mac 地址进行验证,这里用到获取客户端ip和mac地址的两个方法,留存。

1.获取客户端ip地址( 这个必须从客户端传到后台):
   jsp页面下,很简单,request.getRemoteAddr() ;
   因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换
   public String getMyIP() {
        try {
            FacesContext fc = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
            return request.getRemoteAddr();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


2.获取客户端mac地址
    调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下:

   
public String getMACAddress(String ip){
        String str = "";
        String macAddress = "";
        try {
            Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
            InputStreamReader ir = new InputStreamReader(p.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
            for (int i = 1; i < 100; i++) {
                str = input.readLine();
                if (str != null) {
                    if (str.indexOf("MAC Address") > 1) {
                        macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return macAddress;
    }


完整代码:

import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.LineNumberReader;

public class GetMACAddress { 
public String getMACAddress(String ipAddress) { 
String str = "", strMAC = "", macAddress = ""; 
try { 
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); 
InputStreamReader ir = new InputStreamReader(pp.getInputStream()); 
LineNumberReader input = new LineNumberReader(ir); 
for (int i = 1; i < 100; i++) { 
str = input.readLine(); 
if (str != null) { 
if (str.indexOf("MAC Address") > 1) { 
strMAC = str.substring(str.indexOf("MAC Address") + 14, 
str.length()); 
break; 
} 
} 
} 
} catch (IOException ex) { 
return "Can't Get MAC Address!"; 
} 
// 
if (strMAC.length() < 17) { 
return "Error!"; 
}

macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) 
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) 
+ ":" + strMAC.substring(12, 14) + ":" 
+ strMAC.substring(15, 17); 
// 
return macAddress; 
}

public static void main(String[] args) { 
GetMACAddress getMACAddress = new GetMACAddress(); 
System.out.println(getMACAddress.getMACAddress("59.78.63.38")); //获得该ip地址的mac地址 
}

public static String procAll(String str) { 
return procStringEnd(procFirstMac(procAddress(str))); 
}

public static String procAddress(String str) { 
int indexof = str.indexOf("Physical Address"); 
if (indexof > 0) { 
return str.substring(indexof, str.length()); 
} 
return str; 
}

public static String procFirstMac(String str) { 
int indexof = str.indexOf(":"); 
if (indexof > 0) { 
return str.substring(indexof + 1, str.length()).trim(); 
} 
return str; 
}

public static String procStringEnd(String str) { 
int indexof = str.indexOf("\r"); 
if (indexof > 0) { 
return str.substring(0, indexof).trim(); 
} 
return str; 
} 
}

只要写一个servlet来调用这个类的getMACAddress(String netip)方法就可以获得客户端的mac地址了,相信你会写jsp应该对servlet也不陌生吧,在jsp中调用servlet通过session传递返回的mac地址,加以判断就可以了

mac地址是可以通过注册表修改的,不建议以此来作为限制依据~
分享到:
评论
4 楼 小普洱他老爸 2013-07-07  
在linux下可以通过arp ip 命令获取到mac地址

这个是我执行的例子:

arp 192.168.1.104
? (192.168.1.104) at 0:1c:42:7e:f:31 on en1 ifscope [ethernet]
3 楼 zhuchao_ko 2011-09-27  
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);

改成 e
2 楼 yangkai 2010-04-14  
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Getting MAC Address From Javascript(IE Only)</title>
 
<script language="javascript">
function showMacAddress(){
 
	var obj = new ActiveXObject("WbemScripting.SWbemLocator");
	var s = obj.ConnectServer(".");
	var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
	var e = new Enumerator (properties);

 
	var output;
	output='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
	output=output + '<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
	while(!e.atEnd())

	{
		e.moveNext();
		var p = e.item ();
		if(!p) continue;
		output=output + '<tr bgColor="#FFFFFF">';
		output=output + '<td>' + p.Caption; + '</td>';
		output=output + '<td>' + p.MACAddress + '</td>';
		output=output + '</tr>';
	}

	output=output + '</table>';
	document.getElementById("box").innerHTML=output;
}
</script>
 
</head>
<body>
	<input type="button" value="Show MAC Address" onclick="showMacAddress()" />

	<div id="box">
	</div>
</body>
</html>

The code are also works fine in IE only.
1 楼 gwpking8419 2010-04-12  
如果是linux ,代码能执行 ?

相关推荐

Global site tag (gtag.js) - Google Analytics