This is an alternative to the other script I posted. This one runs in the background and tests the network connection every two minutes to determine if it is using an Ethernet connection or wireless. If on Ethernet, it enables Time Machine; when the connection switches to wireless it disables Time Machine.
Step 1: System Check
While connected to Ethernet, you will need to run one command manually to confirm which interface Ethernet is assigned to. Run this command:
ifconfig
It should output a screen full of information. What you are looking for is a section labeled en
with a number after it, and whose last line is status: active
, like this:
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=b<RXCSUM,TXCSUM,VLAN_HWTAGGING>
ether 00:25:00:ef:30:3c
inet6 fe80::225:ff:feef:303c%en1 prefixlen 64 scopeid 0x5
inet 192.168.1.68 netmask 0xffffff00 broadcast 192.168.1.255
media: autoselect (100baseTX <full-duplex>)
status: active
Note the number next to en
at the beginning - this is the interface that your Ethernet connection is running on.
Step 2: Create the AppleScript Application
In the script below, where it says set wired_interface to "0"
, change the 0
to the number next to en
in the above output. (It should be 0; if you are on a Mac Pro, it may be 1.) Also in the below script, at the top where it says myusername
, substitute your own Mac username.
-- Edit variables here --
global current_username
set current_username to "myusername"
global wired_interface
set wired_interface to "0"
-- DO NOT EDIT BELOW THIS LINE --
global last_connection
set last_connection to "wireless"
on idle
if (do shell script "ifconfig en" & wired_interface & " | awk '/inet/ {print $2}'") is not equal to "" then
-- on ethernet
if last_connection is equal to "wireless" then
-- turn TM on
-- else do nothing, we're still on ethernet
set last_connection to "ethernet"
do shell script "sudo /Users/" & current_username & "/TMSwitch/TM_On.csh"
end if
else
-- on wireless
if last_connection is equal to "ethernet" then
-- turn tm off
-- else do nothing, we're still on wireless
set last_connection to "wireless"
do shell script "sudo /Users/" & current_username & "/TMSwitch/TM_Off.csh"
end if
end if
return 120
end idle
⌘+s to save. In the Save property sheet, set the File Format
as Application
, and check the box for Stay open after run handler
. Save it wherever you like - Desktop, or Applications Folder - it really doesn't matter, just know where you saved it.
Step 3: Create the Shell Scripts
Next, open Terminal. Type the following commands:
cd ~/
mkdir TMSwitch
cd TMSwitch
pico TM_On.csh
Paste the following line in:
defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool TRUE
Press control+x, type y and press return to save and exit. Then run this command:
pico TM_Off.csh
And in this file paste the following line:
defaults write /Library/Preferences/com.apple.TimeMachine AutoBackup -bool FALSE
Again, control+x, then y and return to save and exit.
Next, enter these commands:
chmod 4555 TM_O*.csh
chown root TM_O*.csh
Step 4: Setting Up sudo
to Run Without a Password
Letting the Terminal command sudo
run without a password can be very dangerous. That's why the steps above created the shell scripts in their own directory, so what can actually be run is limited.
Enter the following command in Terminal:
sudo pico /etc/sudoers
Then enter your administrator password when prompted.
This may bring you to a (mostly) blank screen, or it may have some text in it. If it's blank - that's fine. You'll just paste the below line at the top. If text already exists, that's also fine; use your down arrow to go right below the lines already in the # User privilege specification
section, as seen in the below screenshot.
Here, add the following line:
<yourusername> ALL = NOPASSWD: /Users/<yourusername>/TMSwitch/*
In both places where <yourusername>
appears, replace it with your Mac username. Press control + x, type y and press return to save and exit.
Test that these files turn Time Machine on and off by running the following command (assuming Time Machine is currently on):
sudo ./TM_Off.csh
After a moment the Time Machine icon in the menubar should turn gray, indicating Time Machine is turned off. (You may need to click the icon for it to reflect the change). Assuming this works, run this command:
sudo ./TM_On.csh
And Time Machine should be re-enabled.
And Off You Go
Run the application you created in AppleScript Editor above, and it will remain open, enabling and disabling Time Machine as your connection switches from Ethernet to wireless and back. To disable switching, simply close the AppleScript application (right-click on the icon in the Dock and choose Quit).
Best Answer
Selfcontrol
Selfcontrol is an app build for a different purpose, it disables your network connectivity for a certain amount of time to keep you from visiting social websites and keep you more productive. It makes it possible for you to create a
white-list
or ablack-list
to allow or block certain ip-adress/sites.So I login to the server, set Selfcontrol to 1h. Now my internet connection from my server is disabled for 1h and will automatically come back afterwards.