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
:tool e-mail send to=email@example.com subject="Violation alert!!! New DHCP client in the network" body="MAC: $leaseActMAC,
/tool e-mail
set address=mail.provider.com from=dhcpalerts@provider.com password=youremailpassword port=465 start-tls=tls-only user=dhcpalerts@provider.com
: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"}}
This should go in the script box of the dhcp-server you want it to run on - not in the alerts tab.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
there is no log entryCode: Select all: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"}}
I think the event didn't fire and the code never executed
thank you I think it is my mistake I'll try that and replyThis should go in the script box of the dhcp-server you want it to run on - not in the alerts tab.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
there is no log entryCode: Select all: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"}}
I think the event didn't fire and the code never executed
: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"}
}
: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"}
}
Nice Addition to the script
add filter rule to block mac address from using internet
Code: Select all: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"} }
Very nice modification .... just to let you know that on my system this generates a syntax error in the following: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
Code: Select all: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"} }
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
: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"}
}