js 获取mac地址和IP地址

1. js 获取mac地址

function MacInfo(){
          
   
      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;
           alert(mac)
      }
}

2.获取本机IP地址

function getIPKey(infoObj) {
          
   
    var that = this;
    if (window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection) {
          
   
        var Rtc = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
        try {
          
   
            var nrtc = new Rtc({
          
   
                iceServers: []
            });
            nrtc.createDataChannel(, {
          
   
                reliable: !1
            });
        } catch (e) {
          
   
            infoObj[ip] = 0.0.0.0;
            return infoObj;
        }

        var e = false;
        nrtc.onicecandidate = function(Rtc) {
          
   
            var reg = /([0-9]{1,3}(.[0-9]{1,3}){3})/,
                infoArr = [];
            complete != Rtc.target.iceGatheringState || e || (e = !0,
                Rtc.target.localDescription.sdp.split(
).forEach(function(a) {
          
   
                    if ((a = reg.exec(a)) && 127.0.0.1 != a[1] && 0.0.0.0 != a[1] && -1 === infoArr.indexOf(a[1])) {
          
   
                        infoArr.push(a[1]);
                    }
                }),
                (function() {
          
   
                    window.currentIp = 0 < infoArr.length ? infoArr.sort()[0] : 0.0.0.0;
                    infoObj.ip = window.currentIp;
                })());
        };

        nrtc.createOffer(function(a) {
          
   
            nrtc.setLocalDescription(a, that.F(), that.F());
        }, that.F());

        infoObj.ip = window.currentIp || 0.0.0.0;

        return infoObj;
    } else {
          
   
        infoObj[ip] = 0.0.0.0;

        return infoObj;
    }
}

3. 获取外网IP

引入js <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>

//获取外网ip方式1:搜狐
    function getIpBysouhu(){
          
   
        console.log("---cip---"+returnCitySN["cip"]);
        console.log("---cid---"+returnCitySN["cid"]);
        console.log("---cname---"+returnCitySN["cname"]);
    }
    //获取外网ip方式2:ipify
    function getIpByIpify(){
          
   
        $.ajax({
          
   
            url : "https://api.ipify.org/?format=json",
            type : "GET",
            data : {
          
   },
            dataType : "json",
            success : function(result) {
          
   
                console.log("---ip---"+result.ip);
            },
            error : function(){
          
   
                 
            }
        });
    }
经验分享 程序员 微信小程序 职场和发展