0

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

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

Community
  • 1
  • 1
umair
  • 3
  • 2
  • Caution : this "x.x.x.200" modification won't work with IPv6. – SR_ Sep 11 '16 at 08:53
  • Hem... The local IP cannot be changed from javascript. It is a datum set by a network administrator. Obviously you can get an String containing an IP Address and transform it to another string, but what for? You may use that String afterwards, but if you want the rest of client applications to get your transformed address when getting the local IP, the answer is no. – Little Santi Sep 11 '16 at 08:56
  • `ip.split('.').splice(0,3).concat('200').join('.')` – Jaromanda X Sep 11 '16 at 08:58
  • i have miniserver on arduino.i have to open website running on it. website IP address is in this pattern, to make it work on all types of routers. All i need is to get router IP address and browse that IP with 200 at its end. – umair Sep 11 '16 at 09:02
  • @JaromandaX can you please merge it in above code.and store new ip address in a variable. i have no idea of Javascript.Thanksa lot – umair Sep 11 '16 at 09:05
  • if you have no idea of javascript then why are you bothering? – Jaromanda X Sep 11 '16 at 09:06
  • change the final ip in `document.write('your ip: ', ip)` to the code I posted ... wont help, it just writes stuff to the document but doesn't actually change the ip – Jaromanda X Sep 11 '16 at 09:07
  • okay thank you man – umair Sep 11 '16 at 09:17

2 Answers2

0

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))
Community
  • 1
  • 1
Daniel
  • 10,641
  • 12
  • 47
  • 85
0

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))
Rajendra
  • 1,118
  • 15
  • 20