get Alert by email on new Device

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

You could trigger /tool e-mail from your dhcp server’s lease-script
-Chris

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

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

yes
and I already have a scheduled script that emails me configuration backup every certain period of time

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

Your script has a syntax error, missing closing quote.
Test your script commands e.g. in a CLI window to see if they work.

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

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.

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…

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

now It worked fine

thanks for all help

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"}
}

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"}
	
}

www.domotz.com

Email, alert on your phone, and log.

Very nice - thank you.


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**=** {

Anyone knows how to add the hostname to the email? Thanks!

To add the hostname use $“lease-hostname”