Posted on 07-16-2012 09:57 AM
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.
Posted on 07-16-2012 10:06 AM
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.
Posted on 07-16-2012 10:29 AM
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.
Posted on 07-16-2012 10:30 AM
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."
Posted on 07-16-2012 10:36 AM
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.
Posted on 07-16-2012 10:40 AM
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
Posted on 12-03-2015 07:06 AM
@bbass Your script helped me complete one of my scripts. Thanks!