Macで無線APを監視してhostsを書き換え
<?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>Label</key> | |
<string>hosts_switch</string> | |
<key>LingonWhat</key> | |
<string>sh /Users/retlet/Dropbox/settings/script/hosts_switch/hosts_switch.sh</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>sh</string> | |
<string>/Users/retlet/Dropbox/settings/script/hosts_switch/hosts_switch.sh</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true /> | |
<key>StandardErrorPath</key> | |
<string>/tmp/foo</string> | |
<key>StandardOutPath</key> | |
<string>/tmp/bar</string> | |
<key>WatchPaths</key> | |
<array> | |
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string> | |
<string>/Library/Preferences/SystemConfiguration/com.apple.network.identification.plist</string> | |
</array> | |
</dict> | |
</plist> |
#!/bin/sh | |
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep -q "ssig33" | |
ap=$? | |
if [[ "${ap}" -eq 0 ]];then | |
ed - /etc/hosts <<EOF | |
1,20s/10\.2\.0\.1 hanpen/192.168.1.2 hanpen/ | |
1,20s/#192\.168\.1.2 hanpen\.mycloudnas\.com/192.168.1.2 hanpen.mycloudnas.com/ | |
w | |
q | |
EOF | |
else | |
ed - /etc/hosts <<EOF | |
1,20s/192\.168\.1.2 hanpen/10.2.0.1 hanpen/ | |
1,20s/192\.168\.1.2 hanpen\.mycloudnas\.com/#192.168.1.2 hanpen.mycloudnas.com/ | |
w | |
q | |
EOF | |
fi | |
dir=`dirname $0` | |
date=`date '+%Y-%m-%d %T'` | |
echo ${date} ${ap} >>${dir}/log |
# launchd setup | |
copy_dir="/Library/LaunchDaemons" | |
cp -f hosts_switch.plist ${copy_dir}/hosts_switch.plist | |
chmod 644 ${copy_dir}/hosts_switch.plist | |
chown root:wheel ${copy_dir}/hosts_switch.plist | |
launchctl unload ${copy_dir}/hosts_switch.plist | |
launchctl load ${copy_dir}/hosts_switch.plist |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment