Community discussions

MikroTik App
 
undecided
Member Candidate
Member Candidate
Topic Author
Posts: 107
Joined: Mon May 16, 2011 11:07 am

Hotspot - send email upon user creation/login

Mon Jul 29, 2013 11:08 am

Hi guys,

I'm trying to generate an email for when a particular set of users login to the hotspot. I have created a standard hotspot and have created a customer account, all users created by this customer have "rtl" in the username. I've also configured email on my router(Rb750) and this is tested and sending emails without issue.

I've found a script to log when a user logs into the hotspot, and a script that matches ther "rtl" portion of the username.
I receive a "user rtl123 logged in, Trigged on login script" followed by "user matches name filter".....however I receive no email.

My script

/log info "$user just logged in, triggered On Login script";
# CONFIG --------------------------------------------\
# Email address to send to
:local emailaddress "myemail@googlemail.com";
# How long user stays in Address List
:local timeout 30d;
# Name filter, only process usernames that start with this string, CASE sensitive
# If you want to allow all users, remove everything between the quotes :local nameFilter "";
:local nameFilter "rtl";
# END CONFIG ----------------------------------------/
# if username starts with nameFilter, proceed
if ([:find "$user" "$nameFilter"] = 0) do={/log info "[HOTSPOT] - $user - logged in, matches name filter";
# Set date and time variables
:local date [/system clock get date];
:local time [/system clock get time];
# get user IP
:local ip [/ip hotspot active get [find user="$user"] address];
# delcare a few variables
:local emailsubject;
:local emailbody;
# if user does NOT exist in Address List
:if ([:len [/ip firewall address-list find list~"^$user - HSLOGIN"]] = 0) do={/log info "[HOTSPOT] - $user - not found in Address List";
# add firewall rules that will add dynamic address list entry
/ip firewall filter add action=add-src-to-address-list address-list="$user - HSLOGIN,$date,$time" address-list-timeout=$timeout chain=pre-hs-input disabled=no src-address=$ip comment="$user - HSLOGIN";
/ip firewall filter add action=add-src-to-address-list address-list="$user - HSLOGIN,$date,$time" address-list-timeout=$timeout chain=forward disabled=no src-address=$ip comment="$user - HSLOGIN";
:local counter 0;
# number of times to attempt to add user to Address List before giving up
:local limit 60;
# delay between attempts
:local delaytime 5s;
# loop a number of times to check if user is added to Address List
:while (counter < $limit) do={
:set counter ($counter + 1);
/log info "[HOTSPOT] - $user - checking if user is in Address List - attempt $counter of $limit";
# wait between Address List checks
:delay $delaytime;
# if Address List entry is found, proceed
:if ([:len [/ip firewall address-list find list~"^$user - HSLOGIN"]] = 1) do={/log info "[HOTSPOT] - $user - user has been added to Address List, sending email";
# set email subject and body variables
:set emailsubject "New Hotspot Login ($user)";
:set emailbody "User: $user\r\n$time, $date\r\nIP: $ip\r\nExpires in: $timeout";
# increment counter
:set counter ($limit+10);} else={
# if we have reached the limit of times to check, send email
:if ($counter = $limit) do={/log info "[HOTSPOT] - $user - failed to add user to Address List, sending email";
# set email subject and body variables
:set emailsubject "New Hotspot Login ERROR ($user)";
:set emailbody "ERROR: failed to add to Address List, need to investigate.\r\n\r\nUser: $user\r\n$time, $date\r\nIP: $ip\r\n";
}
}
}
# remove firewall rules afterwards
/ip firewall filter remove [find comment="$user - HSLOGIN"];
# send email
/tool e-mail send to="$emailaddress" subject="$emailsubject" body="$emailbody";
# if user DOES exist in address list
} else={/log info "[HOTSPOT] - $user - already in Address List";}
# if user does not match name filter
} else={/log info "[HOTSPOT] - $user - logged in, does not match name filter";}


I'm not sure where it's going wrong.....does anyone else do anything similar or have an idea where my script is failing?

Thanks
Undecided
 
User avatar
samir494
Member Candidate
Member Candidate
Posts: 236
Joined: Thu Aug 06, 2009 11:27 am
Location: India

Re: Hotspot - send email upon user creation/login

Mon Jul 29, 2013 11:58 am

here you can check, this is i used for free user

1. set this on login script

:local freeuser
:set freeuser "$user"
:local freeuserno [/ip hotspot active find where user=$freeuser];
:local freemac [/ip hotspot active get $freeuserno mac-address];
:local freeip [/ip hotspot active get $freeuserno address];
:local emailaddress "YOUREMAIL@YOURDOMAIN:COM"
:local date [/system clock get date]
:local time [/system clock get time]
:local emailsubject
:local emailbody
:set emailsubject "New Hotspot Login ($freeuser)"
:set emailbody "Trial: $freemac\r\n$time, $date\r\nIP: $freeip\r\n";
/tool e-mail send to="$emailaddress" subject="$emailsubject" body="$emailbody"



2. set this on logout script

local freeuser
:set freeuser "$user"
:local freeuserno [/ip hotspot user find where name=$freeuser];
:local freemac [/ip hotspot user get $freeuserno mac-address];
:local utime [/ip hotspot user get $freeuserno uptime];
:local byteup [/ip hotspot user get $freeuserno bytes-in];
:local bytedown [/ip hotspot user get $freeuserno bytes-out];
:local pckup [/ip hotspot user get $freeuserno packets-in];
:local pckdown [/ip hotspot user get $freeuserno packets-out];
:local dhleaseno [ip dhcp-server lease find where mac-address=$freemac]
:local freeip [ip dhcp-server lease get $dhleaseno active-address]
:local freehost [ip dhcp-server lease get $dhleaseno host-name]
:local emailaddress "YOUREMAIL@YOURDOMAIN.COM"
:local date [/system clock get date]
:local time [/system clock get time]
:local emailsubject
:local emailbody
:set emailsubject "New Hotspot Login ($freeuser)"
:set emailbody "Trial: $freemac\r\n$time, $date\r\nHostname : $freehost\r\nIP : $freeip\r\nUptime: $utime\r\nBytes Download : $bytedown\r\nBytes Upload : $byteup\r\nPackets Download : $pckdown\r\nPackets Upload : $pckup\r\n";
/tool e-mail send to="$emailaddress" subject="$emailsubject" body="$emailbody"
 
undecided
Member Candidate
Member Candidate
Topic Author
Posts: 107
Joined: Mon May 16, 2011 11:07 am

Re: Hotspot - send email upon user creation/login

Tue Jul 30, 2013 11:54 am

Thank you for your script, I will give it a try!
 
undecided
Member Candidate
Member Candidate
Topic Author
Posts: 107
Joined: Mon May 16, 2011 11:07 am

Re: Hotspot - send email upon user creation/login

Tue Jul 30, 2013 12:13 pm

Samir,

In my case since it is not for a free user, and I need to match the name would the below work?

/log info "$user just logged in, triggered On Login script";
:local emailaddress "fusionpavlou@googlemail.com";
:local timeout 30d;
:local nameFilter "rtl";
if ([:find "$user" "$nameFilter"] = 0) do={/log info "[HOTSPOT] - $user - logged in, matches name filter";
:local date [/system clock get date];
:local time [/system clock get time];
:local ip [/ip hotspot active get [find user="$user"] address];
:local emailsubject;
:local emailbody;
:local counter 0;
:local limit 60;
:local delaytime 5s;
:while (counter < $limit) do={
:set counter ($counter + 1);
:delay $delaytime;
:set emailsubject "New Hotspot Login ($user)";
:set emailbody "User: $user\r\n$time, $date\r\nIP: $ip\r\nExpires in: $timeout";
:set counter ($limit+10);} else={
/tool e-mail send to="$emailaddress" subject="$emailsubject" body="$emailbody";

Who is online

Users browsing this forum: Bing [Bot], pepe262, Renfrew and 94 guests