JavaScript獲取客戶端IP和MAC地址

  若是獲取不到則須要對IE瀏覽器進行設置。具體設置以下:javascript

請設置IE瀏覽器-工具-安全-自定義級別-ActiveX第三項‘對沒有標記爲安全的ActiveX控件進行初始化和腳本運行’爲啓用,點擊肯定後刷新從新登陸!html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JavaScript獲取客戶端IP</title>
<script type="text/javascript" language="javascript">
<!--
function GetLocalIPAddress()
{
    var obj = null;
    var rslt = "";
    try
    {
        obj = new ActiveXObject("rcbdyctl.Setting");
        rslt = obj.GetIPAddress;
        obj = null;
    }
    catch(e)
    {
        //異常發生
    }
    return rslt;
}

function getMac(){
      var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
      var service = locator.ConnectServer(".");
      var properties = service.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled =True");
      var e = new Enumerator (properties);
      {
            var p = e.item();
            var mac = p.MACAddress;
            return mac
      }
}
//-->

function init () {
    var ip = GetLocalIPAddress();
    var mac  = getMac();
    fm.clientIP.value  = ip;
    fm.clientMAC.value  = mac;
} 

</script>
</head>
<body onload="init();">
<form name = 'fm'>
        經過JavaScript獲取的IP信息:<input type ='text' name = 'clientIP'><br>
        經過JavaScript獲取的MAC信息:<input type='text'name  = 'clientMAC'> 
        經過JSP獲取的IP信息:<%= request.getRemoteAddr() %>
</form>
</body>
</html>
相關文章
相關標籤/搜索