Community discussions

MikroTik App
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

get Alert by email on new Device

Tue Nov 05, 2019 10:20 am

Hi

I need a script that send me an email whenever a new device is connected to my network ( Got a dynamic IP in dhcp leases ) ( LAN or WiFi)
note that all known devices has static IP given
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 978
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: get Alert by email on new Device

Tue Nov 05, 2019 10:29 am

You could trigger /tool e-mail from your dhcp server's lease-script
-Chris
Christopher Diedrich
MTCNA, MTCUME, MTCWE
Basel, Switzerland
Bremen, Germany

There are 10 types of people: Those who understand binary and those who don't.
There are two types of people: Those who can extrapolate from incomplete data
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Tue Nov 05, 2019 10:33 am

I've added the flowing script in dhcp server alerts but not working
:tool e-mail send to=email@example.com subject="Violation alert!!! New DHCP client in the network" body="MAC: $leaseActMAC,
please guide me
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 978
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: get Alert by email on new Device

Tue Nov 05, 2019 1:41 pm

Did you configure /tool e-mail before?

Like:
/tool e-mail
set address=mail.provider.com from=dhcpalerts@provider.com password=youremailpassword port=465 start-tls=tls-only user=dhcpalerts@provider.com
-Chris
Christopher Diedrich
MTCNA, MTCUME, MTCWE
Basel, Switzerland
Bremen, Germany

There are 10 types of people: Those who understand binary and those who don't.
There are two types of people: Those who can extrapolate from incomplete data
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Tue Nov 05, 2019 2:17 pm

yes
and I already have a scheduled script that emails me configuration backup every certain period of time
 
User avatar
cdiedrich
Forum Veteran
Forum Veteran
Posts: 978
Joined: Thu Feb 13, 2014 2:03 pm
Location: Basel, Switzerland // Bremen, Germany
Contact:

Re: get Alert by email on new Device

Tue Nov 05, 2019 2:31 pm

Hmmm...
Turn on logging for topics e-mail,debug and trigger that command manually. Then you can see the whole smtp session in your log.
Maybe you find a hint on what's wrong there.
-Chris
Christopher Diedrich
MTCNA, MTCUME, MTCWE
Basel, Switzerland
Bremen, Germany

There are 10 types of people: Those who understand binary and those who don't.
There are two types of people: Those who can extrapolate from incomplete data
 
pe1chl
Forum Guru
Forum Guru
Posts: 6908
Joined: Mon Jun 08, 2015 12:09 pm

Re: get Alert by email on new Device

Tue Nov 05, 2019 2:32 pm

Your script has a syntax error, missing closing quote.
Test your script commands e.g. in a CLI window to see if they work.
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Tue Nov 05, 2019 2:54 pm

I've got this script from a search but also no email sent when a new device connected to network

I put it in dhcp server alert with default settings of new alert
:local recipient "someemail@soemserver.com"
/ip dhcp-server lease
:if ($leaseBound = 1) do={
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
	} on-error={:log error "Failed to send alert email to $recipient"}}
there is no log entry

I think the event didn't fire and the code never executed
 
pe1chl
Forum Guru
Forum Guru
Posts: 6908
Joined: Mon Jun 08, 2015 12:09 pm

Re: get Alert by email on new Device

Tue Nov 05, 2019 7:00 pm

Debugging MikroTik scripting (especially scripts executed on event) is very frustrating!
When there is the slightest syntax error in your script it will just do nothing and no error is logged.
Also, some syntax that you would think is valid in reality is not, e.g. for some complicated expressions.
That is why I recommend testing the script from CLI first.
Also be sure you fully understand the script permissions thing.
 
WeWiNet
Member
Member
Posts: 414
Joined: Thu Sep 27, 2018 4:11 pm

Re: get Alert by email on new Device

Thu Nov 07, 2019 1:25 am

Would be interested in such a script as well! For me just logging with a custom text would be enough...

Keep us updated when you find the solution...
WeWiNet

**
MTCNA
hapac2, map, hap-lite, ltap-mini, RB4011 (good!), Audience (better), Audience + Wap R ac LTE Hybrid DSL + LTE FWA (best :-) ) !!!
 
RackKing
Member
Member
Posts: 381
Joined: Wed Oct 09, 2013 1:59 pm

Re: get Alert by email on new Device  [SOLVED]

Thu Dec 05, 2019 3:58 am

I've got this script from a search but also no email sent when a new device connected to network

