A short command that returns the IP address?

Hippo Man

Hippo Man
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.
.​
 
http://guides.macrumors.com/IP_Address

"type “ipconfig getifaddr en0” 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.
.
 
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.
.​
 
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.
 
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.
.​
 
Back
Top