Which network port to use? Ethernet or Wifi? On OSX.

Easy way to toggle Ethernet or WiFi ports on MacOS or OSX

====
I find sometimes I want to work on my machine either via the wired or WiFi connections. For example, on my office setup I have different routing between my WiFi and my wired - one is load balanced, the other isn't. Sometimes, I may want stuff to go through the non-load balanced connection.

Of course it's easy enough to turn off WiFi - but what about if you want to turn off the Ethernet port easily? Remember that the service order in MacOS/OSX by default makes Ethernet a higher priority than WiFi?

Well, it's pretty easy really. Start 'Script Editor', and enter the following command in the script editor for turning
off the Ethernet port:

do shell script "/sbin/ifconfig en0 down" with administrator privileges

Then, click 'Save', and make sure you change the 'File Format' to 'Application':

2016-10-19FileFormat

If you double-click on the saved script, you'll get the normal authorise logon box (It's an admin function), but then you'll find your Ethernet post is now disabled.

You can also make another script to turn the Ethernet port back on - same process, but a slightly different command. Would you like to guess the command…? No prizes:

do shell script "/sbin/ifconfig en0 up" with administrator privileges

Again, save it as an Application and you now have two small apps that'll turn the Ethernet port on or off as you like.

Now, there is another way to do this using 'Locations'. If you look at 'Network' in 'System Preferences', look at the bit at the top - notice it says location:

2016-10-19Location

Locations are a great way to define different networking scenarios for different locations - here's the thing though, they don't need to be
locations at all. Think of them as different network scenarios instead. So in my setup for example I have multiple locations defined like this:

2016-10-19LocationDefined

Each location changes the service order so that either WiFi is at the top, or the wired is at the top - I.e. Which one has priority. To change the service order, select 'Set Service Order' on the small cog at the bottom of the network preferences pane:

2016-10-19SetServiceOrder


So you can have a couple of locations that have a different operating priority. Now, once you have these locations defined you can of course do funky scripting with them. For example I have a bash script like this:

# Setup Variable(s) #
Location=`networksetup -getcurrentlocation`

if [ "$Location" = "Default_Wired" ]
then
echo "Current location is Default_Wired"
Echo "Switching to Default_Wifi"
scselect Default_Wifi
else
echo "Current location is not Default_Wired"
Echo "Switching to Default_Wired"
scselect Default_Wired

fi



…Saved as 'ToggleNetwork.sh'. I've also set the file association so that all .sh files open with Terminal:

2016-10-19OpenWith

Dragging the .sh to the dock (the bit
below the line on your dock as it's not an app), means it's there to click on whenever I like - my dock is on the left of the screen by the way.

2016-10-19Dock

You can of course change the icon if you want - clicking on it opens terminal and does one of these:

Switch to WiFi
2016-10-19SwitchToWiFi
Switch to Wired
2016-10-19SwitchToWired


Anyway, as you can see, there's a lot of flexibility in trying to automate your workflow and make your days a little easier.

Now, it may be that you don't want these shell outputs left on your screen - you can also change this behaviour. Fire up Terminal, and go to the Terminal menu and select Preferences. On the Profiles tab, select Shell. You can change the window behaviour there - so you could set it to 'Close if the shell exited cleanly'. Downside to it is I'm not quite sure which connection/location you've switched to!





blog comments powered by Disqus