eibgrad

ddwrt-wanip-notify-331321.sh

Jan 21st, 2022 (edited)
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.04 KB | None | 0 0
  1. #!/bin/sh
  2. # version: 1.0.0, 21-jan-2022, by eibgrad
  3. # href: https://tinyurl.com/3w4mfv8b
  4.  
  5. SCRIPTS_DIR='/jffs/etc/config'
  6. SCRIPT="$SCRIPTS_DIR/ddwrt-wanip-notify.startup"
  7.  
  8. mkdir -p $SCRIPTS_DIR
  9.  
  10. cat << 'EOF' > $SCRIPT
  11. #!/bin/sh
  12. DEBUG=; set -x # comment/uncomment to disable/enable debug mode
  13. (
  14. # ------------------------------ BEGIN OPTIONS ------------------------------- #
  15.  
  16. # how often to check (in secs) for a change in wan ip
  17. INTERVAL=300
  18.  
  19. # smtp options
  20. PASS='XXXXXXXX'
  21. FROM_NAME='[email protected]'
  22. SERVER='smtps://smtp.gmail.com:465'
  23. SUBJECT='WAN IP has changed!'
  24.  
  25. # ------------------------------- END OPTIONS -------------------------------- #
  26.  
  27. # ---------------------- DO NOT CHANGE BELOW THIS LINE ----------------------- #
  28.  
  29. MESSAGE="/tmp/tmp.$$.message"
  30.  
  31. format_message() {
  32. cat << EOM > $MESSAGE
  33. From: "$FROM_NAME" <$USER>
  34. To: "$TO_NAME" <$TO>
  35. Subject: $SUBJECT
  36.  
  37. Current  WAN IP: $curr_wan_ip
  38. Previous WAN IP: $prev_wan_ip
  39. EOM
  40. }
  41.  
  42. send_message() {
  43.     format_message
  44.  
  45.     curl --ssl-reqd \
  46.          --url "$SERVER" \
  47.          --user "$USER:$PASS" \
  48.          --mail-from "$USER" \
  49.          --mail-rcpt "$TO" \
  50.          --upload-file "$MESSAGE" \
  51.          $([ ${DEBUG+x} ] || echo '--silent')
  52. }
  53.  
  54. prev_wan_ip='N/A'
  55.  
  56. # wait for *reliable* internet connection
  57. until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done
  58.  
  59. while :; do
  60.     curr_wan_ip="$(nvram get wan_ipaddr)"
  61.  
  62.     # send notification if wan ip changes
  63.     if [ "$curr_wan_ip" != "$prev_wan_ip" ]; then
  64.         echo "info: wan ip changed to $curr_wan_ip"
  65.  
  66.         if send_message; then
  67.             echo "info: notification sent to $TO"
  68.         else
  69.             echo "error: failed to send notification ($?)"
  70.         fi
  71.  
  72.         prev_wan_ip="$curr_wan_ip"
  73.     fi
  74.  
  75.     sleep $INTERVAL
  76. done
  77.  
  78. ) 2>&1 | logger $([ ${DEBUG+x} ] && echo '-p user.debug') \
  79.     -t $(echo $(basename $0) | grep -Eo '^.{0,23}')[$$] &
  80. EOF
  81. chmod +x $SCRIPT
  82. echo "installed: $SCRIPT"
Advertisement
Add Comment
Please, Sign In to add comment