Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
See also unix.stackexchange.com/a/81699/37512. –  Krinkle Jul 4 '13 at 2:57

10 Answers 10

up vote 27 down vote accepted
curl http://myip.dnsomatic.com
share|improve this answer
    
That's a lot less work than my example :) –  Alex Jul 17 '09 at 1:18
    
Wonderful simplicity. –  Drew Stephens Jul 17 '09 at 6:06

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?

share|improve this answer

"lynx http://whatismyip.com"

share|improve this answer

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 }'

share|improve this answer
lynx -dump http://www.pcmesh.com/ip-check.cgi | awk '/REMOTE_ADDR/{print $2}'
share|improve this answer
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
share|improve this answer
1  
Doesn't work on my MacOS 10.5, Ubuntu 8.04, or Ubuntu 9.04. –  Drew Stephens Jul 17 '09 at 6:05
    
Interesting that it does on my Ubuntu 8.10. –  Brad Gilbert Jul 17 '09 at 17:44
    
It is probably because I use opendns. –  Brad Gilbert Jul 17 '09 at 17:45
    
+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) –  J.F. Sebastian Feb 27 at 14:38

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
share|improve this answer

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
share|improve this answer
    
- 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

STUN is the proper solution.

% stun -v stun.ekiga.net
...
MappedAddress = 88.189.152.187:18009
share|improve this answer

the simplest way is: curl ifconfig.me

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.