disable then enable the Ethernet with a script when the NIC is frozen

There’s a 2006 Mac Pro 1,1 running El Capitan without issues – except one: about once a month the network card is freezing.

That’s a real issue, as this Mac Pro acts as a file server, and usually I’m not there to fix it.

The fix is quite simple: in Apple / System Preferences / Network… the Ethernet 1 ( or 2) has to be disabled by clicking on the wheel and issuing the “Make the make service inactive” command, then after a few seconds turning it active again. Now in order to to this someone has to have physical access to it, so why not make it automatic with a script?

we need 3 files:

  1. a plist that the OS X will load at startup and call a script to be executed
  2. the script itself
  3. an AppleScript file that is executed after the main script – its purpose is sending a notification to a specified address (to you) notifying the NIC froze again – this AppleScript is optional of course

 

1. the script

the content of the .plist file is the following:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
<key>Label</key>
<string>com.MacMedicine.check_Ethernet</string>
<key>ProgramArguments</key>
<array>
<string>/scripts/check_Ethernet.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>30</integer>
</dict>
</plist>
save this file to /Library/LaunchDaemons
…with the following filename: com.MacMedicine.check_Ethernet.plist
permission has to be set to make it loadable by launchctl:
sudo chown root:wheel /Library/LaunchDaemons/com.MacMedicine.check_Ethernet.plist

 

the .plist will run the script every 30 seconds

2. the script

a note: this 2006 Mac Pro has two Ethernet ports and no Wi-Fi. I know the names “Ethernet 1” and “Ethernet 2” should be used as previously I ussed the following command:

networksetup -listallnetworkservices

that gave the following result:

An asterisk (*) denotes that a network service is disabled.

Ethernet 1
Ethernet 2
*FireWire

the content of the script is the following:

 

#!/bin/sh
# -q quiet
# -c nb of pings to perform
ping -c3 xxx.xxx.xxx.xxx > /dev/null
if [ $? -eq 0 ]
then
echo “MacMedicine: en0 or en1 is up”
#        syslog -s -l error “MacMedicine: en0 or en1 is up”
else
echo “MacMedicine: en0 or en1 had frozen”
sudo networksetup -setnetworkserviceenabled “Ethernet 1” off
        sudo networksetup -setnetworkserviceenabled “Ethernet 2” off
syslog -s -l error “MacMedicine: en0 or en1 had frozen, turning it OFF”
sleep 3
sudo networksetup -setnetworkserviceenabled “Ethernet 1” on
        sudo networksetup -setnetworkserviceenabled “Ethernet 2” on
syslog -s -l error “MacMedicine: turning en0 and en1 ON”
osascript /scripts/ethernet_was_down.scpt
fi

 

– change to red content with your details – this is the IP address of the router in my case. Make sure it’s pingable! You may also use any other IP…

– create a folder with name scripts at the base folder of Macintosh HD– now save this file to /Macintosh HD/scripts folder with the following filename: check_ethernet.sh

– let’s make this script executable:

chmod +x /Macintosh\ HD/scripts/check_ethernet.sh

there’s one more thing to do. The following command from the script can’t be run without sudo, so there’s sudo in front of it, but when the script is run the OS X will display a dialog to enter the admin password – and that’s not good in this case, as you will not know it, and you might not be at next to the computer.

sudo networksetup -setnetworkserviceenabled “Ethernet 1” off

There may be another way to fix it but here’s one – we will make an exception and allow this command to be run without the need of entering password. For this we have to modify the sudoers file

In terminal:

sudo pico /etc/sudoers

navigate below the following lines:

## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL

insert the third line:

## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
%admin ALL = (ALL) NOPASSWD: /usr/sbin/networksetup

hit Ctrl+O to save the changes, and hit Enter to finish the save
hit Ctrl+X to exit pico

 

3. the AppleScript

open the Script Editor from the Utilities folder and create + save this script to /Macintosh HD/scripts folder with the following filename: ethernet_was_down.scpt

 

set recipientName toyour name

set recipientAddress toyour.email.address@example.com

set theSubject tocompany – servername: Ethernet was down

set theContent toooops…

tell application “Mail”

##Create the message

set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

##Set a recipient

tell theMessage

make new to recipient with properties {name:recipientName, address:recipientAddress}

##Send the Message

send

end tell

end tell

 

change to red content with your details

 

finishing touches

now that everything is in place we have to tell OS X to load the plist and execute the scipt in the future.

in terminal:

sudo launchctl load -w /Library/LaunchDaemons/com.MacMedicine.check_ethernet.plist

 

how to test if it’s running and working properly?

change the following line in script to an IP that’s not pingable:

ping -c3 xxx.xxx.xxx.xxx > /dev/null

unload the .plist by issuing this command in Terminal

sudo launchctl unload -w /Library/LaunchDaemons/com.MacMedicine.check_ethernet.plist

now load it again

sudo launchctl load -w /Library/LaunchDaemons/com.MacMedicine.check_ethernet.plist

 

If it runs as expected, then because it’s not able to ping the test IP, it will output an error into Console like this:

6/24/18 1:59:02.846 PM syslog[662]: MacMedicine: en0 or en1 had frozen, turning it OFF

then a few seconds later:

6/24/18 1:59:06.129 PM syslog[712]: MacMedicine: turning en0 and en1 ON

…so the script runs smootly, you can revert the test IP to a working one.