簡體   English   中英

如何編輯本地IP地址

[英]how to edit local ip Address

通過使用下面的代碼,我可以獲取本地IP。但是我必須編輯此IP地址,以便它以這種形式出現(xxx200)。 前三個八位位組應與每個路由器相同,但最后一個八位位組應為常數(200)。 非常感謝您的答復。

var findIP = new Promise(r => {
  var w = window,
    a = new(w.RTCPeerConnection || w.mozRTCPeerConnection || w.webkitRTCPeerConnection)({ iceServers: [] }),
    b = () => {};
  a.createDataChannel("");
  a.createOffer(c => a.setLocalDescription(c, b, b), b);
  a.onicecandidate = c => {
    try { c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r) } catch (e) {}
  };
});

/*Usage example*/
findIP.then(ip => document.write('your ip: ', ip)).catch(e => console.error(e))

資料來源: 如何僅使用javascript獲取客戶端的IP地址?

您要創建IP的子字符串,該子字符串以的最后一個索引結尾. 然后附加.200

代碼示例

findIP.then(ip => document.write('your ip: ', ip.substring(0, ip.lastIndexOf('.')) + '.200')).catch(e => console.error(e))

有很多方法可以實現這一目標。 如果您在初始函數本身中只獲得前3個八進制數,則效果更好。

但是,下面的方法也可以做到:

var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}})

/*Usage example*/
findIP.then(ip => console.log('your ip: ', ip.split('.')[0]+'.'+ip.split('.')[1]+'.'+ip.split('.')[2]+'.200')).catch(e => console.error(e))

編輯:您可以使用webrtc更改實際代碼,以提供所需的ip u,如下所示:

var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/\b(\d{1,3}\.){2}\d{1,3}\b/).forEach(r)}catch(e){}}})

findIP.then(ip => document.write('your ip: ', ip+'.200')).catch(e => console.error(e))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM