简体   繁体   中英

how to edit local ip Address

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( xxx200 ). 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))

Source: How to get client's IP address using javascript only?

You want to create a substring of your IP that ends at the last index of . . Then append .200

Code example

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM