精华内容
下载资源
问答
  • js获取本机ip地址
    2021-07-19 11:55:45
    //IP地址获取方法
    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);
        }).catch(function (reason) {
            // An error occurred, so handle the failure to connect
        });
    
        //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);
        };
    }

    浏览器修改设置

    火狐

    • 输入about:config
    • 搜索media.peerconnection.ice.obfuscate_host_addresses,修改为false

    谷歌

    • 搜索chrome://flags/#enable-webrtc-hide-local-ips-with-mdns
    • Anonymize local IPs exposed by WebRTC置为disabled
    • 新版该方法不可用
    更多相关内容
  • javascript 获取当前本机 IP 地址

    千次阅读 2020-09-14 16:15:18
    const getIP = () => { const interfaces = os.networkInterfaces(); console.log('interfaces:', interfaces) for (let devName in interfaces) { const iface = interfaces[devName]; console.log('iface:...
    const os = require('os')
    const getIP = () => {
      const interfaces = os.networkInterfaces();
      console.log('interfaces:', interfaces)
      for (let devName in interfaces) {
        const iface = interfaces[devName];
        console.log('iface:', iface)
        for (let i = 0; i < iface.length; i++) {
          const alias = iface[i];
          console.log('alias:', alias)
          if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal && alias.netmask === '255.255.255.0') {
            return alias.address;
          }
        }
      }
    }

    os.networkInterfaces()   返回的内容:

    通过循环,判断出本机当前的IP

    展开全文
  • JS获取本机IP地址的方法

    千次阅读 2022-04-21 09:37:40
    1.获取本机IP地址方法1: if(typeofwindow!='undefined'){ varRTCPeerConnection=window.RTCPeerConnection||window.mozRTCPeerConnection||window.webkitRTCPeerConnection; if(RTCPeerConnection)(()=>{ ...

    1.获取本机IP地址方法1:

    if(typeof window != 'undefined'){

        var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;

        if (RTCPeerConnection) (()=>{

            var rtc = new RTCPeerConnection()

            rtc.createDataChannel(''); //创建一个可以发送任意数据的数据通道

            rtc.createOffer( offerDesc => { //创建并存储一个sdp数据

            rtc.setLocalDescription(offerDesc)

        }, e => { console.log(e)})

        rtc.onicecandidate =(evt) => { //监听candidate事件

            if (evt.candidate) {

                console.log('evt:',evt.candidate)

                let ip_rule = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/

                var ip_addr = ip_rule.exec(evt.candidate.candidate)[1]

                console.log('ip_addr:',ip_addr)   //打印获取的IP地址

            }}

        })()

        else{console.log("没有找到")}

    }

    2.获取本机IP地址方法2

       //获取用户本地ip的方法
    const getUserIP= (onNewIP)=> {
      let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
      let pc = new MyPeerConnection({
        iceServers: []
      });
      let noop = () => {
      };
      let localIPs = {};
      let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g;
      let iterateIP = (ip) => {
        if (!localIPs[ip]) onNewIP(ip);
        localIPs[ip] = true;
      };
      pc.createDataChannel('');
      pc.createOffer().then((sdp) => {
        sdp.sdp.split('\n').forEach(function (line) {
          if (line.indexOf('candidate') < 0) return;
          line.match(ipRegex).forEach(iterateIP);
        });
        pc.setLocalDescription(sdp, noop, noop);
      }).catch((reason) => {
      });
      pc.onicecandidate = (ice) => {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
      };
    }
    
    
    
    
      getUserIP((ip) => {
       state.ip=ip
          console.log(ip)
          console.log(state.ip)
      });

    如果电脑没获取到,基本上是因为浏览器限制了,解除方法如下:

    解决方案:

    • 火狐(FireFox) 删除隐藏IP

    浏览器输入 about:config

    搜索配置 media.peerconnection.enabled 改为false ( 刷新程序,IP正常显示 )

    • 谷歌(Chrome) 删除隐藏IP

    浏览器输入:chrome://flags/#enable-webrtc-hide-local-ips-with-mdns

    把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )

    • eage浏览器删除隐藏ip

    浏览器输入: edge://flags/#enable-webrtc-hide-local-ips-with-mdns

    把 Anonymize local IPs exposed by WebRTC 设置为 disabled ( 刷新程序,IP正常显示 )

    展开全文
  • –web 客户端JavaScript <!-- 调用方式 --> <body onload="checkCookie()"></body> function getYourIP() { const RTCPeerConnection = window.RTCPeerConnection || window....
  • 完整源代码: 代码如下: <!DOCTYPE html PUBLIC “-//W3C//DTD ... <head> <title>客户端外网IP-软件开发网</title>... 您的IP地址是:”keleyivisitorip”></span> <script type=”text/javascr
  • js 获取本机ip地址

    千次阅读 2019-03-21 13:19:54
    function getIPs(callback){ var ip_dups = {}; //compatibility for firefox and chrome var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || wind...
    function getIPs(callback){
        var ip_dups = {};
        //compatibility for firefox and chrome
        var RTCPeerConnection = window.RTCPeerConnection
            || window.mozRTCPeerConnection
            || window.webkitRTCPeerConnection;
        //bypass naive webrtc blocking
        if (!RTCPeerConnection) {
            var iframe = document.createElement('iframe');
            //invalidate content script
            iframe.sandbox = 'allow-same-origin';
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            var win = iframe.contentWindow;
            window.RTCPeerConnection = win.RTCPeerConnection;
            window.mozRTCPeerConnection = win.mozRTCPeerConnection;
            window.webkitRTCPeerConnection = win.webkitRTCPeerConnection;
            RTCPeerConnection = window.RTCPeerConnection
                || window.mozRTCPeerConnection
                || window.webkitRTCPeerConnection;
        }
        //minimal requirements for data connection
        var mediaConstraints = {
            optional: [{RtpDataChannels: true}]
        };
        //firefox already has a default stun server in about:config
        //    media.peerconnection.default_iceservers =
        //    [{"url": "stun:stun.services.mozilla.com"}]
        var servers = undefined;
        //add same stun server for chrome
        if(window.webkitRTCPeerConnection)
            servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
        //construct a new RTCPeerConnection
        var pc = new RTCPeerConnection(servers, mediaConstraints);
        //listen for candidate events
        pc.onicecandidate = function(ice){
            //skip non-candidate events
            if(ice.candidate){
                //match just the IP address
                var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
                var ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
                //remove duplicates
                if(ip_dups[ip_addr] === undefined)
                    callback(ip_addr);
                ip_dups[ip_addr] = true;
            }
        };
        //create a bogus data channel
        pc.createDataChannel("");
        //create an offer sdp
        pc.createOffer(function(result){
            //trigger the stun server request
            pc.setLocalDescription(result, function(){}, function(){});
        }, function(){});
    
    }

     

    展开全文
  • 前端js获取本机ip地址

    千次阅读 2020-03-09 18:00:44
    <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script> 调用时: var ip = returnCitySN["cip"]; 即可得到ip
  • script type="text/javascript"> /** * Get the user IP throught the webkitRTCPeerConnection * @param onNewIP {Function} listener function to expose the IP locally * @return undefined...
  • 记录一种js获取本机ip地址的方法

    千次阅读 2020-07-15 20:43:39
    首先一个获取当前客户端的IP地址的网址:http://net.ipcalf.com/ Chrome和Firefox浏览器会默认隐藏内网的IP地址,所以需要设置一些额外的东西才可以将IP地址显示出来 Chrome: 在Chrome浏览器地址栏中输入:chrome:/...
  • js中怎么取得本机ip地址/** * 获取本地IP地址 */ function getLocalIPAddress() { var obj = null; var rslt = "127.0.0.1"; try { obj = new ActiveXObject("rcbdyctl.Setting"); if (。isNull(obj.Get...
  • 今天在搞JSjavascript)获取客户端IP的小程序,上网搜了下,好多在现在的系统和浏览器中的都无效,很无奈,在Chrome、FireFox中很少搞到直接利用ActiveX获取IP等的JS脚本。下面的代码是我在所有windowsNT5.0及以上...
  • 主要介绍了nodejs获取本机内网和外网ip地址的实现代码,需要的朋友可以参考下
  • 本文实例讲述了JS简单获取客户端IP地址的方法。分享给大家供大家参考,具体如下: <!doctype html> <html> <head> <meta charset="utf-8"> <title>使用搜狐接口获取客户端IP地址</...
  • 通过js获取本机IP地址

    万次阅读 热门讨论 2019-05-10 10:05:10
    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...
  • JS获取计算机mac地址以及IP和用户名的实现方法,只适用于IE且activeX开启
  • 本文介绍下,js代码获取IP地址的三种方法,通过三个例子来了解如何在js中取得客户端的IP地址。有需要的朋友不妨作个参考
  • js获取IP地址的4种方法

    千次阅读 2021-08-02 11:17:41
    1、js取得IP地址的方法一 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script type="text/javascript"> document.write(returnCitySN["cip"]+','+returnCitySN["cname"]) <...
  • 本机是无法获取的,我建议通过调网上公共接口获取,给你一个我用过的例子。 //引一下sohu这个 //返回数据var returnCitySN = {“cip”: “117.132.58.6”, “cid”: “370200”, “cname”: “山东省青岛市”}; <...
  • javascript获取本地IP

    2013-11-05 20:47:25
    javascript获取本地IP
  • 通过js获取本地电脑ip

    2021-12-03 16:48:27
    } `对chrome来说,如果以上地址弹出的ip为undefined,请按以下步骤修改浏览器设置: 1.打开地址chrome://flags/ 2.搜索 #enable-webrtc-hide-local-ips-with-mdns 该配置 并将属性改为disabled 3.修改成功后重启...
  • 主要介绍了js获取客户端网卡的IP地址、MAC地址的方法,需要的朋友可以参考下
  • JS获取浏览器信息,JS获取操作系统,JS获取本机IP
  • 原理使用webRTC技术获取本地局域网IP地址,如:192.168.1.123。代码function getUserIP(onNewIP) {var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window....
  • JS获取ip地址归属地

    2013-04-09 09:33:10
    js调用第三方API获取ip归属地,简单方便实用,你一定很需要!
  • js获取本地IP地址

    2021-04-30 12:14:37
    js获取本地IP地址 /** * 获取本地ip地址 * @return {String} IP地址 */ function getIPAdress() { let interfaces = require('os').networkInterfaces(); for (var devName in interfaces) { var iface = ...
  • js获取ip地址

    2018-06-05 14:19:56
    js获取ip地址,是物理获取,不联网,不需要走后台,是用的一个空间获取的

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 106,104
精华内容 42,441
关键字:

js获取本机ip地址

友情链接: Example034.rar