terminal: get IP address

Discussion in 'Mac Apps and Mac App Store' started by sonofslim, Mar 13, 2004.

  1. sonofslim macrumors 6502a


    Jun 6, 2003
    is there a terminal command that will display your external IP address? ifconfig and netstat only seem to report my local/network address, so i've been using curl and awk to parse http://checkip.dyndns.org. but i'd prefer a way that doesn't rely on an http call. not to mention that my method will fail if/when dyndns changes the format of that page.

    any ideas?
  2. edesignuk Moderator emeritus


    Mar 25, 2002
    London, England
    if all you want to know is your WAN IP address http://www.whatismyip.com should do the job for you, it has been online for an age, and I can't see anyone changing it anytime soon.
  3. Westside guy macrumors 603

    Westside guy

    Oct 15, 2003
    The soggy side of the Pacific NW
    I'm a bit confused - are you behind a NAT firewall? ifconfig should report your IP address just fine.

    I realize this is just nit-picking, but if your NAT-ted then what you're probably looking for is the IP of the router/firewall rather than your own. If that's the case you can usually get it from the router's Web interface (assuming it has one, and most do).
  4. sonofslim thread starter macrumors 6502a


    Jun 6, 2003
    yes, that's the case. is there any way to get my router's external IP address from the terminal?
  5. saabmp3 macrumors 6502a

    Jul 22, 2002
    Tacoma, WA
    I think your best bet is to do a traceroute of a packet ping (so it sends back to you). This will report both the outgoing (not too helpful) and return trip (where you will find your IP).

  6. GigaWire macrumors 6502

    Dec 25, 2001
    try ifconfig

    yes ifconfig, not ipconfig

    Here is a more detailed look at network commands for UNIX, LINUX, and DOS.
  7. sonofslim thread starter macrumors 6502a


    Jun 6, 2003
    see above: ifconfig gives me my local network IP. but the link you sent was pretty helpful -- thanks.

    sorry, i can't figure out how to combine traceroute & ping... any clues?
  8. sajaki macrumors newbie

    Feb 5, 2010
    try this in a console window (ensure that cURL is installed) :

    curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z/<> :]//g'
  9. JNB macrumors 604


    Oct 7, 2004
    In a Hell predominately of my own making
    Six year old thread. You win! :D
  10. tcarroll2 macrumors newbie

    Sep 14, 2008

    Yes, it is six years old, but as the original poster asked, is there a way, from behind an Airport Extreme, Express, or Time Capsule, to determine your WAN IP address, without relying on an external source, such as checkip.dyndns.org or whatsmyip.com?

    It has been six years and maybe something has come around that will now help us to do this?

    I appreciate any suggestions on accomplishing this locally as originally asked.
  11. markadley macrumors newbie

    Jul 10, 2010
    External IP address via Terminal

    Try this:

    curl ifconfig.me/all/json
  12. tcarroll2 macrumors newbie

    Sep 14, 2008
    Perfect! Now I can create a shell script to parse the external address and compare it to a saved address without having to bother whatismyip.com with unnecessary calls. I appreciate it! :D
  13. chrfr macrumors 604

    Jul 11, 2009
    Instead, you're "bothering" ifconfig.me as it's a website too. What's the difference?
  14. tcarroll2 macrumors newbie

    Sep 14, 2008
    I did not realize that. I thought the ifconfig.me was just a function of the ifconfig command.

    That still doesn't do what I want then, nor the original poster. I'll keep looking. Thank you for your response.
  15. tcarroll2 macrumors newbie

    Sep 14, 2008
    Ok, after doing some more research, I have found a way to get the external IP address from an Airport Extreme or Time Capsule (which is what I have) and I don't see why this wouldn't work for any other router that supports SNMP.

    From the terminal do this:

    prompt$ snmpwalk -Os -c {community name assigned to your device} -v 1 {private ip i.e.} ipAdEntAddr

    The result for me is:
    ipAdrEnt.xxx.xxx.xxx.xxx = IpAddress: xxx.xxx.xxx.xxx
    ipAdrEnt. = IpAddress:
    ipAdrEnt. = IpAddress:
    ipAdrEnt. = IpAddress:
    ipAdrEnt. = IpAddress:

    Now all I need to do is parse this the way I want it and I no longer have to ping an external web site to determine my own external IP address.

    I believe this answers the original posters question.
  16. darkhan macrumors newbie

    Oct 22, 2011
    The reply to this question is there are no such terminal command and/or software of OS that can tell your external IP address (i.e. your "location" in internet) without using external resources (e.g. some other computers).

    The reason is simple. It's like asking from your computer where it's located without plugging it in to the internet..
    When the computer was just manufactured in the factory and OS was just clean installed on it, it doesn't know your IP address, before you buy this particular computer.. what if someone else bought this particular computer? that would mean that any picked computer has to know all existing external IP addresses and intelligently decide which IP belongs to which guy and also keep in mind that that guy's ISP also changes it from time to time...

    That's why you use another server to "bounce" your IP address from as in:
    curl ifconfig.me/all/json

    You also can write a program, which you will be able to run in terminal by just one word, but it still will have to use "external" resources to find out about your external ip address..
  17. tcarroll2 macrumors newbie

    Sep 14, 2008
    You are partially correct in that you do need an external resource other than the computer. In my case it is my SNMP compatible router.

    What the original poster wanted to do was similar to what I have been successful at. I wanted/needed my router's external IP address and I need to check the IP periodically because my router does not support the dynamic IP service I use. Rather than using http calls and creating traffic (albeit minimal) I wanted something internal to my network that would be able to determine whether or not my dynamic IP address has changed and if so I then reach out to my registrar and update the DNS with the new IP address.

    I don't think (my assumption) the OP was asking how to determine the Internet IP address (location) of a computer that has been purchased, set up, and turned on for the first time without connecting it to the Internet.
  18. n8smith09 macrumors newbie


    Feb 9, 2016
    ok, i know this is an old thread, but this same problem put me through a lot of grief.
    this is the best I can do cleanly... it only relies on my ISP to respond to a ping (a very small one too!).

    ISP=`traceroute -M 2 -m 2 -n -q 1 | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`
    extIP=`ping -R -c 1 -t 1 -s 1 -n $ISP | grep RR | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'`
    echo $extIP

    The only other way to do this without relying on any external servers is to get it from curl'ing your modem's status page... I've done this successfully with our frontier DSL modem, but it's dirty, slow, unreliable, and requires hard-coding your modem's password. Here's the "process" for that:

    curl http://[user]:[password]@[modem's LAN address]/[status.html] | grep 'WanIPAddress=' | grep -m 1 -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
    That fetches the raw html, searches for any lines containing "WanIpAddress =" (change that so it's appropriate for your modem's results), and then narrows down those results to an IPv4 style address.

    Have fun!

Share This Page