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