17

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?

1

12 Answers 12

32
curl http://myip.dnsomatic.com
2
  • That's a lot less work than my example :)
    – Alex
    Jul 17, 2009 at 1:18
  • Wonderful simplicity. Jul 17, 2009 at 6:06
9
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
4
  • 1
    Doesn't work on my MacOS 10.5, Ubuntu 8.04, or Ubuntu 9.04. Jul 17, 2009 at 6:05
  • Interesting that it does on my Ubuntu 8.10. Jul 17, 2009 at 17:44
  • It is probably because I use opendns. Jul 17, 2009 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)
    – jfs
    Feb 27, 2014 at 14:38
4

STUN is the proper solution.

% stun -v stun.ekiga.net
...
MappedAddress = 88.189.152.187:18009
3

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 }'
2
lynx -dump http://www.pcmesh.com/ip-check.cgi | awk '/REMOTE_ADDR/{print $2}'
2

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
2

the simplest way is: curl ifconfig.me

1

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?

1

"lynx http://whatismyip.com"

1

I love to use curl -4 icanhazip.com. The service is made to be used in scripts.

0

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
4
  • - 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 Jul 18, 2009 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, 2009 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, 2009 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, 2009 at 9:51
0

If you facing a private ip of your isp and use some of firewall like cisco ASA, then you may setting up DNS Client and try to resolve fqdn myip.opendns.com quary in OpenDNS resolver to now that public ip you have from ISP.

SomeASA# show ip address
System IP Addresses:
Interface                Name                   IP address      Subnet mask     Method
Vlan1                    inside                 10.10.10.1      255.255.255.248 CONFIG
Vlan2                    outside                192.168.1.2     255.255.255.0   CONFIG
Current IP Addresses:
Interface                Name                   IP address      Subnet mask     Method
Vlan1                    inside                 10.10.10.1      255.255.255.248 CONFIG
Vlan2                    outside                192.168.1.2     255.255.255.0   CONFIG
SomeASA#

! - - Configure DNS client for OpenDNS resolver on ASA:

dns domain-lookup outside
dns server-group DefaultDNS
 name-server 208.67.222.222
 name-server 208.67.220.220
 domain-name example.com

! - - and try to ping myip.opendns.com

SomeASA# ping myip.opendns.com
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 1.1.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
SomeASA#

! - - when DNS resolver return IP on fqdn myip.opendns.com request with IP what he that obtain from sources of ip address in request that arrives on resolver (in this example is resolver1.opendns.com [208.67.222.222] and resolver2.opendns.com [208.67.220.220])

SomeASA# show dns-hosts

Host                     Flags      Age Type   Address(es)
myip.opendns.com         (temp, EX) 0    IP    1.1.1.1
SomeASA#

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .