Jun 22 2013 0

Toggle Network Services on Mac OS

From time to time when performing a number of large downloads simultaneously, for whatever reason the network connectivity for the Ethernet port stops working. Looking at the Network pane within System Preferences does not give any indication that there is an issue with the Ethernet port. System Preferences Network Pane

Resolving The Issue Manually

Resolving the issue manually requires the following steps

  1. Open System Preferences and goto Network
  2. Select the Ethernet service
  3. Click on Gear and select the option Make Service Inactive’
  4. Click on the Apply’ button

Now the Ethernet service has been deactivated, now perform the following steps to activate it again

  1. Click on Gear again and now select the option Make Service Active’
  2. Click on the Apply’ button again

Now the Ethernet serice is active again and the connectivity issue is resolved. Not a lot steps but tedious nonetheless. So there has to be a better way of performing all these steps.

Resolving The Issue From The Terminal

Your Mac comes with a tool named networksetup which is a configuration tool for network settings in System Preferences. You need at least admin” privileges to be able to run this tool. If you what to see all the things this tool can do for you read through the man-pages (man networksetup).

Using the following command will give you a list of all the network services defined on your Mac

networksetup -listallnetworkservices

which will give something like the following (depending on what types of services have been added on your Mac)

An asterisk (*) denotes that a network service is disabled. Bluetooth DUN Ethernet Wi-Fi

From the man-pages I found that there is also a setnetworkserviceenabled option which takes the following syntax

-setnetworkserviceenabled networkservice on | off

So issue the following command will disable the Ethernet port

sudo networksetup -setnetworkserviceenabled Ethernet off

and this will enable the Ethernet port again

sudo networksetup -setnetworkserviceenabled Ethernet on

You can also execute the command without using the sudo command, you will then be asked to enter your password in the Confirmation dialog that will appear

Password DialogPassword Dialog

While this is already better, there is still room for improvement…

Automator To The Rescue

To further automate these steps I used Automator to create a Service

  1. Start Automator and choose Service’ for the document type and click on the Choose’ button Automator
  2. Set Service receives’ to no input’ and select any application’ for the second list
  3. Make sure Actions is selected and type run’ in the search box
  4. Double click Run AppleScript’ to add it as a step to the service AppleScript step
  5. Add the piece of AppleScript code (listed at the end of this post) into the AppleScript step (make sure to replace <your account> and <your password> with the correct information)
  6. Save the Service and give it an appropriate name (I choose Fix Ethernet’)

NOTE: Since your account name and password will be stored in the service this poses a security risk. By default a Service you create is stored in ~/Library/Services/ so the risks should be minimal as long as you exercise proper security steps (i.e. lock your Mac when you leave your Mac unattended).

From the moment the Service has been saved it will appear in every Services sub menu Services menu

If you leave the System Preferences Network Pane open when you use the service you can even see the Ethernet service switch states.

AppleScript code 1

do shell script "networksetup -setnetworkserviceenabled Ethernet off" user name "<your account>" password "<your password>" with administrator privileges
do shell script "networksetup -setnetworkserviceenabled Ethernet on" user name "<your account>" password "<your password>" with administrator privileges

Previous post
Renewing APNS certificates (iOS) This article assumes that you already have a valid Developer license and an iOS application setup with push notifications. You can find more
Next post
Raspberry Pi Setup Yesterday I got my Raspberry Pi and this post serves as reference for myself for whenever I need to setup a new SD card for it. Download latest
This blog is powered by Blot