Posted: Wed Dec 23, 2015 20:53 Post subject: Script for sending emails from router using a Gmail account
Hello,
I'm posting a script I put together to send emails from my R7000 router through a gmail account (running Kong PTB 28000M). I was bashing my head against the wall trying to use sendmail, so after piecing together a dozen different tutorials, I came up with sendgmail.sh! Read the script for instructions on usage.
Make sure you read the part about authorizing "less secure apps" in Gmail (see the script description).
Hopefully people find it useful!
Change the .txt extension to .sh (the forum wouldn't let me upload it otherwise).
I was also trying to solve this myself and head bashing a lot while doing so. Finally I gave up and used another email account to send emails. Later I found out about the security, but never changed it.
I will use your script as reference and reason to finally do it. Thanks. _________________ Netgear R7000 running BS DD-WRT build 26446 (home)
No problem! I call sendgmail.sh from another script I wrote that monitors my logs for unusual activity (as defined by me) and emails me the relevant log activity when found.
I also put it in my startup script so the router emails me whenever it has rebooted (hey, why not!). This all started when I got "possible DNS-rebind attack detected" showing up in my logs.
Make sure all of the echos are on a new line without whitespace after the backslash.
Not sure if the forums edited the file or OP didn't have new lines there. Picture shows what I'm talking about.
Also! If you have 2-factor auth you are able to use this by entering a "App Password" instead of your real login password, these can be generated here.
It also depends on your text editor. I use Notepad++, but the Windows version of notepad doesn't put in the newlines and it's a jumbled mess with that editor.
Make sure you have turned on access for less secure apps in Gmail (instructions in original script). Also make sure your script is executable : chmod +x sendgmail.sh (make sure you rename it first).
I recently discovered that the "curl" command can talk to Gmail the exact same way that my script does... with the advantage that everything is contained in one command!
I modified my script to use curl instead (less secure access still needs to be turned on). I've also hard-coded the email address and password in the script. I've also added a "default" recipient, since I often email myself and didn't want to put my email address every time. Leaving the recipient blank makes the script use the default email address.
To have an email sent to you on reboot, put this (with the right path) in your startup (under commands in admin tab of GUI):
/opt/sendgmail.sh "Router rebooted" "Router has finished rebooting on $(date)."
This syntax will only work with the second version of the script.
In the second script, try adding -vvv at the end of the curl command. Run the script from a SSH session and post the results here. That switch should make the curl command very verbose and should help troubleshoot.
root@DD-WRT:~# /jffs/web/sendgmail.sh "Router rebooted" "Router (Psymon) has finished rebooting on $(date)." -vvv
/jffs/web/sendgmail.sh: line 46: curl: not found
I meant to edit the script yourself and add the -vvv at the end of the curl command in the script.
Although the error message was interesting. I take it to mean you may not have the curl command nor the openssl command (for the first script) in your installation of dd-wrt.