0

I do a lot of work on Virtual Machines, and have several scripts to help me configure them quickly. I could fully automate spinning up a headless VM, except that I need to know the IP of my host machine for configuring VNC stuff.

I use either of the following two commands to get the current IP address:

ipconfig getifaddr en1      # For IP address assigned to wireless
ipconfig getifaddr en0      # For IP address assigned to ethernet

The problem is that I have to tell my script whether to use en0 or en1. I use wireless at home and ethernet at work, so I can't make an assumption.

I could use:

ifconfig | grep '192'

and parse the resulting line (both work and home assign class C addresses), but I'd rather not have to do that.

Is there a way to determine which NIC is currently connected to the network from the command line? Or at least a better way of determining the host IP?

My machine uses Mountain Lion, if that's relevant.

  • 1
    I'm not an expert but couldn't you just use ipconfig getifaddr en0 to see if there is a cable connected. ( without an cable I'm getting nothing back if I use that command ) – FLY Jun 18 '13 at 8:52
  • That's actually a really good idea. If this had been an answer, and if I'd seen it before accepting the other guy, I'd accept this answer as the simplest & most elegant solution. – Cody Poll Jun 18 '13 at 17:51
  • Last comment to @FLY. – Cody Poll Jun 18 '13 at 20:38
  • no worries :) I'm glad that it helped. Did you use my solution or the one that you accepted as answer? just curious ;) – FLY Jun 19 '13 at 7:07
  • Yours. Much simpler. @FLY – Cody Poll Jun 20 '13 at 3:25
1

I would think your problem is determining "service order." The basic magic you see happen in the System Preferences > Network where the highest ranking connected service sorts to the top.

This Mac OS Hints article discusses a similar problem and solution.

| improve this answer | |
0

How about

ifconfig -au|egrep "en\d:"|cut -d: -f1

which will show you the names of any 'en' interfaces that are in an up state

| improve this answer | |
  • 1
    Great start, and thank you. The issue with this solution is that it displays both en0 & en1 when I'm connected wirelessly. Even when nothing is plugged in, en0 is still in an up state. – Cody Poll Jun 18 '13 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .