Similar to a http://whatismyip.com lookup. It would obviously need to query a computer out there. Just wondering if anyone had a clever way to do it?
-
See also unix.stackexchange.com/a/81699/37512. – Timo Tijhof Jul 4 '13 at 2:57
curl http://myip.dnsomatic.com
dig +short myip.opendns.com
This only works if you are using OpenDNS as your dns server.
If you aren't, one of these should work:
dig +short myip.opendns.com @208.67.222.222 dig +short myip.opendns.com @208.67.220.220 dig +short myip.opendns.com @208.67.222.222 @208.67.220.220
-
1
-
-
-
+1 for using DNS to find out an IP (instead of http). You could also specify the dns server using its name e.g.,
@resolver1.opendns.com
(for readability) – jfs Feb 27 '14 at 14:38
STUN is the proper solution.
% stun -v stun.ekiga.net ... MappedAddress = 88.189.152.187:18009
- a list of public STUN servers
- a free software STUN client
- The STUN standard
You can use curl to get the page from something like whatismyip and then get the pieces out. I used whatismyipaddress.com in this example...obviously the fields will differ with different services.
curl -s http://whatismyipaddress.com/ | grep LOOKUPADDRESS | awk '{ print $4 }'
One must use OpenDNS' servers to use this... You can query a certain DNS server with dig like that:
dig +short myip.opendns.com @208.67.222.222
i just run a traceroute to somewhere on the internet and look for the hop out of our local network.
perhaps there's a better way?
you can use the ifconfig command to list all interfaces and their associated IP address(es).
so, if you know your internet interface is ppp0, you can run
$ ifconfig ppp0 ppp0 Link encap:Point-to-Point Protocol inet addr:X.X.XX.X P-t-P:Y.Y.Y.Y Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1444 Metric:1 RX packets:198986 errors:0 dropped:0 overruns:0 frame:0 TX packets:122929 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:134195571 (127.9 MiB) TX bytes:17101701 (16.3 MiB)
X.X.X.X will be your IP address. Y.Y.Y.Y is the IP address of the next hop.
you can then postprocess the output of ifconfig with grep/awk/sed/cut/perl/whatever to extract just the IP.
another alternative, if you have the iproute tools installed, is to use the ip command. e.g.
$ ip addr list ppp0 21842: ppp0: mtu 1444 qdisc htb state UNKNOWN qlen 3 link/ppp inet X.X.X.X peer Y.Y.Y.Y/32 scope global ppp0
that's probably easier to read and certainly easier to parse:
$ ip addr list ppp0 | awk '/inet/ {print $2}' X.X.X.X
-
- There is chance that any IP in ifconfig will not match real Internet IP (in case of biNAT) - In FreeBSD there is no iproute2 package, so ip command is not applicable – SaveTheRbtz Jul 18 '09 at 3:57
-
true, i don't use NAT so it never occurred to me to....it just seemed "wrong" to me to use an external service for information you can query your own system about. even with NAT, i'd still prefer to figure out how to query the router doing the NAT (perhaps via an SNMP query) than to rely on an external service which may or may not be available when i need it. – cas Jul 18 '09 at 4:44
-
@Craig, Your approach is fine. However, its not an external IP unless you can reach it on that value from outside. You do have to rely on an external point for this answer. The idea is to find a reliable (at least more than your perimeter point) external point for the query. OpenDNS is a good point. – nik Jul 19 '09 at 17:17
-
@nik: the only likely case where your border router's IP isn't the external IP is when you're behind multiple layers of NAT...in which case, the correct solution is to switch to a non-brain-damaged service ASAP. also, the problem with fetching a URL to find out your IP is that it only tells you the IP of the host that actually fetches it - which may be a proxy that strips Via headers. – cas Jul 20 '09 at 9:51