I can find my IP address using ifconfig or hostname -i command.
But how do I find my Public IP?
(I have a static public IP but I want to find it out using unix command)
I can find my IP address using ifconfig or hostname -i command.
But how do I find my Public IP?
(I have a static public IP but I want to find it out using unix command)
curl ifconfig.me
curl ifconfig.me/ip
(for just the ip)
curl ifconfig.me/all
(for more info, takes time)
For more commands visit: http://ifconfig.me/#cli_wrap
You can request myip.opendns.com
. from OpenDNS. dig @208.67.222.220 myip.opendns.com
dig @ns1.google.com -t txt o-o.myaddr.l.google.com +short
dig -4 @ns1-1.akamaitech.net -t a whoami.akamai.net +short
dig -4 @resolver1.opendns.com -t a myip.opendns.com +short
Note that the above only works for IPv4 currently (none of these resolvers even seem to have IPv6 currently, but if you omit -4
and the explicit -t a
, then you risk it breaking down in the future (with the exception of Google's txt
, which might actually work for IPv6 one day, if properly enabled by Google)).
Note that myip.opendns.com
is only resolvable through resolver1.opendns.com
, and not with auth1.opendns.com
— so, they seem to be doing some DNS hijacking and man-in-the-middle of their own domain name! Thus, you cannot use it to find the IP of a random resolver, since resolver1.opendns.com
is not authoritative for myip.opendns.com
.
Note that o-o.myaddr.l.google.com
looks like the most flexible and future-proof approach; it's even good for testing whether your DNS resolver supports the experimental EDNS0 extension for client subnet (which very few resolvers have the support for):
% dig @8.8.8.8 -t txt o-o.myaddr.l.google.com +noall +answer +stats | tail -8
;; global options: printcmd
o-o.myaddr.l.google.com. 60 IN TXT "74.125.189.16"
o-o.myaddr.l.google.com. 60 IN TXT "edns0-client-subnet 88.198.54.0/24"
;; Query time: 13 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Dec 8 20:38:46 2013
;; MSG SIZE rcvd: 114
A very simple anwser if you have internet access is:
curl icanhazip.com
Bear in mind, trusting third party sources for your IP might be problematic especially if what your doing with that data has special meaning.
A more trustworthy way is to pick a known, trustworthy DNS server (ideally running DNSSEC) and query the hostname of the box with it, providing your DNS server contains such entries;
dig @trustworthysource.com +short `hostname`
I wrote a simple and fast webservice for this.
curl ident.me
You can ask for your IPv4:
curl v4.ident.me
Or IPv6:
curl v6.ident.me
It also supports HTTPS, DNS, SSH.
The API is documented on http://api.ident.me/
One way: http://www.whatismyip.com/
If
then you can just parse the output from ifconfig for the IP addresses of the interfaces (the "inet addr:" part) to get the list of IP addresses of all your interfaces. The one IP address that is not in the private range (see http://en.wikipedia.org/wiki/IP_address#IPv4_private_addresses ) is your public IP address.
The same list can also be obtained through
ip addr show
which may be easier to parse.
If you do not have a direct internet connection (NAT etc.), there is no way to find your public IP address without external help (since your computer does not know it). Then you'll have to do it like in the other answers.
I took a little different approach by using the STUN protocol which was designed for NAT Traversal. If you use Ubuntu you can just install the package "stun" by typing:
sudo apt-get install stun
The package installs a STUN server which you probably wont need, but it also comes with a STUN test client which I used to solve this problem. Now you can retrieve your public IP with one (not so simple) command:
stun -v stunserver.org 2>&1 1>/dev/null | grep MappedAddress | sed -e 's/.*MappedAddress = //' -e 's/:.*//' | uniq
Now, what it does is: stun contacts the public STUN server "stunserver.org" and gets an answer back with your public IP, the rest of the command is just to filter out the IP from the output.
Google now displays your public IP address: http://www.google.com/search?q=ip
Okay...I know that this is WAY after the fact and probably not even worth posting, but here's my working solution.
#!/bin/sh
IP="$(ifconfig | egrep 'inet ' | sed -e 's/inet //' -e 's/addr://' -e 's/ Bcast.*//' -e 's/127.*//')"
echo $IP
Nice and simple.
The simpliest way is to use http://ifconfig.me/, as suggested.
On this page, you will know what command to use for what information you want to retrieve.
For IP:
curl ifconfig.meor
curl ifconfig.me/ip
For public Hostname:
curl ifconfig.me/host
For all informations in a XML file:
curl ifconfig.me/all.xml
etc... just check out http://ifconfig.me
One way is , you can make a request to the page at
http://www.biranchi.com/ip.php
it returns the IP address of your system
I'm doing this a lot, and from a lot of devices, so I made my own two services on a server :
php file at the root of a webserver :
user@host:~$ cat index.php`
<?php echo $_SERVER['REMOTE_ADDR']; echo "\n" ?>
Usage on a shell:
$ curl -4 mydomain.com
79.22.192.12
also working with ipv6:
$ curl mydomain.com
2a01:e34:ee7d:180::3
With netcat:
$ echo "GET /" | nc myserver.com 80
2a01:e34:ee7d:180::3
On a Cisco router:
router#more http://myserver.com/index.php
79.22.192.12
quick hack with a custom telnet server: xinetd spawning /usr/bin/env:
service telnet
{
server = /usr/bin/env
socket_type = stream
protocol = tcp
flags = IPv6
wait = no
port = 23
cps = 3 30
passenv = %a
}
and then telnet to it :
$ nc -4 myserver.com 23
REMOTE_HOST=::ffff:79.22.192.12
$ nc myserver.com 23
REMOTE_HOST=2a01:e34:ee7d:180::3
works the same with a router:
router#telnet myserver.com
79.22.192.12
This way you can make it work on your internal network, if some nat or proxy is involved in the communication and you'd like to know from what IP you appear.
It doesn't require any third party service.
To avoid relying on external sources, I use expect, to telnet into my router and get the ip address of its public interface. Here's an example expect script:
#!/usr/bin/expect
if { $argc < 3 } {
puts "usage: ./telnet2router.exp router-ip username password"
return -1
}
set ip [lrange $argv 0 0]
set username [lrange $argv 1 1]
set password [lrange $argv 2 2]
spawn telnet $ip
expect "login:" {
send "$username\r"
}
expect "Password:" {
send "$password\r"
}
expect "#" {
send "ifconfig ppp0 | grep inet\r"
send "exit\r"
}
expect eof
I then execute the above script like this to get the public ip:
./telnet2router.exp <router-ip> <username> <password> | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1
Of course, this is based on the assumption that I have admin access to the router, and its a linux-based router, with ifconfig command available.
A simple shell script solution can be found here:
http://bash.cyberciti.biz/misc-shell/read-local-ip-address/
Works on Linux, FreeBSD, SunOS and Apple Darwin (with a minor modification).
If what you want is to find the external ip address on your router, you either ask the router itself for its wan address, or ask someone outside to get it for you..
for a manual way you can browse any of the above given sites that will return the ip of the incomming request.
For an automated way, you can try :
wget -q -O - http://www.ipaddressworld.com | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}'\.
which will get you the line that contains the ip address on the http response, then parse it out with sed, awk , etc
lynx --dump http://www.whatismyip.com/ | grep -o '[0-9].*\.[0-9].*\.[0-9].*\.[0-9].*' -m1
I do this. It just gives me the IP without any third-party involvement..
ip addr show | grep eth0 | grep inet | tr -s " " | cut -f3 -d " " | cut -f1 -d "/"
Here is another alternative that depends on hosts who's business resolves around managing dynamic IP rather that "public service" sites that may go away or change format.
1) Register your server at one of the many free dynamic dns services (e.g. no-ip.com) This will give you a DNS entry like xxx.no-ip.org.
2) Install the service's dynamic update tool (reports IP changes to service).
To get the IP address in a script, just do:
$external_ip = `dig +short xxx.no-ip.org`
Great for use in cron job to check if dynamic IP has changed and some configuration entries need to be changed.
On OS X here are two simple solutions to get both the private and public IP (with bonus code if you use LaunchBar).
$ ipconfig getifaddr $1
# $1=en0 || en1 || en*
#!/bin/sh
title="$USER@$(HOSTNAME -s)"
text=$(ipconfig getifaddr en1)
open "x-launchbar:large-type?font-name=TerminalDosis-Light&string=$text&title=$title"
$ dig +time=1 +tries=1 +retry=1 +short myip.opendns.com @resolver1.opendns.com
# ||
$ curl $1
# $1=http://wtfismyip.com/text || http://ip-addr.es || http://icanhazip.com || http://wgetip.com || http://ident.me || http://ifconfig.me || https://shtuff.it/myip/short || http://curlmyip.com
#!/bin/sh
title="$USER@$(HOSTNAME -s)"
text=$(dig +time=1 +tries=1 +retry=1 +short myip.opendns.com @resolver1.opendns.com)
open "x-launchbar:large-type?font-name=TerminalDosis-Light&string=$text&title=$title"
you can use just the shell to check your external ip, also using external providers
#!/bin/bash
TCP_HOST="checkmyip.com"
TCP_PORT=80
exec 5<>/dev/tcp/"${TCP_HOST}"/"${TCP_PORT}"
echo -e "GET / HTTP/1.0\nHOST:${TCP_HOST}\n" >&5
while read -r line
do
case "$line" in
*"Your local IP address is"* )
line="${line#*Your local IP address is }"
line=${line%%</p>*}
echo "Your ip is: $line"
exec >&5-
exit
;;
esac
done <&5
output