WAN IP change - suggestions/tips tricks/beta testers

  • ATTENTION! As of November 1, 2020, you are not able to reply to threads 6 months after the thread is opened if there are more than 500 posts in the thread.
    Threads will not be locked, so posts may still be edited by their authors.
    Just start a new thread on the topic to post if you get an error message when trying to reply to a thread.

Maverickcdn

Senior Member
Hello everyone... So I've been killing time writing possibly the most overly complicated/really featureless script for users of RMerlin's firmware. But I wanted to contribute back something within my wheel house.

What is it you ask... simply a preconfigured script to send you an Email notification if/when your WAN IP changes. I prefer not to use a DDNS service and find this works great for my needs. This script is targeted at those that dont want to learn about creating/writing their own notification script or just want something easily installed/configured and does what its supposed to.

Started out very simple (about 120 lines) and worked great for 2+ years. After a recent upgrade I incorrectly named the entry in cron and got caught with a WAN IP change and no notification, what started as simply adding cron entry/checking functions grew to this ridiculous script I now have. I figure Ive gotten this far I might as well see it out that its usable to my desires from what I intended when I started

But here in lies my dilemma... Im a total noob!

Before I go through the effort of making this a supported release and uploading this to a github account I wanted to get other peoples opinions and maybe a few people willing to try it out and give their feedback. Consider this an alpha/beta release. The core functions have worked flawlessly for years for me (beta) but now its become quite elaborate (back to alpha status). I have been running this myself for 2 weeks now and have noticed no ill effects.

Most of the functions/techniques are pulled from other SNBforum contributors (you're all a wealth of knowledge) and from posts from other users here (sendmail/curl formats etc)

My major concerns

1) basic script command usage/formatting etc. Everything works but is there a better way....
2) smtp_plain_auth login is untested, I dont have an Email provider that allows plain_auth login.
3) script is currently tested with 4 major email providers total, would like to see more tested
4) is running getrealip.sh every time the bestway to check WAN IP, seems to me I might be able to check the nvram var it sets, but is this more robust and wont interrupt any firmware operations.
5) confirmation that a DHCP ip change does not trigger a wan-event (untested) but seems to me, no wan-events are triggered with WAN DHCP, the scripts usage of cron is nearly entirely based on my observation of no wan-events when my modem/router negotiate DHCP (and potentially changes)
6) init-start or services-start for cru entry adds?
7) better to save all user settings in a file in addons folder or okay to keep them retained in single file and overwriting...?

The scripts is nearly 1800 lines as there is a lot of terminal screen output so I dont expect much of anyone, but if you're willing to skim through and point out anything that stands out to you it would be greatly appreciated!

It could be more robust but as long as you follow the instructions and correctly input your details (theres no email format checking etc) it will work.

And if you're bold enough to try it and report back thats even better! Only works on 384.15 and newer though as it does use wan-event which was introduced then and Im unsure on which firmware introduced getrealip.sh. Its possible to have this work with versions of Johns fork and older than 384.15 but would require probably another 200ish lines of code. Maybe another coder in training could fork this to work with older versions :)

WICENS - WAN IP Change Email Notification Script.

Run with option 'help' for more info about this script.

This script when configured will send an Email (1-4) at variable intervals
X(second/minute/hour/day) to your Email notifying you when your WAN IP has
changed.

Supports Gmail, Hotmail, Outlook

IMPORTANT NOTE: your Email user name and password are stored as plain text
within the script. If you dont practice good security habits around your
router ssh access, this script might not be for you.

Uses firmware built in getrealip.sh to retrieve your WAN IP using Google
STUN server. This will show your WAN IP regardless if your router is behind
NAT (unbridged modem) or not.

Executed by settable minute || hour cron run (default 31min) for checking
your current WAN IP. Also executed by 'wan-event connected' trigger. A DHCP
renewal when your IP changes may not trigger a 'wan-event connected' event.

All cron/wan-event entries are automatically generated upon completion of
your Email user login information and is double checked with every run.

'wan-event connected' execution has a 90 sec delay from trigger to WAN IP
check as we wait for DHCP/NTP to catch up on reboots.

