Thursday, February 2, 2012

Get the public IP of your *nix box via CLI

If you need a quick and easy way to get the public facing IP address of your Unix/Linux server in runlevel 3 (no GUI, and therefore no browser), try this. This method is especially helpful if there is no command-line based browser installed and you don't feel like installing one. (Wget and Curl may be more likely to already be installed.)

Hints: Checkip.dyndns.com is possibly more reliable, but if you need a nicer format (just the IP only), try using icanhazip.com (lol) as shown below. Method 2b is perhaps the easiest and cleanest, but Method 1a is the most fool-proof.

UPDATE 11/15/2012: Scroll down to the bottom of this post for an even better Method 3!


Method 1a:


[~]$ wget http://checkip.dyndns.com -O myip.txt
--2012-02-02 13:26:43--  http://checkip.dyndns.com/
Resolving checkip.dyndns.com... 91.198.22.70, 216.146.38.70, 216.146.39.70 [These are not the IPs you want.]
Connecting to checkip.dyndns.com|91.198.22.70|:80... connected. [This is not the IP you want.]
HTTP request sent, awaiting response... 200 OK
Length: 107 [text/html]
Saving to: “myip.txt”

100%[===========================================================================================================>] 107         --.-K/s   in 0s     

2012-02-02 13:26:43 (33.2 MB/s) - “myip.txt” saved [107/107]

[~]$ cat myip.txt
<html><head><title>Current IP Check</title></head><body>Current IP Address: x.x.x.x</body></html>


Method 1b:

[~]$ wget http://icanhazip.com -O myip2.txt
--2012-02-02 13:27:07--  http://icanhazip.com/
Resolving icanhazip.com... 216.69.252.100, 2606:f200:0:7::baad:d00d [These are not the IPs you want.]
Connecting to icanhazip.com|216.69.252.100|:80... connected. [This is not the IP you want.]
HTTP request sent, awaiting response... 200 OK
Length: 16 [text/plain]
Saving to: “myip2.txt”

100%[===========================================================================================================>] 16          --.-K/s   in 0s     

2012-02-02 13:27:07 (1.34 MB/s) - “myip2.txt” saved [16/16]

[~]$ cat myip2.txt
x.x.x.x

Method 2a:


[~]$ curl http://checkip.dyndns.com
<html><head><title>Current IP Check</title></head><body>Current IP Address: x.x.x.x</body></html>

Method 2b:

[~]$ curl http://icanhazip.com
x.x.x.x



UPDATED METHOD 3 11/15/2012:
# wget -q -O - http://icanhazip.com
x.x.x.x
# wget -q -O - http://checkip.dyndns.com
<html><head><title>Current IP Check</title></head><body>Current IP Address: x.x.x.x</body></html>
#

No comments:

Post a Comment

Spammy/foul language comments or those with an explicit avatar will be tossed in a 55 gallon drum and a match thrown in after them. (Oooo, now I can warm my hands!!)