Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Get your IP number from the terminal Internet
[Editor: Revised completely to reflect the tips added to the comments - thanks everyone!]

If you have a dynamic IP address (one that changes every time you connect to the net), there are a number of GUI tools that will display it for you. However, if you want to be able to get your IP number from a terminal session, the following appears to be the easiest way. The command uses 'wget' to read the contents of a web page that returns your IP address as seen by the external web server, and then processes the web page to extract the IP. The command to type is:

wget -q -O /dev/stdout |[space]
  grep '<H1>' | sed 's|</*H1>||g'

NOTE: Shown on two lines for a narrower window; enter on one line and replace [space] with an actual "space" character.

You'll get a single line containing your external IP address. Note that this will only continue to work as long as doesn't change the format of their web page! Thanks to everyone (see the comments) who contributed to the development of this tip.

Read the rest of this article for an explanation of how it works, and a way to make an easy to use "alias" that will make it as simple as typing "showmyip" or whatever you'd like to call it.

So what does that complicated line of text do? In a nutshell, it goes out to a website which reads your IP number. Using wget in quiet mode (-q) and with output directed to standard output (-O /dev/stdout), the web page (http://// is grabbed, and then the resulting HTML is sent (|) through the UNIX search command (grep). grep is looking for a particular HTML tag (H1) which denotes the start of the line that contains the actual IP address.

SIDEBAR COMMENT: This command will break badly if ever changes the formatting of the output page -- so if it starts misbehaving, that's probably what happened! To fix it, you'd need to view the raw HTML on the page and see if there's a way to identify where the IP number starts, and then adjust the 'grep' and 'sed' portions of the command.

The 'grep' command returns a line which looks like <H1></H1>, where is your actual IP number. This is then passed (again, via the |) to 'sed', which will edit the stream of data it just received. 'sed' runs a search and replace (near as I can tell; it's beyond my skills at present!) which takes the H1 start and end tags out, leaving just the IP number. Pretty slick.

Now, to make this easily useful, just embed it in an alias:

alias myextip 'wget -q -O /dev/stdout[space]
  | grep H1 | sed "s|</*H1>||g" '

NOTE: Shown on two lines for a narrower window; enter on one line and replace [space] with an actual "space" character.

See Bart's comments for information on how to make the alias permanent.
  • Currently 2.00 / 5
  You rated: 1 / 5 (7 votes cast)

Get your IP number from the terminal | 20 comments | Create New Account
Click here to return to the 'Get your IP number from the terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
simpler way
Authored by: Anonymous on May 05, '01 01:37:38PM
just type
ipconfig getifaddr <interfacename>
Where interface name is one of the interfaces listed by typing
ifconfig -a
if you have a broadband or network connection, you're probably using your ethernet connection and your interface name is probably 'en0' (Ethernet card 0)

This method is fast and completely local, i.e. you don't need to go make an HTTP request to get your own IP address.

[ Reply to This | # ]

Authored by: robg on May 05, '01 01:57:18PM
But this seems to only return the internal IP address. What if I want the external IP address? My machine sits behind a hardware router; all my internal IPs are "192.168.1.x", but my public IP is provided by AT&T. So when I hit a web site, the actual value returned is my true IP, not the internal-only (obviously) 192 version. When I tried these commands in my terminal, all that ipconfig getifaddr en0 returned was my "192" address. I don't see any way to pull the public IP from the ipconfig data? -rob.

[ Reply to This | # ]
Authored by: Anonymous on May 06, '01 01:06:07PM

You're right, that will give you the local address. If you're behind a firewall and your address is being translated, the only way I can think of to get your address is to make a request out like you did with wget or a similar utility. That's what many dynamic DNS clients do.

That's assuming you want the external address and it doesn't change. Sometimes firewalls will not give you a 1-to-1 address correlation. The address may change every time you make a connection through the firewall.

[ Reply to This | # ]
an easier way, perhaps
Authored by: bakednotfried on May 05, '01 01:44:10PM
hey folks,
macosx comes with a common unix utility called ifconfig (for interface config) that allows you to configure network interface parameters. it also lets you display them as well. typing
ifconfig -a
allows you to see information for all the interfaces. running this on my system returns
lo0: flags=8049 mtu 16384
        inet netmask 0xff000000 
en0: flags=8863 mtu 1500
        ether 00:30:65:bd:98:a6 
        media: autoselect (none) status: inactive
        supported media: none autoselect 10baseT/UTP  10baseT/UTP  100baseTX  100baseTX 
en1: flags=8863 mtu 1500
        inet aaa.bbb.ccc.ddd netmask 0xffffff00 broadcast aaa.bbb.ccc.255
        ether 00:30:65:00:82:9c 
        media: autoselect status: active
        supported media: autoselect
my machine has three interfaces. interface lo0 is the loopback interface, a logical interface that is always up. it is a way to say 'myself' in a computer. it has the ip address and can be used to connect to yourself, as in
telnet localhost
en0 is the ethernet interface on my machine, and en1 is the airport card. the ip address for my airport card is aaa.bbb.ccc.ddd (obviously, not the real address). ifconfig tells you more than the ip address, including the hardware address or MAC address of the physical interface, 00:30:65:00:82:9c for my airport card. you can also display info for a particular interface
ifconfig en1
hope this helps,

[ Reply to This | # ]
Authored by: krove on May 05, '01 02:51:55PM

If you're behind a firewall or NAT setup, this is beautiful as the usual "ipconfig getifaddr <interfacename>" will only give an internal network ip address.

Cool! krove

[ Reply to This | # ]
the grep'ing
Authored by: robh on May 05, '01 04:54:23PM

How about this...

wget -q -O /dev/stdout | grep '<H1>' | sed 's|</*H1>||g'

[ Reply to This | # ]
Making it user-friendlier
Authored by: Bart VdBroeck on May 05, '01 05:24:18PM

After I read this tip and its replies, I just had to try it out. Here's what I did. I added the following two lines to the ".cshrc" file in your home directory:

alias myip ipconfig getifaddr en0
alias myextip 'wget -q -O myipnum; grep H1 myipnum | cut -f 2 -d < | cut -f 2 -d >; rm -f myipnum'

There are two mistakes in the second line. Seems whatever takes care of formatting these comments takes out the backslash characters. You'll have to add a backslash (the flipped version of this one: "/") before "<" and ">".

Again some explanation. First, if you haven't got a ".cshrc" file in your home directory, you'll have to make one. Since this should be a plain text file, you'll have to instruct TextEdit to make a plain text file when you use it to make this file. You'll find the needed menu item in the menu "Format". I however prefer to do this with pico. So I opened and typed "pico .cshrc" in a terminal window. Then I put (you can enter them with copy and paste) the above lines in the file. Type control-o and then control-x (save file and close file).

Now, about those lines. They'll create aliases i.e. named series of commands, something like macros or scripts.
The first one is called "myip" (you can change the name if you like, but be aware these names are case-sensitive like everything Unix). It simply executes the excellent tip from Boulter. If you're a telephone modem user, replace "en0" with "ppp0". If you're not sure check with "ifconfig -a".
The second one is called "myextip" (same remark as above). It executes a series of commands based on the original tip from our beloved Rob Griffiths ;-) It gets the needed web page, finds the line with the IP number (enclosed with <H1> and <H1> tags), selects the second field delimited by "<", so everything before the <H1> tag, and then selects the second field delimited by ">", so everything after the <H1> tag. The result is written as output, 'cause we don't process it any further. Finally the webpage is deleted without any warnings (the -f option). The "" before "<" and ">" tells the shell those are just characters, not commands (the default way the shell looks at those two).

Making those aliases makes sure you don't have to remember the commands. Since you can chose the names of the aliases, I guess remembering them is a lot easier.

[ Reply to This | # ]
Making it user-friendlier
Authored by: Bart VdBroeck on May 05, '01 05:28:45PM

Okay, so the line

The "" before "<" and ">" tells the shell those are just characters, not commands (the default way the shell looks at those two).

should be something like

The backslash character before "<" and ">" tells the shell those are just characters, not commands (the default way the shell looks at those two).

To enable you're new settings type "source .cshrc" after you've edited the ".cshrc" file.

[ Reply to This | # ]
Making it user-friendlier
Authored by: Bart VdBroeck on May 05, '01 05:38:53PM

Ooh, robh's tip is even better. Replace line two with

alias myextip 'wget -q -O /dev/stdout | grep H1 | sed "s|</*H1>||g" '

No backslashes needed this time :-)

PS Sorry about the double post. Seems OmniWeb and the server soft don't like each other.

[ Reply to This | # ]
wget is not good
Authored by: fotoun on May 06, '01 05:03:27AM
I tried the proposed wget -q -O /dev/stdout | grep '' | sed 's|||g'. This is not good for me because it returns the address of the proxy ! To get the same information directly, I prefer to use ifconfig -a | grep inet | grep -v "" | awk '{print $2}'.

[ Reply to This | # ]
Authored by: robg on May 06, '01 09:21:19AM

On my machine, behind the firewall, that still returns the value of the local box (192.x) not the public IP. Are you on a proxy, but NOT behind a firewall?


[ Reply to This | # ]
Authored by: fotoun on May 07, '01 05:43:56PM

I use a transparent proxy and my IP is not translated. That means that I am not 'behind' a Firewall but behind a simple HTTP proxy.

[ Reply to This | # ]
wget does not work for me
Authored by: James Bond on May 13, '01 11:31:05AM

What do I need to do to get it to work?

I am behind port 8080 operated by my ISP/Cable supplier.

I have set http_proxy and use_proxy as per the GNU documentation but I get the following:

[localhost:~] roger% set | grep proxy
use_proxy on
[localhost:~] roger% wget
=> `index.html.3'
Connecting to
connect: Operation timed out

(try: 2) => `index.html.3'
Connecting to ^C

[ Reply to This | # ]
better place to get external IP
Authored by: Anonymous on May 06, '01 01:13:35PM


Its format will not change and is simpler to parse.

[ Reply to This | # ]
you can also use this
Authored by: stoffel on May 14, '01 04:52:09AM
netstat -rn

[ Reply to This | # ]
Not on my machine...
Authored by: robg on May 14, '01 11:11:25AM
Netstat doesn't seem to return any info about the external IP address if you're using a hardware router:
[8:06am robg ~]% netstat -rn

Routing tables
Destination Gateway Flags Refs Use Netif Expire
default UGSc 8 5 en0 UH 9 9003 lo0
192.168.1 link#2 UC 0 0 en0 0:22:58:d6:cb:24 UHLW 9 0 en0 1194 link#2 UHLW 1 1771 en0 ff:ff:ff:ff:ff:ff UHLWb 2 273 en0
As far as I've seen, the only way to get your external IP address is by hitting a site outside your internal network and requesting the IP number.

[ Reply to This | # ]
the easiest (for the user) way
Authored by: shemp9999 on Jul 10, '01 10:04:46PM

based on this thread, i've written 2 perl scripts (and a crontab) that get/mail the current ip address.

i have earthlink DSL (dynamic) and an asante firewall box, so the wget method is appropriate for me.

the mail script is nice, it mails me at work when my ip changes (so i an access any machine i've configured access for on the router).

i posted them on the forum a while ago, but they seem not to have shown up (??!!??)...anyhoo;

[ Reply to This | # ]
PHP web page for pinging
Authored by: shmuel on Dec 30, '02 02:52:48AM

I didn't want to worry about someone changing the format of their webpage and breaking my IP address utility so I created a page of my own to ping. I have a hosted website with PHP installed so I created a page with a piece of PHP like this:

<? print "<H1>" . $REMOTE_ADDR . "</H1>"; ?>
<? print "<p>" . GetHostByAddr($REMOTE_ADDR) . "</p>"; ?>

Make sure there is a carriage return between the first and second line or the grep offered here will not work.

I then use the same grep pattern offered here and a simple alteration of the same pattern to get my hostname as well.

alias myhost="wget -q -O /dev/stdout | grep '<p$
alias myip="wget -q -O /dev/stdout | grep '<H1>$


[ Reply to This | # ]
Get your IP number from the terminal
Authored by: n1mie on Jan 18, '06 11:31:03AM

This is an old hint, but it helped me hone in on an updated solution. The address suggested in the original hint doesn't work anymore. You can use the new one (or several others) suggested by my update.

This single line of code will get your external ip address and return only that, nothing else, using STANDARD tools included with OS X (wget is not included in current OS X installation).

curl -s | grep -o '[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9]*'


[ Reply to This | # ]
Get your IP number from the terminal
Authored by: paulrob on Jan 18, '06 12:35:56PM

Assuming that appropriate security can be applied, the best webpage to 'skim' would be the status page (assuming there is one) on your router. I've yet to see a router that is configurable via webpage that did not also display the info somewhere. It is unlikely to be reformated.

Many routers can also be contacted via SNMP or TELNET (the latter certainly being insecure)

Also if the router has a firewall, whose logs are being forwarded to your computer, you maybe able to retrieve the IP number from the logs.

[ Reply to This | # ]