Skip to main content
Question

Shell Scripting: Detect Wi-Fi interface


Forum|alt.badge.img+24

So I hit a snag with a shell script that power cycled the Wi-Fi so that a preferred network setting would take effect. What I found was that I needed to figure out what the Wi-Fi interface was internally (en0, en1 etc) so I could call it correctly depending on the machine it was running on. (We have MacBook Airs where Wi-Fi is en0 and MacBook Pros where Wi-Fi is en1)

Here's the bit of code, hopefully it'll help someone else out.

wifi=`networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/,/Ethernet/' | awk 'NR==2' | cut -d " " -f 2`
networksetup -setairportpower $wifi off
networksetup -setairportpower $wifi on

If you've got a better way of nibbling down that -listallhardwareports output, let me know. I couldn't quite get sed to work right on this one.

I may or may not have heard that in some future version of the OS that might or might not rhyme with "Bountain Rion" that networksetup might or might not get more intelligent.

6 replies

Forum|alt.badge.img+7
  • Contributor
  • 51 replies
  • July 16, 2012

It can be done like this, too:

wifi=/usr/sbin/networksetup -listallhardwareports | grep -A 1 Wi-Fi | grep Device | awk '{print $2}'

Not sure it makes much of a difference, though.


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • July 16, 2012

You should be able to just call the service name, ie Airport or Wi-Fi from the networksetup command. Does that not work?

OK so I tested it, for the power setting you have to use the port, not the service so Wi-Fi doesn't fly.


Forum|alt.badge.img+13

meanwhile, in the bikeshed:

airport=`/usr/sbin/networksetup -listallhardwareports | /usr/bin/awk '/Wi-Fi|AirPort/ {getline; print $NF}'`
    /usr/sbin/networksetup -setairportpower $airport off
    echo "power to $airport is off."

Forum|alt.badge.img+24
  • Author
  • Valued Contributor
  • 1892 replies
  • July 16, 2012

Nice Nate. I'll use a mod on yours. Thanks

You always are in the darn bikeshed. :)

Tom

OK so I tested it, for the power setting you have to use the port, not the service so Wi-Fi doesn't fly.

You've just discovered the "more intelligent" bit.


Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • July 16, 2012

Yup and I remember how I did it in the past. I wrote a case statement based off of hardware model since we had Mac Pros too (dual Ethernet card) that also had airport cards in them. Then based on the hardware model in the case statement that set the port.

That was like 4 years ago though, so I did not remember the service versus the port bit until I tested it.

Oh well, it is good to re-learn things from time to time.

-tom


Forum|alt.badge.img+7
  • Contributor
  • 97 replies
  • December 3, 2015

@bbass Your script helped me complete one of my scripts. Thanks!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings