Script to check new hardware connected (IP - MAC)

Hello,

I am new on this forum and with MK too. I would to know how could I do a script to send me an email when a new hardware request IP to my MK.
The email should have IP and MAC from request to check our schedule whom is asking for new IP.

I have already email config configured but I don’t know how I could do script. I tried in “Scripts” without luck.

After, I would like to add that script on interval (15:00 pm until 7:00 am), is it possible?

Thanks you. Greetings.

http://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server

lease-script
Script that will be executed after lease is assigned or de-assigned. Internal “global” variables that can be used in the script:

  • leaseBound - set to “1” if bound, otherwise set to “0”
    leaseServerName - dhcp server name
    leaseActMAC - active mac address
    leaseActIP - active IP address

I.e. you need jut send email with leaseActIP / leaseActMAC

Example of logging here:
http://wiki.mikrotik.com/wiki/Manual:Scripting

/system script
add name=myLeaseScript policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    source=":log info \$leaseActIP\r\
    \n:log info \$leaseActMAC\r\
    \n:log info \$leaseServerName\r\
    \n:log info \$leaseBound"

/ip dhcp-server set  myServer lease-script=myLeaseScript

Hi, thanks you for reply BlackVS.
I added that code on Scripting section but I dont know if I need do something more to receive emails when any IP is assigned.

You said me that I need send email with leaseActIP / leaseActMAC but I dont know where I could do it.

Could you guide me a little? I’m new on MK.
This is mi code:

source=":log info \$leaseActIP\r\
\n:log info \$leaseActMAC\r\
\n:log info \$leaseServerName\r\
\n:log info \$leaseBound"

/ip dhcp-server set  myServer lease-script=myLeaseScript

If ($leaseBound=1) do={
 /tool e-mail send to=myemail@mydomain.com  subject="DHCPLIST" body="$leaseActIP - $leaseActMAC"
}

Here a picture:

Thanks again.

I.e:

  1. You forgot “:” before “if”.
    Variant 2 (I was wrong - example from Wiki works %):

Thanks BlackVS, I tried to do it but I can’t receive emails when any IP is given.

Here my screenshots (I can send me test emails without problem). Any help is appreciated :slight_smile:

Section Scripts:

Section DHCP Server → default zone.

Thanks again.

Do you see in the log messages from script? I speacially put “/log info” commands for easy debug.
And you have error - must be “e-mail” not “email” %)

Hi BlackVS, as always thanks you for your patience.

I changed to e-mail but not luck, I don’t receive any emails. I checked logs but there aren’t any entry respect that, only I see that:

07:11:13 wireless,info 08:D4:XX:XX:XX:XX@wlan1: connected
07:11:17 wireless,info 08:D4:XX:XX:XX:XX@wlan1: disconnected, received deauth: sending station leaving (3)

I see a new MAC connected to wlan. I understand that I should have an email from that connection. How could I debug it more? Or what more could I check?

Thanks again.

It seems to be you have valid DHCP lease during connection and no new DHCP lease is given. By default lease is valid 24hrs.
Clear leases at DHCP Server-> leases and try reconnect.
Or If it is windows PC run 2 commands:

ipconfig /release
ipconfig /renew

By default you should see something like this in logs (it is default, not script output):

If you don’t see this it means that you DHCP server didn’t released or leased IP addresses and script not run.

Hi, I did it but I can’t receive any emails, here log:

12:20:27 dhcp,info default deassigned 192.168.XX.XX from 78:F8:XX:XX:XX:XX
12:20:27 dhcp,info default assigned 192.168.XX.XX to 78:F8:XX:XX:XX:XX
12:20:38 dhcp,info default deassigned 192.168.XX.XX from 2C:76:XX:XX:XX:XX
12:20:38 dhcp,info default assigned 192.168.XX.XX to 2C:76:XX:XX:XX:XX

To check all everyrthing, in System → Script I have:

/log info "myLeaseScript Started. $leaseBound-$leaseServerName-$leaseActMAC-$leaseActIP"
:if ($leaseBound=1)do={
 /log info "DHCPLIST: $leaseActIP - $leaseActMAC"
 /tool e-mail send to=email@myemail.com subject="DHCPLIST" body="$leaseActIP - $leaseActMAC"
}
/log info "myLeaseScript Ended"

And DHCP Server → default → In section “Lease script” → myLeaseScript.

In Tool → Email → I have details correctly because I can send/receive any test.

I attach a picture DCHP Server config

Maybe are there any option that I am not seeing?

Thanks you for your help.