SMTP Email send formats available:
sendmail - StartTLS v1.1 higher (eg. GMail port 587)
sendmail - StartTLS v1 only
curl - SSL (eg GMail port 465)
sendmail - SMTP plain auth (no encryption)
sendmail - ISP based (no password reqd, generally port 25)

IMPORTANT - If using GMail, you must enable 'insecure app' access to your
GMail account. If you use 2factor authentication you must setup an assigned
password in GMail for this script to use.

### Technical ###

Script generates a lock file in /tmp called wicens.lock to prevent
duplicate runs as well as another file in /tmp called wicenssendmail.lock
when sending Email notifications. Script will automatically remove (with
cron) stale lock files if original starting process no longer exists or
lock files are over age limit.

Sendmail/Curl output for Emails is saved to /tmp/wicenssendmail.log for
debugging if neeeded. This file can be viewed by running this script with
'error' option

Sendmail doesnt always return an error code on a misconfiguration so false
send success can occur. If script says email has sent but no email received
use '/jffs/scripts/wicens.sh error' to read sendmail output for errors

The script does not update its saved WAN IP until the script has completed
sending all notifications and adds to the Email message of success or
failure in updating it, so in the event of message failure it should run
again with next cron run and attempt to send again.

Every Sunday the script will write to log the number of calls from cron.

If you use any send format other than smtp_isp_nopswd or smtp_plain_auth
the required file Equifax_Secure_Certificate_Authority.pem will be
downloaded and added to /jffs/configs.

Thank you for using this script.

SEE POST #15 for new Beta link

With the usual prerequisite script necessities applied, create a file in /jffs/scripts called wicens.sh, copy and paste the pastebin text to the file, then run 'sh /jffs/scripts/wicens.sh setup' to start.
Use 'sh /jffs/scripts/wicens.sh test' to ensure the sending of Emails works properly.

To truly test the script is, run through the setup, then go back in the file and edit the saved_wan_ip variable to something different then let the script run with cron or manually invoke it. If more than 1 Email notification is set though, the script will sleep the full time set for the 1/2, 2/3 and 3/4 email interval time etc.

Look forward to hearing some critiques/criticisms/feedback and hopefully some tester feedback.
 
Last edited:

dave14305

Part of the Furniture
Dare I click through the pastebin warning? Eroticode? ;)

1604351417391.png
 

Maverickcdn

Senior Member

Maverickcdn

Senior Member
So I know theres issues still and one of them is the input menus, I got so gung ho I totally forgot about them

They are extremely intolerant to incorrect entries with blank entries (reusing saved settings) and Y or N confirmation and when not hitting Y or N

I will update all the user entry functions to use loops and eliminate the F_y_or_n function to create proper invalid entry responses and restarts of those input functions

User input functions started from F_build_settings, instead of a plain function call it will be in an until loop
eliminates issues on blank entries

Code:
F_build_settings() {
    ...
    # old F_send_to addr
    until F_send_to_addr ; do : ; done  # uses return codes for control
    ...
}

And all user input functions will use a while loop for Y or N confirmation instead of the old F_y_or_n function

Code:
F_send_to_addr() {
    .... (after reading user input)
    [ -z "$user_send_to_addr" ] && [ -z "$send_to_entry" ] ... (instead of finishing recalling function, return 1)
    # old F_y_or_n call
    while true; do
        printf "%b Is %b%s%b correct? Y or N" "$tCHECK" "$tGRN" "$send_to_entry" "$tCLR"
        # restart_entry="F_send_to_addr"
        # F_y_or_n
    
        read -rsn1 "yesorno"
        case "$yesorno" in
            y|Y) sed -i "1,/user_send_to_addr=.*/{s/user_send_to_addr=.*/user_send_to_addr=\"$send_to_entry\"/;}" "$script_name_full"
             user_send_to_addr="$send_to_entry"
            ;;
            n|N) return 1 ;;  # return code 1 will restart input with initial until loop
        *) F_terminal_check_fail "Invalid Entry, Y or N"
            continue
        ;;
        esac
        break
    done

} ### send_to_addr
 

Maverickcdn

Senior Member
Yay, feedback! Thanks haha!

Many revisions made. Ill get it up on github sometime this week
 

JGrana

Senior Member
Hi @Maverickcdn
I like how you labeled functions and their use for screen display and input.

Not to start a GUI war, but the more I read many of these bash scripts I have often wondered why no one has thought about using Linux dialog.


Its available in Entware. It's pretty dated - but is still used quite a bit in various Linux distributions before an X based windows manager is installed/available. I especially like functions like radiolist, infobox, inputbox, etc.

I would think it would reduce the size of scripts.

Just curious.
 

Maverickcdn

Senior Member
Hi @Maverickcdn
I like how you labeled functions and their use for screen display and input.

Thanks, after watching some youtube videos of various coding, I learned the one thing most instructors harp on is lack of descriptive variables/functions, plus I just find it easier to read through/follow along with that way

Not to start a GUI war, but the more I read many of these bash scripts I have often wondered why no one has thought about using Linux dialog.

Its available in Entware. It's pretty dated - but is still used quite a bit in various Linux distributions before an X based windows manager is installed/available. I especially like functions like radiolist, infobox, inputbox, etc.

Remember BASH is only available through entware on this platform, most user scripts here are written as Unix Shell scripts and I could be wrong but I dont think any are dependant on BASH being installed through entware. If I remember correct 384.19 is based on 1.25 Busybox (Ash). So there are many bashisms or BASH based solutions that cant be used here without installing entware

I wrote this more as practice for myself, and tried to keep it with the least amount of prerequisites needed. Actually nothing is required other than scripts enabled/the script installed/ and it will download the required SSL cert if needed.

But trust me I know where you are coming from... its only in the last few years Ive grown comfortable with command line/scripting. Before I was a GUI only guy unless there was step by step instructions for using the command line. I think this script proves how my comfort level has grown working behind the scenes of the GUI.

If covid continues to disrupt everything in 2021 maybe Ill learn about creating my own GUI page for this.
 
Last edited:

JGrana

Senior Member
I hear you - and can appreciate your idea of less dependencies the better.

But,,,, since Entware is a requirement for many of the scripts, what’s the harm in a few more applications ;-)
BTW, your comfort level has grown fine - nicely done script!
 

dev_null

Regular Contributor
@Maverickcdn thanks for this. I have a script that runs on my PBX that checked for IP address changes (because some providers use IP authentication), but that script does not give the previous IP, which is critical to verify the notification isn't a "false positive". That your script includes both old and new IP is very helpful.

Only one suggestion since you prompt for several attributes: consider prompting for a customized subject line, so that users can include some designation of location/router at a glance (I edited the line within the script already).
 
Last edited:

Maverickcdn

Senior Member

Maverickcdn

Senior Member
Long list of changes made.... any other requests?? Still editing 1 or two things and Ill get a release up in the next day or two (would like to say tonight but not likely)

added ability to CC a second email address for all messages
added option 'subject' for custom subject, can add current/saved WAN IP or device model
added option 'custom' to add a line of plain text to add to email
added option 'sample' to show email output on terminal
added option 'color' toggle for terminal

- removed F_y_or_n
- all user entries started by loops/controlling invalid entries
- all optional entries with y or n also updated
- disabled auto ssl_download, may not be necessary
- updated init-start entries to services start instead
- moved F_reset_do autorun removal commands to F_disable_auto_run
- added disable option to only remove autorun entries
- added check for entry in wan-event for removal that was missing before removal (now F_disable_auto_run)
- corrected all rm commands
- changed F_smtp_pswd to be no longer readable anywhere, and double verify
-saved pswd as encrypted text (obfuscated) no longer plain text (decrypt still in script though)
-obfuscated pswd entry, added second pswd confirmation entry

changed test mode to 1 message by default
fixed/moved wicenssendmail.lock removal in F_send_mail
added wancall count to sunday log
add 3 attempt loop to F_getrealip call
removed 10s interval var in test - was unused, added 1,2,3 intervals for Email text
restated 31 min default cron everywhere
moved F_message_intervals_entry call to F_build_settings
fixed typo in F_reset_do for tmp file removal
only chmod services-start/wan-event if not already a+rx in F_auto_check
created entry header function for user inputs

