What's new

amtm I want to leverage the email function within amtm

Ripshod

Part of the Furniture
I'm adapting a little script to notify me when a file changes (comparing md5 checksums) but it was originally written to use sendmail (built into asuswrt/busybox). Needless to say it fails. What I'd like to do is use the email functionality in amtm to handle emails, but I just don't know where to start (my sending email is already set up and working in amtm).
Any pointers?
 
I'm adapting a little script to notify me when a file changes (comparing md5 checksums) but it was originally written to use sendmail (built into asuswrt/busybox). Needless to say it fails. What I'd like to do is use the email functionality in amtm to handle emails, but I just don't know where to start (my sending email is already set up and working in amtm).
Any pointers?
Look at the code of other 3rd-party scripts such as MerlinAU or BACKUPMON that have incorporated this functionality.
 
I'm adapting a little script to notify me when a file changes (comparing md5 checksums) but it was originally written to use sendmail (built into asuswrt/busybox). Needless to say it fails. What I'd like to do is use the email functionality in amtm to handle emails, but I just don't know where to start (my sending email is already set up and working in amtm).
Any pointers?
Like to see a clear answer on this myself. In the meantime here is a snippet of what I came up with that works.

Code:
[email protected]

      NEW_ADDR=`cat /opt/ipchange/last_ip`
        {
         echo From: rt-be88u.dbonenet.com
         echo To: [email protected]
         echo Subject: External IP Address Change
         echo
         echo -n New IP ADDRESS:
         echo ' [ '$NEW_ADDR' ] '
        } | /usr/sbin/sendmail  -H 'exec openssl s_client -quiet -verify_quiet -tls1_3 -CAfile /opt/etc/ssl/cert.pem -connect mail.dbonenet.com:465' $M_ADDR 2>/dev/null
 
Look at the code of other 3rd-party scripts such as MerlinAU or BACKUPMON that have incorporated this functionality.

MerlinAU actually has all the email code integrated directly into the add-on, which may be a bit trickier to digest, but removes any dependencies on external code for MerlinAU.
However @Viktor Jaep 's BACKUPMON uses the Shared Email Library from @Martinski , which is what I would recommend as a starting point and is the simplest implementation.

The library is specifically designed to be a detached "all-in-one" solution other scripts/add-ons can call on to leverage email functionality:
 
Last edited:
@Ripshod ... if you've downloaded BACKUPMON, RTRMON, VPNMON-R3 or TAILMON, and enabled email notifications for any of these, then you will have downloaded the shared email library... you should be able to find it under /jffs/addons/shared-libs. Let me know if I can help with anything. ;)
 
@Ripshod, another option for you is to copy and use the code written by AMTM's author himself, @thelonelycoder within either one of the two scripts (if you have them installed from the AMTM menu)

"Scripts update notification" script located at:
/jffs/addons/amtm/sc_update.mod
OR
"Firmware update notification" script located at:
/jffs/scripts/update-notification

However, if you have neither of them installed in your router, you can search for the source files, or just install the scripts temporarily to copy the code.
 
@Ripshod, another option for you is to copy and use the code written by AMTM's author himself, @thelonelycoder within either one of the two scripts (if you have them installed from the AMTM menu)

"Scripts update notification" script located at:

OR
"Firmware update notification" script located at:


However, if you have neither of them installed in your router, you can search for the source files, or just install the scripts temporarily to copy the code.
Spot on, short & sweet and easy to 'hack' to send whatever you want !!!

:)
 
Sorted. I ended up with a "hack" of the curl method from wicens. So simple really.

My first asuswrt script, at my age 😂
 
Last edited:
Sorted. I ended up with a "hack" of the curl method from wicens. So simple really.

My first asuswrt script, at my age 😂
Never too late! Lol!
 
Dare I call it FileMON? 🤪
A little tidy up and some obfuscation and here it is - I'll stick with what it's called now. It's ready to go, as long as your email settings are done in amtm (em).Just set the file you want to watch (including the directory) where the script says <INSERT THE NAME OF YOUR FILE HERE>

/jffs/scripts/check_file_change.sh
Bash:
#!/bin/sh

# Load amtm email configuration
. /jffs/addons/amtm/mail/email.conf

# Define variables for your message
SUBJECT="Router Alert: $(nvram get computer_name)"
BODY="Your home router (RT-BE88U)  has detected an event at $(date). Your watched file has changed."
TEMP_MAIL="/tmp/mail_body.txt"

# Path to the file you want to monitor
FILE_TO_WATCH="<INSERT THE NAME OF YOUR FILE HERE>"

# Store the last known MD5 checksum
LAST_SUM=$(cat /tmp/file_checksum.txt 2>/dev/null)
CURRENT_SUM=$(md5sum "$FILE_TO_WATCH" | awk '{print $1}')

# Compare the file MD5s
if [ "$LAST_SUM" != "$CURRENT_SUM" ]; then

# Construct the email
echo "Subject: $SUBJECT" > "$TEMP_MAIL"
echo "From: $FROM_NAME <$FROM_ADDRESS>" >> "$TEMP_MAIL"
echo "To: $TO_ADDRESS" >> "$TEMP_MAIL"
echo "" >> "$TEMP_MAIL"
echo "$BODY" >> "$TEMP_MAIL"

# Send via curl using amtm's stored encrypted credentials
/usr/sbin/curl --url "$PROTOCOL://$SMTP:$PORT" \
  --mail-from "$FROM_ADDRESS" \
  --mail-rcpt "$TO_ADDRESS" \
  --upload-file "$TEMP_MAIL" \
  --ssl-reqd \
  --user "$USERNAME:$(/usr/sbin/openssl aes-256-cbc $emailPwEnc -d -in /jffs/addons/amtm/mail/emailpw.enc -pass pass:ditbabot,isoi)" \
  $SSL_FLAG

# Cleanup
rm "$TEMP_MAIL"

~ Close the loop
fi
I set this to run every 5 minutes using
Code:
cru a check_file_change "*/5 * * * * /jffs/scripts/check_file_change.sh"
I also added the cru to services-start so it runs from boot.
 
Last edited:
Dare I call it FileMON? 🤪
A little tidy up and some obfuscation and here it is - I'll stick with what it's called now. It's ready to go, as long as your email settings are done in amtm (em).Just set the file you want to watch where the script says <INSERT THE NAME OF YOUR FILE HERE>

/jffs/scripts/check_file_change.sh
Bash:
#!/bin/sh

# Load amtm email configuration
. /jffs/addons/amtm/mail/email.conf

# Define variables for your message
SUBJECT="Router Alert: $(nvram get computer_name)"
BODY="Your home router has detected an event at $(date). A file has changed."
TEMP_MAIL="/tmp/mail_body.txt"

# Path to the file you want to monitor
FILE_TO_WATCH="<INSERT THE NAME OF YOUR FILE HERE>"

# Store the last known MD5 checksum
LAST_SUM=$(cat /tmp/file_checksum.txt 2>/dev/null)
CURRENT_SUM=$(md5sum "$FILE_TO_WATCH" | awk '{print $1}')

# Compare the file MD5s
if [ "$LAST_SUM" != "$CURRENT_SUM" ]; then

# Construct the email
echo "Subject: $SUBJECT" > "$TEMP_MAIL"
echo "From: $FROM_NAME <$FROM_ADDRESS>" >> "$TEMP_MAIL"
echo "To: $TO_ADDRESS" >> "$TEMP_MAIL"
echo "" >> "$TEMP_MAIL"
echo "$BODY" >> "$TEMP_MAIL"

# Send via curl using amtm's stored encrypted credentials
/usr/sbin/curl --url "$PROTOCOL://$SMTP:$PORT" \
  --mail-from "$FROM_ADDRESS" \
  --mail-rcpt "$TO_ADDRESS" \
  --upload-file "$TEMP_MAIL" \
  --ssl-reqd \
  --user "$USERNAME:$(/usr/sbin/openssl aes-256-cbc $emailPwEnc -d -in /jffs/addons/amtm/mail/emailpw.enc -pass pass:ditbabot,isoi)" \
  $SSL_FLAG

# Cleanup
rm "$TEMP_MAIL"

# Close the loop
fi
I set this to run every 5 minutes using
Code:
cru a check_file_change "*/5 * * * * /jffs/scripts/check_file_change.sh"
so it checks the file every 5 minutes. I also added the cru to services-start so it runs from boot.
Thanks @Ripshod! FILEMON sounds like the way to go! LOL. :p Could you please explain the use case? Why are you watching for a file change? Does this have something to do with the situation you were dealing with as of late?
 
Does this have something to do with the situation you were dealing with as of late?
You clearly remember.
I was experiencing (self induced) worries over some strange IPs logging into my router's GUI, so rather than relying on checking through the large log I made a filter for syslog-ng:
Code:
# log all httpd logs to httpd.log

destination d_httpd {
    file("/opt/var/log/httpd.log");
};

filter f_httpd {
    program("HTTPD") or
    program("httpd") or
    program("httpds");
};

log {
    source(src);
    filter(f_httpd);
    destination(d_httpd);
    destination(log_server);
    flags(final);
};

#eof
This was great. It produced a seperate log for any http(s) activity making it easier to check.
I thought to myself,I should be able to get notifications to my phone if someone does login to the GUI so the idea was born. Saves checking logs, let the router tell me.
 

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top