A Bash Command To Get Your IP
This is a one line bash command to get your IP.
It queries whatismyip.com, processes the output and displays the IP.
You might want to add it to your aliases file.
UPDATE: Don’t use this command, use the one in the end of this post
1 | wget -O - whatismyip.com 2> /dev/null | grep "Your IP Address Is" | sed -e 's/^.*Is//' -e 's/<.*$//' |
Enjoy
Update: A nice guy – Theodore- has put this along with a script of his own to get a simple and neat bash script which shows both the internal (wired and wifi) and the external IPs. Script posted at his blog. Script in Theodore’s post is for Mac, but can be easily modified to run on Linux. Thanks Theodore.
Update2: Thanks to commenter Paul Dorman, I visited whatismyip’s forum and realized that they don’t allow to visit their home page too many times (they might ban you if you do so). However, they give an alternative: Another page in their website where you can get your IP. So the new command to query your IP is:
1 | wget http://www.whatismyip.com/automation/n09230945.asp -O - 2> /dev/null; echo |
Please use this and not the previously mentioned command.
Thanks Paul.
Related posts:
10 Comments to A Bash Command To Get Your IP
Trackbacks/Pingbacks
[...] to Twitter, I just happened to run across a post about getting the external IP in bash on Amir Watad’s blog. So simple. I like [...]
[...] Posted an item embee: A Bash Command To Get Your IP (via Digg) [...]
Leave a Reply
About Me
Tags
Categories
- Algorithms
- Bash
- BlackBerry
- Collaboration
- Command Line
- Cool Tricks
- Easter Eggs
- Ebooks
- Firefox
- Hardware
- Humor
- iPhone
- Linux
- Linux Development
- Linux Kernel
- Networks
- Open Knowledge
- Other
- Productivity
- Programming
- Regular Expressions
- Science
- Security
- Shell Scripts
- Short Posts
- Social Networks
- Thoughts
- Tools
- Vim
- Web Development
- Websites
Popular Posts
Calendar
Archives
- July 2010 (5)
- June 2010 (1)
- May 2010 (1)
- April 2010 (3)
- March 2010 (1)
- January 2010 (1)
- December 2009 (2)
- September 2009 (13)
- July 2009 (1)
- June 2009 (6)
- May 2009 (4)
- March 2009 (18)
- February 2009 (10)
- January 2009 (10)
- December 2008 (7)
- November 2008 (8)
- October 2008 (1)
- August 2008 (1)
- July 2008 (1)
- June 2008 (2)

I tweeted this, but for those who are not on Twitter, what about using ifconfig | grep inet? You can use sed and grep to get output that only includes the IP, or just to format things more nicely. I made a bash script that does only that, for convenience when I was having trouble with my school’s wireless. I’d be happy to send it to you if you would like to take a look at it.
Hi De-Escalate,
Thanks for your comment.
I agree that ifconfig will show you the IP, but it’s not always the same IP that the external world see.
If you’re behind a NAT for example, you computer will “think” that it has a certain IP (which was assigned to it by the NAT and identifies it inside the LAN), but if you visit a website, it will see a different IP, which is in the above case the IP assigned by your ISP to the NAT.
ifconfig will give you the internal IP, whatismyip is an external site, so it will see you “real” IP.
And yes, please send the script to me. My Email appears in the top of the sidebar.
Thanks again
That makes sense… we were trying to solve different problems. I might tack your command to my script so that I can have inside and outside IPs. Thanks!
Welcome
I got banned for trying this several times within 5 minutes. However the site actually has a place for scraping: http://www.whatismyip.com/automation/n09230945.asp
They tell you about it when they ban you
)
Hi Paul,
Thank you for your comment. I checked in their forums and you’re right.
I updated the post.
Thanks for sharing this information.
This is my method…
curl -s checkip.dyndns.org|sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//’
I’ve a more simple method:
curl icanhazip.com
bye!