如何编辑本地IP地址 [英] how to edit local ip Address

查看:63
本文介绍了如何编辑本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用下面的代码,我可以获得本地IP。但我必须编辑此IP地址,以便它以这样的模式显示(x.x.x.200)。
前三个八位字节应与每个路由器相同,但最后一个应该是常量(200)。
您的回复将受到高度赞赏。

By using the code below i am able to get local IP.but i have to edit this IP address so that it appears in pattern like this( x.x.x.200 ). 1st three octet should be same as per router but last one should be constant(200). Your responses will be highly appreciated.

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地址?

推荐答案

有很多方法可以实现这一目标。如果你在初始函数期间只获得前3个octate,那就更好了。

There are many ways to achieve this. its better if you only get first 3 octate during your initial function itself.

但是下面也会做这个工作:

But following will also do the job:

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:

You can change actual code using webrtc to give the ip u needed as follows:

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))

这篇关于如何编辑本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