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: 997
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
 
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: 997
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
 
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: 997
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
 
pe1chl
Forum Guru
Forum Guru
Posts: 10195
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: 10195
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
Long time Member
Long time Member
Posts: 592
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...
 
RackKing
Member
Member
Posts: 380
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 Guru
Forum Guru
Posts: 2102
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.
 
RackKing
Member
Member
Posts: 380
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
Forum Veteran
Forum Veteran
Posts: 872
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
newbie
Posts: 31
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: 77
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"
 
kostmas
just joined
Posts: 1
Joined: Sat Mar 27, 2021 3:16 pm

Re: get Alert by email on new Device

Sat Mar 27, 2021 3:19 pm

Anyone knows how to send email only if a new device connected.
I mean, if the "lease time" is not over yet, then don't send email
 
jirislav
just joined
Posts: 1
Joined: Tue Sep 28, 2021 6:29 pm

Re: get Alert by email on new Device

Thu Feb 03, 2022 1:20 am

Anyone knows how to send email only if a new device connected.
I mean, if the "lease time" is not over yet, then don't send email

I use this modification to "remember" already sent MAC addresses (globally). The downside is that it sends again all addresses after restart.
- Also, it would be nice to implement TTL for each record, but this is good enough for me right now.
- Note that my version also sends the hostname of the connected device.

:global reportedMacs;
:if (:typeof[$reportedMacs] = "nil") do={
  :global reportedMacs [ :toarray "" ]
}

:local recipient "someemail@server.com"

/ip dhcp-server lease
:if (($leaseBound=1) && ([:type [:find $reportedMacs $leaseActMAC]]="nil") && ([/ip dhcp-server lease find where dynamic mac-address=$leaseActMAC]!="")) do={
	:do {
                :local leaseHostname $"lease-hostname"
		:tool e-mail send to=$recipient subject="[MikroTik] DHCP Address Alert [MAC: $leaseActMAC]" body="MAC:  $leaseActMAC\nIP:   $leaseActIP\nHost: $leaseHostname\nDHCP: $leaseServerName\n\n-- \nRouterOS"
		:log info "Sent DHCP alert for MAC $leaseActMAC"
                :set reportedMacs ( $reportedMacs, $leaseActMAC );
	} on-error={
                :log error "Failed to send alert email to $recipient"}
        }
 
hunte88
just joined
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Re: get Alert by email on new Device

Mon Dec 26, 2022 9:27 pm

Hi, i have a problem with this type of script. On my routerboard not work nothing. I'm probably doing something wrong.

Can you help me please ?

I have configured the email tools and with script of netwatch the alerts works fine.

This type of script are configured in alerts tab of dhcp server ?

Thanks a lot
 
pe1chl
Forum Guru
Forum Guru
Posts: 10195
Joined: Mon Jun 08, 2015 12:09 pm

Re: get Alert by email on new Device

Wed Dec 28, 2022 5:29 pm

Probably not. Read the above replies first.
 
User avatar
pothi
newbie
Posts: 46
Joined: Fri Sep 14, 2018 7:48 pm
Location: Srivilliputhur, Tamil Nadu, India
Contact:

Re: get Alert by email on new Device

Sat Jul 29, 2023 8:32 am

@hunte88 Not in the alerts tab. But, in IP => DHCP-server => DHCP. Click on the dhcp-server that assigns the IP addresses. Then look for the script box in the end.

It's also called as lease-script. Ref: https://help.mikrotik.com/docs/display/ ... Properties

Who is online

Users browsing this forum: No registered users and 30 guests