Script for sending emails from router using a Gmail account

Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware
Goto page 1, 2  Next
Author Message
raph321
DD-WRT Novice


Joined: 20 Jan 2014
Posts: 29

PostPosted: Wed Dec 23, 2015 20:53    Post subject: Script for sending emails from router using a Gmail account Reply with quote
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).



sendgmail.txt
 Description:

Download
 Filename:  sendgmail.txt
 Filesize:  4.26 KB
 Downloaded:  1489 Time(s)

Sponsor
raqua
DD-WRT User


Joined: 13 Feb 2014
Posts: 58

PostPosted: Sat Dec 26, 2015 13:29    Post subject: Reply with quote
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)
raph321
DD-WRT Novice


Joined: 20 Jan 2014
Posts: 29

PostPosted: Sat Dec 26, 2015 16:58    Post subject: Reply with quote
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.
raqua
DD-WRT User


Joined: 13 Feb 2014
Posts: 58

PostPosted: Sat Dec 26, 2015 20:22    Post subject: Reply with quote
I use it 100% the same way. Smile
_________________
Netgear R7000 running BS DD-WRT build 26446 (home)
marteline
DD-WRT Novice


Joined: 22 Dec 2015
Posts: 29

PostPosted: Sat May 21, 2016 0:44    Post subject: Reply with quote
Hi, I just donwload this script and saved it at /jffs/sendgmail.sh

now I try to run it from GUI (admin/commands) like that:

sh /jffs/sendgmail.sh my_email@gmail.com mypass destinatary@gmail.com test "this is a test"


but nothing happens.... could someone help me?
wutyes
DD-WRT User


Joined: 16 Jan 2016
Posts: 67

PostPosted: Sat May 21, 2016 3:25    Post subject: Reply with quote
I was able to get it working.

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.



newline.png
 Description:
 Filesize:  38.75 KB
 Viewed:  15890 Time(s)

newline.png


raph321
DD-WRT Novice


Joined: 20 Jan 2014
Posts: 29

PostPosted: Sat May 21, 2016 15:15    Post subject: Reply with quote
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.



sendgmail.txt
 Description:

Download
 Filename:  sendgmail.txt
 Filesize:  1.29 KB
 Downloaded:  867 Time(s)

wutyes
DD-WRT User


Joined: 16 Jan 2016
Posts: 67

PostPosted: Sat May 21, 2016 16:03    Post subject: Reply with quote
Quote:
Make sure you have turned on access for less secure apps in Gmail (instructions in original script).


If you have 2-factor auth this is unneeded because you can generate a "App Password".
Proself
DD-WRT User


Joined: 07 Apr 2014
Posts: 82

PostPosted: Sat May 21, 2016 16:25    Post subject: Reply with quote
Hmm edited the text and renamed it. Put it in jffs, made it executable and added the script (changed the dir from opt to jffs) to startup. No go.
raph321
DD-WRT Novice


Joined: 20 Jan 2014
Posts: 29

PostPosted: Sat May 21, 2016 16:55    Post subject: Reply with quote
Try putting:

sleep 180

right before the sendgmail call. It could be that your WAN interface hasn't come up yet, so the script fails since there is no internet access yet.

Thanks for the tip for 2-factor authorization, I'll have to check it out.
Proself
DD-WRT User


Joined: 07 Apr 2014
Posts: 82

PostPosted: Sat May 21, 2016 17:13    Post subject: Reply with quote
Code:
sleep 180
/jffs/web/sendgmail.sh "Router rebooted" "Router (Psymon) has finished rebooting on $(date)."

Still nothing.
raph321
DD-WRT Novice


Joined: 20 Jan 2014
Posts: 29

PostPosted: Sun May 22, 2016 2:26    Post subject: Reply with quote
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.
Proself
DD-WRT User


Joined: 07 Apr 2014
Posts: 82

PostPosted: Sun May 22, 2016 2:46    Post subject: Reply with quote
Hmm... maybe I am missing another step?

Code:
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
raph321
DD-WRT Novice


Joined: 20 Jan 2014
Posts: 29

PostPosted: Sun May 22, 2016 2:51    Post subject: Reply with quote
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.

What build number and router model do you have?
Proself
DD-WRT User


Joined: 07 Apr 2014
Posts: 82

PostPosted: Sun May 22, 2016 3:19    Post subject: Reply with quote
Dlink DIR862-A1

DD-WRT v3.0-r29519 std (04/25/16)

Shouldn't all have curl installed?
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum