Community discussions

MikroTik App
 
hunte88
just joined
Topic Author
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Script that send mail when new device connected to my network

Wed Dec 28, 2022 1:07 am

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.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2865
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 8:45 am

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]")
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 2:47 pm

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...
 
hunte88
just joined
Topic Author
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 3:59 pm

Something like this script set for DHCP lease should work.

You can extend the idea as you need:
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

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 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
 
B20016
just joined
Posts: 11
Joined: Sat Nov 20, 2021 8:32 am
Location: Kenya

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 4:22 pm

: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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 4:56 pm

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

What are you writing, don't you check first???
Last edited by rextended on Thu Dec 29, 2022 12:54 am, edited 1 time in total.
 
hunte88
just joined
Topic Author
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 6:51 pm

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
 
B20016
just joined
Posts: 11
Joined: Sat Nov 20, 2021 8:32 am
Location: Kenya

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 7:01 pm

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 <TOKEN> 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.
 
hunte88
just joined
Topic Author
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Re: Script that send mail when new device connected to my network

Wed Dec 28, 2022 11:53 pm

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
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script that send mail when new device connected to my network

Thu Dec 29, 2022 12:50 am

Can you help me please
Don't get fooled by that i–t who doesn't even know what he writes...

⚠️⚠️⚠️ Remove the fake rule on the firewall forward input chain...
 
hunte88
just joined
Topic Author
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Re: Script that send mail when new device connected to my network

Thu Dec 29, 2022 3:31 am

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 ?
 
hunte88
just joined
Topic Author
Posts: 14
Joined: Mon Dec 26, 2022 8:42 pm

Re: Script that send mail when new device connected to my network

Fri Dec 30, 2022 2:24 am

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"}}
 
massinia
Member Candidate
Member Candidate
Posts: 159
Joined: Thu Jun 09, 2022 7:20 pm

Re: Script that send mail when new device connected to my network

Fri Feb 17, 2023 11:05 am

Something like this script set for DHCP lease should work.
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 :lol:

Who is online

Users browsing this forum: F3BOli, muona, ywlhlp and 46 guests