I put it in dhcp server alert with default settings of new alert
:local recipient "someemail@soemserver.com"
/ip dhcp-server lease
:if ($leaseBound = 1) do={
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
	} on-error={:log error "Failed to send alert email to $recipient"}}
there is no log entry

I think the event didn't fire and the code never executed
This should go in the script box of the dhcp-server you want it to run on - not in the alerts tab.
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Thu Dec 05, 2019 10:16 am

I've got this script from a search but also no email sent when a new device connected to network

I put it in dhcp server alert with default settings of new alert
:local recipient "someemail@soemserver.com"
/ip dhcp-server lease
:if ($leaseBound = 1) do={
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
	} on-error={:log error "Failed to send alert email to $recipient"}}
there is no log entry

I think the event didn't fire and the code never executed
This should go in the script box of the dhcp-server you want it to run on - not in the alerts tab.
thank you I think it is my mistake I'll try that and reply
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Thu Dec 05, 2019 10:55 am

now It worked fine

thanks for all help
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Thu Dec 05, 2019 11:36 am

a simple alteration to the script

the previous script sends an email when ever a new devices connected to router static or dynamic

in my situation I need to know only dynamic ones because the static ones in known to me

this modification sends only dynamic ip addresses
:local recipient "someemail@server.com"
/ip dhcp-server lease
:if (($leaseBound=1) && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do {
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
	} on-error={:log error "Failed to send alert email to $recipient"}
}
 
abuumarselo
newbie
Topic Author
Posts: 34
Joined: Wed Aug 21, 2019 3:11 pm

Re: get Alert by email on new Device

Wed Dec 25, 2019 11:47 am

Nice Addition to the script

add filter rule to block mac address from using internet

:local recipient "someemail@server.com"
/ip dhcp-server lease
:if (($leaseBound=1) && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do {
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
		
		/ip firewall filter
               add chain=forward src-mac-address=$leaseActMAC action=drop
	} on-error={:log error "Failed to send alert email to $recipient"}
	
}
 
gotsprings
Forum Veteran
Forum Veteran
Posts: 952
Joined: Mon May 14, 2012 9:30 pm

Re: get Alert by email on new Device

Thu Dec 26, 2019 3:26 pm

www.domotz.com

Email, alert on your phone, and log.
"It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so."
Mark Twain
 
RackKing
Member
Member
Posts: 381
Joined: Wed Oct 09, 2013 1:59 pm

Re: get Alert by email on new Device

Thu Dec 26, 2019 5:46 pm

Very nice - thank you.

Nice Addition to the script

add filter rule to block mac address from using internet

:local recipient "someemail@server.com"
/ip dhcp-server lease
:if (($leaseBound=1) && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do {
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
		
		/ip firewall filter
               add chain=forward src-mac-address=$leaseActMAC action=drop
	} on-error={:log error "Failed to send alert email to $recipient"}
	
}
 
User avatar
mozerd
Member
Member
Posts: 452
Joined: Thu Oct 05, 2017 3:39 pm
Location: Canada
Contact:

Re: get Alert by email on new Device

Mon Dec 30, 2019 4:40 pm

a simple alteration to the script

the previous script sends an email when ever a new devices connected to router static or dynamic

in my situation I need to know only dynamic ones because the static ones in known to me

this modification sends only dynamic ip addresses
:local recipient "someemail@server.com"
/ip dhcp-server lease
:if (($leaseBound=1) && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do {
	:do {
		:tool e-mail send to=$recipient subject="DHCP Address Alert [MAC: $leaseActMAC]" body="The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
	} on-error={:log error "Failed to send alert email to $recipient"}
}
Very nice modification .... just to let you know that on my system this generates a syntax error in the following:
&& ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do {

To correct the following change works:
&& ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do= {
 
lsettembri
just joined
Posts: 24
Joined: Tue Jan 14, 2014 10:04 pm

Re: get Alert by email on new Device

Mon Mar 02, 2020 8:06 pm

Anyone knows how to add the hostname to the email? Thanks!
 
hngjared
Frequent Visitor
Frequent Visitor
Posts: 75
Joined: Thu Dec 01, 2011 8:36 pm
Location: NYC USA

Re: get Alert by email on new Device

Thu Jun 11, 2020 11:03 pm

To add the hostname use $"lease-hostname"

Who is online

Users browsing this forum: quartzq, sawesa and 29 guests