Script that send mail when new device connected to my network

Hi all, I have a routerboard in my network.
I’m trying to create a script that sends me a mail when a new device connects to my LAN/dhcp_server (wifi or ethernet). I’ve searched the forum here but can’t find anything that works.
Can you give me some help please.

Something like this script set for DHCP lease should work.

You can extend the idea as you need:

:local zone "mylocalzonemail"
:local dnsrecord [/ip dns static find where address=$leaseActIP ]
:if ( $leaseBound = 0 ) do={
  :if ( [ :len $dnsrecord ] > 0 ) do={
    :log warning ("DNS release record for $[/ip dns static get $dnsrecord name] ( $leaseActIP )")
    /ip dns static remove $dnsrecord
  }
} else={
  :local hostname [/ip dhcp-server lease get [/ip dhcp-server lease find where address=$leaseActIP server=$leaseServerName ] host-name]	
  :local fqdn
  :local dhcplease
  :foreach dhcplease in=[/ip dhcp-server lease find where ( server="$leaseServerName" && address=$leaseActIP ) ] do={
    :set hostname [/ip dhcp-server lease get $dhcplease host-name ]
    :set fqdn ( $hostname . "." . $zone )
    :local ttl [/ip dhcp-server lease get $dhcplease expires-after ]
    :if ( [ :len $dnsrecord ] > 0 ) do={
      /ip dns static remove $dnsrecord
    }
    :log warning ("Renew DNS record for $fqdn ( $leaseActIP )")
    /ip dns static add name=$fqdn address=$leaseActIP ttl=$ttl comment=$leaseActMAC
  }
}
#
#
/tool e-mail send to=anynameyouwant@internetdomain.pl subject=("DHCP script ($[/system resource get architecture-name]/$[/system resource get version]) @ $[/system clock get date] $[/system clock get time]")

The problem is what you mean for a “new device”.
Without store the info of already knowed devices,
You obtain everytime the device briefly reconnect or dhcp lease expire, emails…

Hi, thanks for the info.
Where should I put this script? in the dhcp_server alerts, in script section of dhcp, or in script normal and after configured schedule ?

Thanks a lot


Hi, thanks for the question.
In my case i want that the script send an email with ip and mac address every time the new device connect with my network, for sample every time a dynamic ip is released.
Thanks

:global emailTo "your@email.com"
:global emailFrom "router@yourdomain.com"
:global smtpServer "smtp.yourprovider.com"

:local currentTime [/system clock get time]
:local newDeviceMac "00:00:00:00:00:00"
:local newDeviceName "Unknown"

:log info "Checking for new devices on the network"

:foreach i in=[/ip arp find] do={
  :local deviceMac [/ip arp get $i mac-address]
  :local deviceLastSeen [/ip arp get $i last-seen]
  :if (deviceLastSeen > $currentTime - 1m) do={
    :if ([/ip arp get $i address] != $newDeviceMac) do={
      :set newDeviceMac $deviceMac
      :set newDeviceName [/ip arp get $i address]
      /tool e-mail send to=$emailTo subject="New device connected" body="A new device with MAC address $newDeviceMac and IP address $newDeviceName has connected to the network at $currentTime." from=$emailFrom server=$smtpServer
      :log info "Sent email notification for new device with MAC $newDeviceMac and IP $newDeviceName"
    }
  }
}

To use this script, you will need to replace your@email.com, router@yourdomain.com, and smtp.yourprovider.com with the appropriate values for your setup. You can then schedule the script to run at regular intervals using the /system scheduler command. For example:

/system scheduler
add interval=5m name=checkNewDevices on-event="/path/to/script.rsc"

This will run the script every 5 minutes to check for new devices on the network, and send an email notification if any are found.

:if (deviceLastSeen > $currentTime - 1m) do={

missing $ before deviceLastSeen

What are you writing, don’t you check first???

Thanks to both.
I have a problem, for to try to add a new device in my network i disconnect my smartphone from the network and i delete his ip dynamic realeas in dhcp server. When i connect the same device (that i have deleted before) to my network, i see into the log that the log “Checking new device on the network” but the mail not come send.
The problem can be that if i deleted the ip from dhcp server, he re-take same ip after reconnect ?
My problem now is how try testing the script. Thanks

try using telegram

Here is an example of a RouterOS script that sends a Telegram message when a new device connects to a MikroTik device’s network:

# Create a new script to send the Telegram message
/system script add name=send-telegram-message source="# Send a Telegram message
:local message \"New device connected to the network: $address\"
/tool fetch url=\"https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=\$message\"

Replace with your Telegram bot’s token and <CHAT_ID> with the chat ID of the recipient.

Next, create a firewall rule to trigger the script when a new device connects to the network:

# Create a new firewall rule to trigger the script
/ip firewall filter add chain=input connection-state=new action=jump jump-target=send-telegram-message

This script creates a new script called “send-telegram-message” that sends a Telegram message to a specified chat ID. It then creates a firewall rule that triggers the script when a new connection is detected on the “input” chain (which handles incoming traffic).

When a new device connects to the network, the firewall rule will be triggered and the script will execute, sending a Telegram message to the specified chat ID with the new device’s IP address.

Overall, this script demonstrates how to use RouterOS to send a Telegram message when a new device connects to a MikroTik device’s network. You can customize the script and the firewall rule to meet your specific needs and requirements.

Sorry guys but not work for me, no script.

The only script that work for me are these for netwatch:

:log info "send message to your phone"
/tool fetch url="https://api.telegram.org/botmytokenA/sendMessage?chat_id=-xxxxxxxxx&text=mytextmessage"

and for email:

:log error "192_168_0_12_is_offline"
/tool e-mail send to="xxxxx@yahoo.com" subject="192_168_0_12_is_offline $[/system clock get time]"

All others script not work for me.

Can you help me please

Don’t get fooled by that i–t who doesn’t even know what he writes…

:warning::warning::warning: Remove the fake rule on the firewall forward input chain…

ok i removed the rule firewall.

The scripts not working for me. How i can try them if i not have a new device for my network ?
Can i disconnect a device knowed in my network and delete his ip from dhcp server lease ?

Sorry guys i have need of your help please.

I understand that the parameters that i have need are

$leaseBound
$leaseServerName
$leaseActMAC
$leaseActIP

But i don’t have understand how use them and where.

Can you tell me please, i’m new with mikrotik.

EDIT:

Thanks a lot to all. I did it.

I report the script that worked in dhcp-server → dhcp–>script.

:local recipient "xxxxx@yahoo.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"}}

Thanks BartoszP, it works perfectly and it’s the best solution because leases remain stored and it doesn’t send emails every time I restart the router :laughing:

Is there a way to add the active host name into all this?

:local recipient "XXXX@yahool.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"}}