fixed F_terminal_separator spelling mistake
updated default subject line to include device_model
some beautifying of terminal text
some better wording in terminal outputs and uniform responses
fixed function desc ending names (remove, count. test. pswd)
removed/added space in F_send_format_tls_v1 command
added log output to watcher for getrealip.sh if it does kill it
changed timeout for getrealip to 5 secs
moved vars for F_message_intervals_entry to F_build_settings
one lined device_model var
added build extended fw no to display
compressed some global vars
one lined all terminal print functions/global and some if statements
optioned full counts reset for 'wan ip change date' reset
removed line spacer in email for script update info
changed welcome message to all setup menus
calling F_internet_check with until loop for better restart control
removed exit on stale lock removal - run after removal
changed install date update to optional on edit run of setup
added output for failed autorun removal on uninstall
moved sendmail/curl logging in front of arguements and append instead of write

F_ssl_download put wget in own function, response in if statement directly instead of $?
disabled ssl_download, seems it may not be necessary and removed refs to certs
fixed missing cert removal on uninstall - probably obsolete now anyway
add ssl option in case F_ssl_download is needed
 

dev_null

Regular Contributor
Looks great. You should consider getting the mods to move this to the ADD-ONS directory and create a PREFIX.

Someday maybe add it to AMTM!
 

maghuro

Very Senior Member
Any news on this? I'm so excited !

Suggestion - when receiving the email saying that the ip changed, is it possible to have something saying that "your ip lasted for 2 days and 5 hours", for example?
So we can know how much time does our leases last. Thanks
 

Maverickcdn

Senior Member
Any news on this? I'm so excited !

Suggestion - when receiving the email saying that the ip changed, is it possible to have something saying that "your ip lasted for 2 days and 5 hours", for example?
So we can know how much time does our leases last. Thanks

Hey, sorry life keeps getting in the way of my free time! ha That combined with my inability to leave the script alone and setting up a new i5 NUC server I havent got around to doing the git upload (still reading how to use git as well)

Ive made numerous changes to it as well, no longer is the script require arguments on the command line, I've created a main menu to operate from for all options built into the script.

The script already records changes so I will do some reading on calculating date diff and will see about adding this before I put up an official beta on git.

For now, heres the updated version I have currently, (pastebin will expire in 30days)

wicens 1.0B (No eroticode warning this time... yay!)

Same as before, cut paste into file called wicens.sh in /jffs/scripts. Script now aliases in your system, so first run requires running 'sh /jffs/scripts/wicens.sh', after first run script can be called by simply typing 'wicens' in the command line
 

maghuro

Very Senior Member
Hey, sorry life keeps getting in the way of my free time! ha That combined with my inability to leave the script alone and setting up a new i5 NUC server I havent got around to doing the git upload (still reading how to use git as well)

Ive made numerous changes to it as well, no longer is the script require arguments on the command line, I've created a main menu to operate from for all options built into the script.

The script already records changes so I will do some reading on calculating date diff and will see about adding this before I put up an official beta on git.

For now, heres the updated version I have currently, (pastebin will expire in 30days)

wicens 1.0B (No eroticode warning this time... yay!)

Same as before, cut paste into file called wicens.sh in /jffs/scripts. Script now aliases in your system, so first run requires running 'sh /jffs/scripts/wicens.sh', after first run script can be called by simply typing 'wicens' in the command line
Problem....
Screenshot_20201204-202230264.png
 

Maverickcdn

Senior Member

How are you transposing from pastebin? If I 'download' and rename I get the same issue, but if I copy/paste from Raw Data at the bottom I have no issue

Its either an issue with the transposing or something in my Alias code maybe
 

dave14305

Part of the Furniture

maghuro

Very Senior Member
How are you transposing from pastebin? If I 'download' and rename I get the same issue, but if I copy/paste from Raw Data at the bottom I have no issue

Its either an issue with the transposing or something in my Alias code maybe

I deleted everything and did it again and it worked.. Maybe some copy/paste issue.

Thanks for your work! It's keeping better!!!
Another sugestion: possibility to run a script WHEN IP change is detected.
 

maghuro

Very Senior Member
And maybe you can use another file (wicens.conf) to store user vars, because each time we update the script, we have to manually configure everything again... :)
 
Top