Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # version: 1.0.0, 21-jan-2022, by eibgrad
- # href: https://tinyurl.com/3w4mfv8b
- SCRIPTS_DIR='/jffs/etc/config'
- SCRIPT="$SCRIPTS_DIR/ddwrt-wanip-notify.startup"
- mkdir -p $SCRIPTS_DIR
- cat << 'EOF' > $SCRIPT
- #!/bin/sh
- DEBUG=; set -x # comment/uncomment to disable/enable debug mode
- (
- # ------------------------------ BEGIN OPTIONS ------------------------------- #
- # how often to check (in secs) for a change in wan ip
- INTERVAL=300
- # smtp options
- USER='sender@gmail.com'
- PASS='XXXXXXXX'
- FROM_NAME='sender@gmail.com'
- TO='recipient@gmail.com'
- TO_NAME='recipient@gmail.com'
- SERVER='smtps://smtp.gmail.com:465'
- SUBJECT='WAN IP has changed!'
- # ------------------------------- END OPTIONS -------------------------------- #
- # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
- MESSAGE="/tmp/tmp.$$.message"
- format_message() {
- cat << EOM > $MESSAGE
- From: "$FROM_NAME" <$USER>
- To: "$TO_NAME" <$TO>
- Subject: $SUBJECT
- Current WAN IP: $curr_wan_ip
- Previous WAN IP: $prev_wan_ip
- EOM
- }
- send_message() {
- format_message
- curl --ssl-reqd \
- --url "$SERVER" \
- --user "$USER:$PASS" \
- --mail-from "$USER" \
- --mail-rcpt "$TO" \
- --upload-file "$MESSAGE" \
- $([ ${DEBUG+x} ] || echo '--silent')
- }
- prev_wan_ip='N/A'
- # wait for *reliable* internet connection
- until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
- while :; do
- curr_wan_ip="$(nvram get wan_ipaddr)"
- # send notification if wan ip changes
- if [ "$curr_wan_ip" != "$prev_wan_ip" ]; then
- echo "info: wan ip changed to $curr_wan_ip"
- if send_message; then
- echo "info: notification sent to $TO"
- else
- echo "error: failed to send notification ($?)"
- fi
- prev_wan_ip="$curr_wan_ip"
- fi
- sleep $INTERVAL
- done
- ) 2>&1 | logger $([ ${DEBUG+x} ] && echo '-p user.debug') \
- -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
- EOF
- chmod +x $SCRIPT
- echo "installed: $SCRIPT"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement