PortCheck - Send an email if a connection has been made

Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload
Goto page 1, 2  Next
Author Message
cyberde
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 1488
Location: the Netherlands

PostPosted: Thu Jan 25, 2007 21:06    Post subject: PortCheck - Send an email if a connection has been made Reply with quote
Hi, I was looking for a way to get an email if a connection has been made to a specific port (eg my OpenVPN). But I didn't find any, so I decided to create one my own.


The attached zipfile includes the folowing:
- portcheck.sh
- sendmail (used for sending the notification email, I didn't made that)

Howto use this script?
Well it's easy, all you have to do is enter the variables:
Code:
INTERVAL=5
PORT=443
CHECK_ONLY_WAN=1
LOGFILE=/tmp/somelogfile.log
MAIL_BINARY=/mmc/usr/sbin/sendmail
MAIL_SERVER=smtp.isp.com
MAIL_TO=emailto@isp.com
MAIL_FROM=emailfrom@isp.com
MAIL_SUBJECT="Some subject for your email"
MAIL_ON_CONNECT=1
MAIL_ON_DISCONNECT=1


To be able to use it, you should have an SDcard mod or JFFS2 space left to save this script and save the sendmail binary. A samba share should do too.
To start the script automatically on router boot, you could enter ./portcheck.sh & to your startup script or save it as eg. /mmc/etc/config/portcheck.startup.
For more info on this have a look at Startup scripts in the Wiki.


How does it work?
Also easy, it counts the current connections to the specified port from "netstat -n". If the amount changed with the previous value it adds some info to the specified logfile and then emails it (if enabled ofcourse).

The logfile looks like this:
Code:
Thu Jan 25 21:51:47 UTC 2007   Log started
Thu Jan 25 21:51:52 UTC 2007   1 established connection(s)
Thu Jan 25 21:51:52 UTC 2007   Clients: 62.140.X.X
Thu Jan 25 21:52:44 UTC 2007   0 established connection(s)


I hope this little script might come in handy for someone :)

P.S. If you have any additions or improvements, please post them Smile

_________________
Firmware: DD-WRT v24-sp2 (latest available) mega
WRT320N

Donater
Sponsor
madamada
DD-WRT Novice


Joined: 05 Sep 2006
Posts: 47

PostPosted: Tue Jan 30, 2007 19:13    Post subject: Reply with quote
Hi,

Very nice script ! Good Job sir.
Can I monitor multi port with your script ?

Thanks.
cyberde
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 1488
Location: the Netherlands

PostPosted: Tue Jan 30, 2007 19:16    Post subject: Reply with quote
You can, but to do that you need to run this script twice with other settings...
Eg.
vpncheck.sh
port123check.sh

So it's not possible to do that with just one script... Maybe that's something for the next version Smile

_________________
Firmware: DD-WRT v24-sp2 (latest available) mega
WRT320N

Donater
madamada
DD-WRT Novice


Joined: 05 Sep 2006
Posts: 47

PostPosted: Tue Jan 30, 2007 20:35    Post subject: Reply with quote
Hi,

I will put some into mine ;)

Thanks
trey504
DD-WRT User


Joined: 02 Oct 2006
Posts: 220

PostPosted: Thu Feb 01, 2007 1:32    Post subject: Reply with quote
Thanks im gona check this out later. Could you update it for mailserver auth or post the sendmail docs ...I looked and could not find one on what arguments are what
cyberde
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 1488
Location: the Netherlands

PostPosted: Thu Feb 01, 2007 9:07    Post subject: Reply with quote
The options available for sendmail are the folowing:
Code:
/mmc/usr/sbin # ./sendmail -?
usage:  ./sendmail [-f<name>] [-t] [-s<server>] [-p<port>] [-T<timeout>] [-v] [address ...]


So I don't think authentication can be done Sad

_________________
Firmware: DD-WRT v24-sp2 (latest available) mega
WRT320N

Donater
trey504
DD-WRT User


Joined: 02 Oct 2006
Posts: 220

PostPosted: Sat Feb 03, 2007 0:21    Post subject: Reply with quote
netstat does not work with udp so don't waste time trying to figure out why its not working like me
cyberde
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 1488
Location: the Netherlands

PostPosted: Sat Feb 03, 2007 11:08    Post subject: Reply with quote
Yup, that's true... It only works for established TCP connections.
_________________
Firmware: DD-WRT v24-sp2 (latest available) mega
WRT320N

Donater
trey504
DD-WRT User


Joined: 02 Oct 2006
Posts: 220

PostPosted: Sat Feb 03, 2007 19:43    Post subject: Reply with quote
Add this line to get rid of that unknown sender crap
echo "From: $MAIL_FROM" >> /tmp/mailnotification


Add these for some vpn info add those two variables to the beginning also and enable status log in your openvpn config file
Code:

if [ $ENABLE_VPN_STATUS -eq 1 ]; then
   # Displays crt name and local and external ip
   VPN=`cat $VPNSTATUS | grep Peer | tail -n1 | awk '{ print $7 " " $6 ":::" $12}' | cut -d":" -f1,3,4,5`
   echo "Last VPN Connect from: $VPN" >> /tmp/mailnotification
fi
deocder
DD-WRT Novice


Joined: 05 Apr 2007
Posts: 21

PostPosted: Wed May 23, 2007 17:20    Post subject: Reply with quote
Great topic! I'm trying to do something similar. I have a dynamic IP address from my wan and can't change it to static. So I would like to send an email every time the wan IP address changes. Does someone know how to do this?

Thanks
-Laura
cyberde
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 1488
Location: the Netherlands

PostPosted: Thu May 24, 2007 13:43    Post subject: Reply with quote
Sure, you can write the output `nvram get wan_ipaddr` to a file on jffs / mmc or even create your own nvram variable for it.
After that you can compare the both values eg of wan_ipaddr and wan_ipaddr_before and if changed send an email.

_________________
Firmware: DD-WRT v24-sp2 (latest available) mega
WRT320N

Donater
cyberde
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 1488
Location: the Netherlands

PostPosted: Fri Jun 01, 2007 9:57    Post subject: Reply with quote
Hi deocder, I've created the script you wanted.
You can find it here: http://www.dd-wrt.com/phpBB2/viewtopic.php?t=16041

_________________
Firmware: DD-WRT v24-sp2 (latest available) mega
WRT320N

Donater
bruns
DD-WRT Novice


Joined: 07 Jun 2006
Posts: 22
Location: NYC

PostPosted: Thu Jun 21, 2007 20:55    Post subject: Reply with quote
For those who need it, I have a build of msmtp for dd-wrt available here in case you don't have an external mmc card and need something tiny (under 100kb) to fit on the existing empty jffs space.:

http://users.sosdg.org/~bruns/dd-wrt/msmtp-1.4.12cvs-ddwrt.tar.bz2

Unpack this, put the contents of bin/ in /jffs/bin, contents of etc/ in /jffs/etc. Edit /jffs/etc/msmtprc.

ln -s /jffs/bin/msmtp /jffs/bin/sendmail

Adjust your programs as necessary, and they will use msmtp as if its sendmail.

_________________
Brielle Bruns
The Summit Open Source Development Group
http://www.sosdg.org
schnabelm
DD-WRT Novice


Joined: 29 Aug 2008
Posts: 4

PostPosted: Sat Aug 30, 2008 15:51    Post subject: Status mail Reply with quote
Hey,

is it possible to send a mail every 15 minutes to a special email account with a special subject so that I can see if my AP is online without open ports?

And furthermore is it possible to receive emails, so that if there is a email in a pop3 inbox a vpn connection is establihed, because I don't wanna open ports.

Or do you see any other possibility to connect to the router without port forwarding?

Thanks for help

Maximilian
Nucleus111
DD-WRT Novice


Joined: 27 Jul 2010
Posts: 24

PostPosted: Thu Jan 26, 2012 4:12    Post subject: Reply with quote
Really like this script!

Question...
How do I get the log file to clear itself when a user disconnects or after X amount of lines?

----never mind; figured it out.
Goto page 1, 2  Next Display posts from previous:    Page 1 of 2
Post new topic   Reply to topic    DD-WRT Forum Index -> Contributions Upload 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 cannot attach files in this forum
You cannot download files in this forum