# Notification Of Device Connection To The Network - MikroTik Script RouterOS

    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.

    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.

    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.

    :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 $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
    }
    Credit: https://mhelp.pro/mikrotik-scripts-notification-of-device-connection-to-the-network/