在页面获取本地电脑IP

Posted lijianda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在页面获取本地电脑IP相关的知识,希望对你有一定的参考价值。

 

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    ResourceBundle resource = ResourceBundle.getBundle("config");
    String ctx = request.getContextPath();
    request.setAttribute("ctx", ctx);
%>
<!-- <html lang="en" style="min-height: 100%;overflow: hidden">
<head lang="zh-cn">
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <meta content="MSHTML 6.00.2800.1106" name="GENERATOR">
</head>
<body> -->
    
    <!--<form id="formfoo" name="formbar" action="#" method="post" style="display:none">
        <input value="00:05:5D:0E:C7:FA" name="txtMACAddr">
        <input value="192.168.30.68" name="txtIPAddr">
        <input value="lzf" name="txtDNSName">
    </form>-->
<!-- </body>
</html> -->

<!--[if !IE]> -->
<!-- <![endif]-->

<script type="text/javascript">

$(function () 
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1;
    if (userAgent.indexOf("Chrome") > -1)
        gooleGetIP();
        return "Chrome";
    
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) 
        ieGetIP();
        return "IE";
    ; 
);
function gooleGetIP()
    function getUserIP(onNewIP)  //  onNewIp - your listener function for new IPs
          //compatibility for firefox and chrome
          var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
          var pc = new myPeerConnection(
             iceServers: []
         ),
         noop = function() ,
         localIPs = ,
         ipRegex = /([0-9]1,3(\.[0-9]1,3)3|[a-f0-9]1,4(:[a-f0-9]1,4)7)/g,
         key;
     
         function iterateIP(ip) 
             if (!localIPs[ip]) onNewIP(ip);
             localIPs[ip] = true;
        
     
          //create a bogus data channel
         pc.createDataChannel("");
     
         // create offer and set local description
         pc.createOffer().then(function(sdp) 
             sdp.sdp.split(‘\n‘).forEach(function(line) 
                 if (line.indexOf(‘candidate‘) < 0) return;
                 line.match(ipRegex).forEach(iterateIP);
             );
             
             pc.setLocalDescription(sdp, noop, noop);
         );
     
         //sten for candidate events
         pc.onicecandidate = function(ice) 
             if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
             ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
         ;
    
     
    // Usage
     
    getUserIP(function(ip)
        //alert("Got IP! :" + ip);
        $.ajax(
            url:"$ctx/system/saveUserLoginIP",
            data:loginIp:ip,
            type:"post",
            dataType:"json",
            async:false, //同步
            success:function(data)
                console.log(data.data);
            ,
            error:function(data)
                alert(data);
            
        );
    ); 
;

function ieGetIP() 


</script>


<object id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT></object>
<object id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223></object>

<script language="JScript">
    var service = locator.ConnectServer();
    var MACAddr;
    var IPAddr;
    var DomainAddr;
    var sDNSName;
    service.Security_.ImpersonationLevel = 3;
    service.InstancesOfAsync(foo, ‘Win32_NetworkAdapterConfiguration‘);
</script>
<script language="JScript" event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for="foo">
   /*  document.forms[0].txtMACAddr.value = unescape(MACAddr);
    document.all.txtIPAddr.value = unescape(IPAddr);
    document.forms[0].txtDNSName.value = unescape(sDNSName); */
    //alert(unescape(IPAddr));
    $.ajax(
        url:"$ctx/system/saveUserLoginIP",
        data:loginIp:unescape(IPAddr),
        type:"post",
        dataType:"json",
        async:false, //同步
        success:function(data)
            console.log(data.data);
        ,
        error:function(data)
            alert(data);
        
    );
</script>
<script language="JScript" event="OnObjectReady(objObject,objAsyncContext)" for="foo">
    if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true) 
        if(objObject.MACAddress != null && objObject.MACAddress != "undefined") 
            MACAddr = objObject.MACAddress;
        
        if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined") 
            IPAddr = objObject.IPAddress(0);
        
        if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined") 
            sDNSName = objObject.DNSHostName;
        
    
</script>

 

以上是关于在页面获取本地电脑IP的主要内容,如果未能解决你的问题,请参考以下文章

自动获取IP是时。显示本地连接无访问权限

浏览器无法跳转到路由器设置页面本地连接显示已经连接

电脑为何不能自动获取IP地址?

java工具类,在Windows,Linux系统获取电脑的MAC地址本地IP电脑名

获取本地终端电脑有线和无线的IP

怎么把家里的本地连接地址类型设置成通过DHCP指派