MikroTik Scripts: Notification of device connection to the network

We set up a notification about the connection of a new device to a local network or WiFi using the DHCP server settings (allocation of an IP address or lease expiration). We send a message to Telegram or email.

My (30M) wifi router says a man was home with my wife (29F) while I was at work

 LickingWoundSalt (Reddit)

But this is not the only application of this possibility. 🙂

If you set the IP address lease time (Lease Time) equal to 10 hours, you can find out what time the employees’ computers were connected to the network or who does not turn off the computer when leaving the workplace.

If you use a simple WiFi password, you may find that the neighbor’s child is also using your WiFi. 🙂

The script will help you find out about the existing problems with the stability of the connection or about network problems.

You can also configure other actions when activating this script.

Article in other languages:
?? – MikroTik Scripts: Notificación de la conexión del dispositivo a la red
?? – MikroTik Скрипты: Уведомление о подключении устройства к сети
?? – Scripts MikroTik: notification de la connexion de l’appareil au réseau
?? – MikroTik-Scripts: Benachrichtigung über die Geräteverbindung zum Netzwerk
?? – MikroTik-scripts: Melding van apparaat verbinding met het netwerk

Script: Sending a notification when a device is connected to the network

Mikrotik script is triggered when an IP address is allocated to a new device or a device whose IP address has expired. Sends a Telegram message or email.

The message contains:

  1. device name (computer name or assigned by the manufacturer);
  2. comment (if the device is assigned a static address with the specified comment);
  3. DHCP interface name;
  4. IP and MAC address of the connected device.

? I recommend using only English characters in the comments to the devices!

The script uses function calls:
MikroTik Scripts: Sending a message to Telegram (function);
MikroTik Scripts: Sending Notifications and Files to Email (function).

Example of device connection email from DHCP server
Example of device connection email from DHCP server

Change IP address lease time

Set lease time for the IP address = 10 hours.

[IP] -> [DHCP Server] -> [Your DHCP server] -> [General: Lease Time=10:00:00]

Create notification script

[IP] -> [DHCP Server] -> [Script]

Script code

:if ($leaseBound =1) do={
    # Variables
    :local Time [/system clock get time];
    :local Date [/system clock get date];
    :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
    :local DeviceName [/system identity get name];

    # START Send Telegram Module
    :local MessageText "\F0\9F\9F\A2 <b>$DeviceName: New DHCP client</b> %0D%0A <b>Name:</b> $"lease-hostname" %0D%0A <b>Comment:</b> [$Comment] %0D%0A <b>Interface:</b> $leaseServerName %0D%0A <b>IP:</b> $leaseActIP %0D%0A <b>MAC:</b> $leaseActMAC";
    :local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]; 
    $SendTelegramMessage MessageText=$MessageText;
    #END Send Telegram Module

    # START Send Email Module
    :local SendTo "notify@mhelp.pro";
    :local Subject "\F0\9F\9F\A2 INFO: $DeviceName [$Date $Time] New DHCP client";
    :local MessageText "Name: $"lease-hostname", Comment: $Comment, Interface: $leaseServerName IP: $leaseActIP MAC: $leaseActMAC";
    :local FileName "";
    :local SendEmail [:parse [/system script get SendEmailFunction source]];
    $SendEmail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
    # END Send Email Module
}
MikroTik Scripts: Notification of device connection to the network

? This article discussed how to create a MikroTik script to send messages about a device’s network connection. I hope you managed to set up sending notifications to Telegram or email. However, if you are faced with any problems getting information, do not hesitate to write in the comments. I will try to help.

✅ The script is checked: hAP ac lite [RouterBOARD 952Ui-5ac2nD], RouterOS 6.47.8 (stable).

P.S. If you have an interesting or unusual use of the script, write in the comments, I will add to the article. 🙂

7 thoughts on “MikroTik Scripts: Notification of device connection to the network”

  1. Hi, i tried to insert the script but not work at this moment, i have configured the email and my account work but the send email not work and in the log not show nothing of the script.
    Can you help me please ?

    Reply
  2. Change to send notification only from devices that have no comments in DHCP Server (no comment is not fixed IP, but can still have a fixed IP without comment):

    :if ($leaseBound =1) do={
    # Variables
    :local Time [/system clock get time];
    :local Date [/system clock get date];
    :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
    :local DeviceName [/system identity get name];

    :if ($Comment = “”) do={
    # START Send Telegram Module
    :local MessageText “\F0\9F\9F\A2 $DeviceName: New DHCP client %0D%0A Name: $”lease-hostname” %0D%0A Comment: [$Comment] %0D%0A Interface: $leaseServerName %0D%0A IP: $leaseActIP %0D%0A MAC: $leaseActMAC”;
    :local SendTelegramMessage [:parse [/system script get MyTGBotSendMessage source]];
    $SendTelegramMessage MessageText=$MessageText;
    #END Send Telegram Module

    # START Send Email Module
    :local SendTo “notify@mhelp.pro”;
    :local Subject “\F0\9F\9F\A2 INFO: $DeviceName [$Date $Time] New DHCP client”;
    :local MessageText “Name: $”lease-hostname”, Comment: $Comment, Interface: $leaseServerName IP: $leaseActIP MAC: $leaseActMAC”;
    :local FileName “”;
    :local SendEmail [:parse [/system script get SendEmailFunction source]];
    $SendEmail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
    # END Send Email Module
    }
    }

    Reply

Leave a Comment