Record your dynamic WAN IP addresses
A launchd + shell script exercise to record your DSL router's dynamic WAN IP addresses. Requires some customization on your part. Use at your own risk.
1. create the laund item in /Library/LaunchDaemons
2. create a shell script that will be run by the launchd item at the specified intervals in seconds (here: every 20 seconds)
1. create the laund item in /Library/LaunchDaemons
# yourname=$(/usr/bin/logname) /usr/bin/sudo /usr/bin/nano /Library/LaunchDaemons/net.$(/usr/bin/logname).wanip.update.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <true/> <key>GroupName</key> <string>yourname</string> <key>Label</key> <string>net.yourname.wanip.update</string> <key>ProgramArguments</key> <array> <string>/Users/yourname/Library/wanip.sh</string> </array> <key>RunAtLoad</key> <true/> <key>StartInterval</key> <integer>20</integer> <key>UserName</key> <string>yourname</string> </dict> </plist> #------------------------- /usr/bin/groups /usr/bin/sudo /usr/sbin/chown root:wheel /Library/LaunchDaemons/net.yourname.wanip.update.plist #/usr/bin/sudo /usr/sbin/chown root:admin /Library/LaunchDaemons/net.yourname.wanip.update.plist /usr/bin/sudo /bin/chmod 0644 /Library/LaunchDaemons/net.yourname.wanip.update.plist # after creating ~/Library/wanip.sh below /usr/bin/sudo /bin/launchctl load -w /Library/LaunchDaemons/net.yourname.wanip.update.plist 2>/dev/null #/usr/bin/sudo /bin/launchctl unload -w /Library/LaunchDaemons/net.yourname.wanip.update.plist 2>/dev/null /usr/bin/sudo /bin/launchctl list /usr/bin/sudo /usr/bin/fs_usage | /usr/bin/egrep -i wanip
2. create a shell script that will be run by the launchd item at the specified intervals in seconds (here: every 20 seconds)
#!/bin/sh # cat ~/Library/wanip.sh (/Users/yourname/Library/wanip.sh) # /usr/sbin/chown $(/usr/bin/logname):$(/usr/bin/logname) ~/Library/wanip.sh # /bin/chmod 0744 ~/Library/wanip.sh # write stdout & stderr to console.log in /Library/Logs/Console/ exec >/dev/console 2>&1 declare last_line_closed last_line_offline newfile old_wanip time wanip declare IF='en0' declare wanip_record_file="${HOME}/Library/wanip_record.txt" # try to find your router_wanip_site by surfing to the IP addresses returned by the following commands: # route -n get default | egrep interface | awk '{print $NF}' # ipconfig getoption en0 router # ipconfig getoption en0 domain_name_server declare router_wanip_site='http://xxxx.xx/xxx.htm' #declare router_wanip_site='http://checkip.dyndns.org' # alternative /bin/sleep 3 /usr/sbin/ipconfig waitall if [[ "$(/sbin/route -n get default | /usr/bin/egrep interface | /usr/bin/awk '{print $NF}')" == "${IF}" ]]; then /usr/bin/curl -I -L -s --max-time 10 "${router_wanip_site}" 1>/dev/null if [[ $? -ne 0 ]]; then exit 0; fi # match first IP address with egrep wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \ /usr/bin/egrep -o -m 1 ' ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')" if [[ $? -ne 0 ]]; then exit 0; fi wanip="${wanip// /}" # alternative with sed for matching a line with a characteristic string plus IP address #wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \ #/usr/bin/sed -E -n -e '/STRING: /{s/^.+ ([[:digit:]\.]+).*$/\1/p;q;}')" #if [[ $? -ne 0 ]]; then exit 0; fi time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)" if [[ -n "${wanip}" ]]; then old_wanip="$(/usr/bin/sed -E -n -e '$,$s/^.+ ([[:digit:]\.]+).*$/\1/p' "${wanip_record_file}")" if [[ "${wanip}" == "${old_wanip}" ]]; then exit 0; fi echo "${time} ${wanip}" >> "${wanip_record_file}" else last_line_closed="$(/usr/bin/sed -E -n -e '$,$s/^.+ (closed).*$/\1/p' "${wanip_record_file}")" if [[ -n "${last_line_closed}" ]]; then exit 0; fi echo "${time} connection closed" >> "${wanip_record_file}" fi else last_line_offline="$(/usr/bin/sed -E -n -e '$,$s/^.+ (offline).*$/\1/p' "${wanip_record_file}")" if [[ -n "$last_line_offline" ]]; then exit 0; fi time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)" echo "${time} offline" >> "${wanip_record_file}" fi if [[ $(/usr/bin/stat -f %z "${wanip_record_file}") -gt 31457280 ]]; then newfile="${wanip_record_file}-$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)" /bin/mv "${wanip_record_file}" "${newfile}" fi exit 0