How to know your public IP with a single command

From the erjaimer's blog is that I get this very useful tip.

Happens that erjaimer He explains to us how in a single line, we can know our public IP (real IP), but also invites anyone who wants to improve the line to do so ... and well ... they know that I am a big fan of bash, so I did the odd change. This is my line:

GET http://www.vermiip.es/  | grep "Tu IP p&uacute;blica es" | cut -d ':' -f2 | cut -d '<' -f1

Let's see a screenshot of the result we get with my changes:

His line is as follows:

GET http://www.vermiip.es/ | grep "Tu IP pública es" | perl -pe 's/(.*:)||\s+//g;'

And what would it look like using the original line:

The explanation of the original command is provided by the same erjaimer en Article:

  1. The command GET It is in any GNU / Linux distro and as its name indicates it downloads an HTML given a URL.
  2. The next step is to look at the HTML document to see what IP the page is showing us. In this second step we obtain this result: Your public IP is: xx.xx.xxx.xxx, where obviously the X's are numbers.
  3. The next step I will use regular expressions and PERL as a console command, filtering the letters, etc. perl -pe 's /(.* :) | (<\ / h2>) | (\ s +) // g; , this regex is made a little lightly, I propose to improve it or even using grep a little better it can be removed.

And here I leave the explanation of mine:

  1. Through GET We download the HTML of that URL, which tells us our IP and also a lot of more text that we do not want.
  2. We will clean all that unwanted code, leaving only the IP line through grep.
  3. The problem is that it shows us the IP with a small text and H2 tags, so now using twice cut we will clean everything and only leave the IP.
  4. (1st cut) »We clean the text and the 1st H2 tag simply saying that it does not show anything that is from the : (two points) to the left, leaving only an H2 tag at the end.
  5. (1st cut) »We clean this last H2 tag with the other cut, which will not show anything from the first <to its right. Remaining only the IP.

Any method (command) that you use will give you the same result, I simply wanted to make my contribution, because when seeing the command line with regular expressions and perl, it would be difficult to explain character by character to others haha.

Anyway, I hope you like it and a thousand thanks to erjaimer for having written the original article.

regards


29 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Rodolfo said

    If you do not have curl and if you have wget (eg my case in MacOSX) this command also works:

    wget -nv -q -O - http://ip.appspot.com/

    (although as Edwin says, ifconfig.me is the right thing to do ... and I installed curl on MacOSX)

  2.   Edwin said

    Easier
    curl ifconfig.me

    1.    KZKG ^ Gaara said

      O_O… I didn't know that site… WTF !, it's GREAT !!!
      Thanks for the data friend, super interesting hehe.

      PS: Now I feel like an idiot for using a whole chain of instructions when you managed to do it with 2 words HAHAHA

    2.    Ariel said

      EXCELLENT, just by installing curl and that command, it works great!

  3.   Santiago said

    Very good contribution! It is also very useful because it does not require anything extra installed. I tried CURL but it asks me to install the package.

    Greetings.

    1.    KZKG ^ Gaara said

      Ah, CURL does not come the same in the default system? O_O
      Well, thanks for the good contribution, you do what you can 🙂

      Greetings and thanks friend

  4.   lesterzone said

    WTF !! great explanation of (copy-paste, xD I have never written that nickname) KZKG ^ Gaara. Edwin, well, great, that method I did not know. Thanks for sharing.

    1.    KZKG ^ Gaara said

      hahaha thanks friend 🙂

  5.   alexander mora said

    Or with:
    curl -s http://ip.appspot.com/

  6.   oberost said

    As the saying goes, you won't go to bed without knowing one more thing

  7.   Sergio said

    Great that way, I didn't know about that service.

    To the author of this article, tell him that the background image in the terminal emulator makes reading difficult.

  8.   fraternal said

    It's great. However, this is easier:
    $curl ifconfig.me

    1.    KZKG ^ Gaara said

      HAHA yeah they already mentioned it above, I feel a little bad for having to use a whole chain of instructions hehehe

  9.   B1tblu3 said

    thank you, very useful

  10.   Algave said

    Good tip… (good to know).

  11.   davervozbox said

    Other methods:
    Know my Public IP:
    wget -qO- ifconfig.me/ip
    wget -qO- ifconfig.me/host

  12.   Hugo said

    Well, at least on Debian, you can alternatively use the default console browser:

    www-browser http://ifconfig.me

    I usually use elinks or w3m

  13.   proper said

    What complicated commands Gaara !! I use

    wget -qO- icanhazip.com

  14.   taregon said

    Hahaha, great.

  15.   Blazek said

    Very good, I did not know this way. You will never go to bed without learning anything new.

    1.    KZKG ^ Gaara said

      That's what I thought when I saw all the ways to do this haha

  16.   manolox said

    I have a similar one. The result is the same.
    wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

    If we put this in a script and add it to be displayed on the screen with zenity (eg), there is a very cool "application"

    The thing would be like this:
    #!/bin/sh

    IPPUBLICA=$(wget -q -O - checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')

    zenity --info --text="Tu IP es: $IPPUBLICA"

    regards

  17.   jorgenator2 said

    Neither wget is installed by default in some distributions, so curl is the most appropriate in this regard

    1.    KZKG ^ Gaara said

      curl is installed by default in all?

  18.   heavynethole said

    I do it like this, I have an alias xD:
    alias myip = 'wget http://automation.whatismyip.com/n09230945.asp -O - ​​2> / dev / null; threw out'

    But the CURL one left me speechless.

    Regards!

  19.   elwuilmer said

    I consider that there are many methods (ways) to obtain the public IP and that everyone is free to choose their own, with which they identify the most, I particularly like to use:

    wget -qO- ifconfig.me/ip

    A suggestion on my part is to edit the post and place each of the alternatives that exist to know the public IP and that the reader is free to choose the method to use.

  20.   Matias said

    Go to the "control panel" there and select "network connections" after opening it, right click on the connection that is working (this will be the ones with 2 blue pcs) and select "properties" in the new window that will open Click on "internet protocols" and then on the option "properties" there and you can see your IP address

    If you like me, answer me, vote me! https://es.answers.yahoo.com/question/index?qid=20080731112416AAVDKNz

  21.   Pablo said

    It was very helpful to find all the information they provide on the page. I was able to follow the steps and know my Public IP. On this other page http://www.gurugles.com/internet-y-computadoras/cual-es-mi-ip-publica/ . I also found many answers about the IP, very clear, for which we do not understand much about the subject.

  22.   Anonymous said

    curl is the maximum