terminal: get IP address

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

  1. 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. 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. macrumors 601

    Westside guy

    Oct 15, 2003
    The soggy part 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. 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. 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. 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. 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. 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. 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. macrumors newbie

    Jul 10, 2010
    External IP address via Terminal

    Try this:

    curl ifconfig.me/all/json
  12. 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. macrumors 601

    Jul 11, 2009
    Instead, you're "bothering" ifconfig.me as it's a website too. What's the difference?
  14. 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. 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. 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. 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.

Share This Page