js獲取客戶端內(nèi)網(wǎng)ip

<!--頁面定義一個隱藏域-->
<input type="hidden" name="docip" id="docip" value=""></input>
<script>
    //判斷瀏覽器
    if (myBrowser() == "IE") {
        var docip = GetAdapterInfo()
        document.getElementById('docip').value = docip
    }
    else {
        notIeGetIp()
    }

    //獲取瀏覽器信息
    function myBrowser() {
        var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串
        var isOpera = userAgent.indexOf("Opera") > -1; //判斷是否Opera瀏覽器
        var isIE = ((userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) || userAgent.indexOf(".NET") > -1) && !isOpera; //判斷是否IE瀏覽器
        var isEdge = userAgent.indexOf("Edge") > -1; //判斷是否IE的Edge瀏覽器
        var isFF = userAgent.indexOf("Firefox") > -1; //判斷是否Firefox瀏覽器
        var isSafari = userAgent.indexOf("Safari") > -1
            && userAgent.indexOf("Chrome") == -1; //判斷是否Safari瀏覽器
        var isChrome = userAgent.indexOf("Chrome") > -1
            && userAgent.indexOf("Safari") > -1; //判斷Chrome瀏覽器

        if (isIE) {
            return "IE";
        }
        if (isOpera) {
            return "Opera";
        }
        if (isEdge) {
            return "Edge";
        }
        if (isFF) {
            return "FF";
        }
        if (isSafari) {
            return "Safari";
        }
        if (isChrome) {
            return "Chrome";
        }

    }

    //IE獲取
    function GetAdapterInfo() {

        var locator = new ActiveXObject("WbemScripting.SWbemLocator");
        var service = locator.ConnectServer("."); //連接本機服務器
        var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");
        //查詢使用SQL標準
        var e = new Enumerator(properties);
        var msg = "";
        for (; !e.atEnd(); e.moveNext()) {
            var p = e.item();
            msg += p.IPAddress(0) + "|"
        }

        return msg.split('|')[0];

    }

    //Chrome內(nèi)核瀏覽器獲取
    function notIeGetIp() {
        var RTCPeerConnection = /*window.RTCPeerConnection ||62616964757a686964616fe4b893e5b19e31333365663465*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

        if (RTCPeerConnection) (function () {
            var rtc = new RTCPeerConnection({ iceServers: [] });
            if (1 || window.mozRTCPeerConnection) {      // FF [and now Chrome!] needs a channel/stream to proceed
                rtc.createDataChannel('', { reliable: false });
            };

            rtc.onicecandidate = function (evt) {
                // convert the candidate to SDP so we can run it through our general parser
                // see https://twitter.com/lancestout/status/525796175425720320 for details
                if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
            };
            rtc.createOffer(function (offerDesc) {
                grepSDP(offerDesc.sdp);
                rtc.setLocalDescription(offerDesc);
            }, function (e) { console.warn("offer failed", e); });


            var addrs = Object.create(null);
            addrs["0.0.0.0"] = false;
            function updateDisplay(newAddr) {
                if (newAddr in addrs) return;
                else addrs[newAddr] = true;
                var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
                document.getElementById('docip').value = displayAddrs.join(" or perhaps ") || "n/a";
            }

            function grepSDP(sdp) {
                var hosts = [];
                sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
                    if (~line.indexOf("a=candidate")) {     // http://tools.ietf.org/html/rfc4566#section-5.13
                        var parts = line.split(' '),        // http://tools.ietf.org/html/rfc5245#section-15.1
                            addr = parts[4],
                            type = parts[7];
                        if (type === 'host') updateDisplay(addr);
                    } else if (~line.indexOf("c=")) {       // http://tools.ietf.org/html/rfc4566#section-5.7
                        var parts = line.split(' '),
                            addr = parts[2];
                        updateDisplay(addr);
                    }
                });
            }
        })(); else {

            document.getElementById('docip').innerHTML = "<code>ifconfig | grep inet | grep -v inet6 | cut -d\" \" -f2 | tail -n1</code>";
            document.getElementById('docip').nextSibling.textContent = "In Chrome and Firefox your IP should display automatically, by the power of WebRTCskull.";
        }
    }
</script>

相關(guān)鏈接

http://www.itdecent.cn/p/8d7348bc84f4
https://zhidao.baidu.com/question/13759768.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容