Kill a Program on Connecting to a specific WiFi – OSX

Hi folks. If you have the tendency, like me, to forget that you are on the corporate VPN, or leave a certain software open when you bring your laptop to work, this might be helpful to you too. It’s a small script which kills a program when you change your Wifi network. Script: #!/bin/bash function log { directory="/Users/<username>/wifi_detect" log_dir_exists=true if [ ! -d $directory ]; then echo "Attempting to create => $directory" mkdir -p $directory if [ ! -d $directory ]; then echo "Could not create directory. Continue to log to echo." log_dir_exists=false fi fi if $log_dir_exists ; then echo "$(date):$1" >> "$directory/log.txt" else echo "$(date):$1" fi } function check_program { to_kill="[${1::1}]${1:1}" log "Checking if $to_kill really quit." ps=$(ps aux |grep "$to_kill") log "ps => $ps" if [ -z "$ps" ]; then # 0 - True return else # 1 - False return 1 fi } function kill_program { log "Killing program" `pkill -f "$1"` sleep 1 if ! check_program $1 ; then log "$1 Did not quit!" else log "$1 quit successfully" fi } wifi_name=$(networksetup -getairportnetwork en0 |awk -F": " '{print $2}') log "Wifi name: $wifi_name" if [ "$wifi_name" = "<wifi_name>" ]; then log "On corporate network... Killing Program" kill_program "<programname>" elif [ "$wifi_name" = "<home_wifi_name>" ]; then # Kill <program> if enabled and if on <home_wifi> and if Tunnelblick is running. log "Not on corporate network... Killing <program> if Tunnelblick is active." if ! check_program "Tunnelblick" ; then log "Tunnelblick is active. Killing <program>" kill_program "<program>" else log "All good... Happy coding." fi else log "No known Network..." fi Now, the trick is, on OSX to only trigger this when your network changes. For this, you can have a ’launchd’ daemon, which is configured to watch three files which relate to a network being changed. ...

October 26, 2015 · 2 min · hannibal