0

I want to get my Public IP address using Node js only

For example when i search on Google "What is my Ip address "

Gives me output like 10X.XX.XX.XXX

How to get this Ip address Using node js without importing any Libraries like ip , Public-ip etc ...

I want to write my Custom Code to get public ip Without sending requests to other websites like whatismyipaddress.com , ipconfig.com etc ...

VLAZ
  • 26,331
  • 9
  • 49
  • 67
tech lazy
  • 19
  • 5
  • 1
    You can check this question https://stackoverflow.com/q/20273128/5146848 – Sifat Haque Feb 06 '22 at 09:52
  • Does this answer your question? [How to get client's IP address using JavaScript?](https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript) – Steven Spungin Feb 06 '22 at 09:59
  • @StevenSpungin No , I want to write my Custom code to get Public Ip address – tech lazy Feb 06 '22 at 10:25
  • @SifatHaque using those method i am getting my wifi-->properties-->IPv4 address not my Public IP address which i can view when i search on google "what is my Ip address " – tech lazy Feb 06 '22 at 10:26
  • 1
    You need to connect to a remote site as stated. The URLS in that link do just that. Public IP is same as client IP, no? Do you want your domain name? – Steven Spungin Feb 06 '22 at 10:37

3 Answers3

4
const { exec } = require('child_process')

exec('curl ip-adresim.app', function(error, stdout, stderr){
    if(error)
        return;
    console.log('your ip is :'+ stdout);
})

you could run a command and get the output like curl or ping

enter image description here

Mohamed Fadli
  • 76
  • 1
  • 1
  • 7
1

There is no native way to get the public IP, in fact, there is no way to get it from your server, not even from the Linux command, you have to ask an external server, and this package does exactly so with different servers.

I understand you don't want to ask an external library or an external server, but you have no choice.

Im suggesting using an external library, because it takes care of asking multiple servers instead of just one in the case that one goes offline

You can try this package it works perfectly

Meddah Abdallah
  • 654
  • 1
  • 7
  • 25
  • 1
    @Meedah using which programming language it is possible to write my Custom code – tech lazy Feb 06 '22 at 10:27
  • I don't understand the question perfectly. You tagged this question nodejs, so I provided a code with nodeJs, but you can write it in any language you want – Meddah Abdallah Feb 06 '22 at 11:31
  • i mean to say if this is not possible with node.js , then wjich languages do whatismyipaddress.com , ipconfig.com etc use to get Public Ip address of user ? – tech lazy Feb 06 '22 at 15:40
  • 2
    it's not about the language, from your computer, you can't see your public ip, it's your router that knows it, when you contact another server ( which might run on nodejs ), they will know your public ip address because actually what's happening is that your computer sends a request to the router, which then sends it to the end server. That's why the end server can see your router's ip address ( your public ip ) and not your computer – Meddah Abdallah Feb 06 '22 at 17:11
  • 3
    No matter the language, you either have to read and see if your router permits you to read directly your public ip ( and I'm mentioning this case just to be thorough and not get flagged by another user on StackOverflow, but you probably don't need this ), or ask another servr to tell you what is your public ip – Meddah Abdallah Feb 06 '22 at 17:13
0

Here's a native JS request method:

fetch("https://ipecho.io/json")
  .then((response) => response.json())
  .then((data) => console.log(data.ip));

The access method encapsulated as a function is:

async function myip() {
  const res = await fetch("https://ipecho.io/json");
  const data = res.json();
  return data.ip;
}

const ip = await myip();
console.log(ip);
leaker
  • 1
  • 2