# A short command that returns the IP address?



## Hippo Man (Nov 16, 2007)

I'm running Leopard on a MacBook Pro.

Is there a short command that I can run from within the Terminal that will return the current IP address?

I know that I can run *ifconfig* and then pipe the results through an appropriate *grep*, but I'm wondering if there's also a more direct command that I can invoke from the terminal that will give me the IP address on stdout.

Thanks in advance.
.​


----------



## Natobasso (Nov 17, 2007)

http://guides.macrumors.com/IP_Address

"type &#8220;ipconfig getifaddr en0&#8221; for an ethernet connection, or "ipconfig getifaddr en1" for wireless, and press Return."


----------



## Hippo Man (Nov 19, 2007)

Natobasso said:


> http://guides.macrumors.com/IP_Address
> 
> "type &#8220;ipconfig getifaddr en0&#8221; for an ethernet connection, or "ipconfig getifaddr en1" for wireless, and press Return."


Thank you for your reply.

This indeed gives the IP, but I have to know whether I'm using the wired or wireless connection ahead of time, so I can then choose whether to put *en0* or *en1* on the command line.  Sometimes I'm on the wired connection and sometimes the wireless, and I'd like to figure out the IP with a single command that will give me the correct result in either case.

I know I can run a group of commands, the first one(s) to determine whether I'm running wireless or wired, and then to use that result to construct the final *ipconfig getifaddr* command, and I know I can put this group of commands into a shell script. I've just been wondering whether there's a single macosx command-line utility which will do all this for me in one swell foop.

I'm getting the idea that there isn't such a thing.  Oh well ...

Thanks again.
.​


----------



## Satcomer (Nov 19, 2007)

There was a MacOSX hint last year that might work to mimic ipconfig /all .


----------



## Hippo Man (Nov 19, 2007)

Thanks.  As I mentioned above, I know how to write one or more shell scripts to perform this task, and that's exactly what this ipinfo thing is doing.  I was just wondering if there's a single native macosx command-line utility which will return the current IP address without requiring grepping and parsing, but that doesn't seem to be the case.

In any case, the author of these scripts did a good job, and they are indeed useful for this and other related purposes.

Thank you very much.
.​


----------



## Natobasso (Nov 19, 2007)

Why not just run both commands? No harm in that, you just won't get a response for one of them. Though this might slow down your script slightly.


----------



## Natobasso (Nov 19, 2007)

Internal and external ips python script:
http://www.cs.cmu.edu/~benhdj/Mac/unix.html#getIP


----------



## Hippo Man (Nov 19, 2007)

Thank you very much.

Yes, I can do that.  Or for that matter, I can write a shell script to pipe *ifconfig* through the appropriate *awk* program, as well ... or I could to the whole thing within a *perl* or *python* or *ruby* program ... or I could write something in *C* ... or ...

I've just been trying to see if there was a single native macosx command which would return the IP ... mostly to gain familiarity with the command-line utilities that are available.  It's more a learning and understanding exercise for me than anything to solve a specific problem, since there are already so many ways to do that by combining groups of existing commands.
.​


----------

