Community discussions

MikroTik App
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Script DHCP Lease not working

Fri Feb 25, 2022 9:21 pm

Hello,

I have this script on my DHCP server that informs me when someone connects to my LAN.
/ip dhcp-server lease
:if ($leaseBound = 1) do={
	:do {
        
        :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
        :local interFace [/caps-man/registration-table get [find where mac-address=$leaseActMAC] interface]
       
        # START Send Telegram Module
        :local MessageText "\E2\84\B9 <b>Mikrotik: Info DHCP</b> %0D%0A Name: $"lease-hostname" %0D%0A IP: $leaseActIP %0D%0A MAC: $leaseActMAC %0D%0A Interface: $interFace %0D%0A Comment: $Comment";
        :local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]; 
        $SendTelegramMessage MessageText=$MessageText;
        #END Send Telegram Module
       
        :log info "Send DHCP Alert $"lease-hostname" $leaseActMAC"
	
	} on-error={:log error "Telegram notification failure"}
}
I have added a functionality ($interFace) so that it also informs me to which wireless interface it has connected.

The script works fine when the hosts are wireless devices, but when they are connected by ethernet cable the script fails and does not send info to Telegram: "no such item" since the MAC of the ethernet device is not in CAPsMAN's "Registration Table".

Any help correcting the script please?
BR.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script DHCP Lease not working

Tue Mar 01, 2022 1:55 pm

Hello,

I have this script on my DHCP server that informs me when someone connects to my LAN.
/ip dhcp-server lease
:if ($leaseBound = 1) do={
	:do {
        
        :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
        :local interFace [/caps-man/registration-table get [find where mac-address=$leaseActMAC] interface]
       
        # START Send Telegram Module
        :local MessageText "\E2\84\B9 <b>Mikrotik: Info DHCP</b> %0D%0A Name: $"lease-hostname" %0D%0A IP: $leaseActIP %0D%0A MAC: $leaseActMAC %0D%0A Interface: $interFace %0D%0A Comment: $Comment";
        :local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]; 
        $SendTelegramMessage MessageText=$MessageText;
        #END Send Telegram Module
       
        :log info "Send DHCP Alert $"lease-hostname" $leaseActMAC"
	
	} on-error={:log error "Telegram notification failure"}
}
I have added a functionality ($interFace) so that it also informs me to which wireless interface it has connected.

The script works fine when the hosts are wireless devices, but when they are connected by ethernet cable the script fails and does not send info to Telegram: "no such item" since the MAC of the ethernet device is not in CAPsMAN's "Registration Table".

Any help correcting the script please?
BR.
No one?
 
holvoetn
Forum Guru
Forum Guru
Posts: 5405
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Script DHCP Lease not working

Tue Mar 01, 2022 2:14 pm

If you're only looking at capsman, it makes perfect sense since it will only handle wireless connections.

Where is your DHCP server connected to ?
Can't you check the DHCP server on your LAN-bridge (since it's all one LAN, I would think ) and check the lease registrations there ?
What's the purpose of specifically looking at the capsman registrations ?
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script DHCP Lease not working

Tue Mar 01, 2022 4:01 pm

If you're only looking at capsman, it makes perfect sense since it will only handle wireless connections.

Where is your DHCP server connected to ?
Can't you check the DHCP server on your LAN-bridge (since it's all one LAN, I would think ) and check the lease registrations there ?
What's the purpose of specifically looking at the capsman registrations ?
This script is located in the DHCP Server (DHCP server -> script tab) and informs you of any device that connects to the local bridge.

Send this information by telegram:

Image

The fact of using CAPsMAN parameters is to obtain information to identify which wireless adapter has been connected to.

As you can see in the previous image, it works well and in Telegram I have all the information I need. The problem is that when someone physically connects to an ethernet port then the script fails and does not report any information.

If I run the script manually it gives me a "no such item" error since it can't compare the MAC of the CAPsMAN part and the MAC of the DHCP Lease part.

I hope I explained myself well (see the first post please).

Thanks.
BR.
 
holvoetn
Forum Guru
Forum Guru
Posts: 5405
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Script DHCP Lease not working

Tue Mar 01, 2022 4:52 pm

I'm not an expert in scripting but it seems you need to perform that check towards the capsman registration with some error clause so another default can be returned in case the check fails.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script DHCP Lease not working

Tue Mar 01, 2022 5:02 pm

I'm not an expert in scripting but it seems you need to perform that check towards the capsman registration with some error clause so another default can be returned in case the check fails.
I understand that, what I don't know is how to check that error with the IF conditional. If any expert can help me I would appreciate it.

BR.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Script DHCP Lease not working

Tue Mar 01, 2022 5:43 pm

This should do:
:if ($leaseBound = 1) do={
	:do {
        :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
	:local interFace
	:do {
		:set $interFace [/caps-man/registration-table get [find where mac-address=$leaseActMAC] interface]
	} on-error={:set $interFace NotCapsMan}
       
        # START Send Telegram Module
        :local MessageText "\E2\84\B9 <b>Mikrotik: Info DHCP</b> %0D%0A Name: $"lease-hostname" %0D%0A IP: $leaseActIP %0D%0A MAC $leaseActMAC %0D%0A Interface: $interFace %0D%0A Comment: $Comment"
        local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]
        $SendTelegramMessage MessageText=$MessageText
        #END Send Telegram Module
       
        :log warning "Send DHCP Alert $"lease-hostname" $leaseActMAC"
	
	} on-error={:log info "Telegram notification failure"}
}
If there are no caps-man information, set interface to NotCapsMan.
Since I do not have Telegram, I have not tested that part, but the other part did work on my test router.
I do send every log message and information to Splunk, and there I can see all who gets DHCP and when.
Also possible to send Alert email with information if needed.
 
holvoetn
Forum Guru
Forum Guru
Posts: 5405
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Script DHCP Lease not working

Tue Mar 01, 2022 6:06 pm

Super Jotne to the Rescue :lol:
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script DHCP Lease not working

Tue Mar 01, 2022 8:22 pm

This should do:
:if ($leaseBound = 1) do={
	:do {
        :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
	:local interFace
	:do {
		:set $interFace [/caps-man/registration-table get [find where mac-address=$leaseActMAC] interface]
	} on-error={:set $interFace NotCapsMan}
       
        # START Send Telegram Module
        :local MessageText "\E2\84\B9 <b>Mikrotik: Info DHCP</b> %0D%0A Name: $"lease-hostname" %0D%0A IP: $leaseActIP %0D%0A MAC $leaseActMAC %0D%0A Interface: $interFace %0D%0A Comment: $Comment"
        local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]
        $SendTelegramMessage MessageText=$MessageText
        #END Send Telegram Module
       
        :log warning "Send DHCP Alert $"lease-hostname" $leaseActMAC"
	
	} on-error={:log info "Telegram notification failure"}
}
If there are no caps-man information, set interface to NotCapsMan.
Since I do not have Telegram, I have not tested that part, but the other part did work on my test router.
I do send every log message and information to Splunk, and there I can see all who gets DHCP and when.
Also possible to send Alert email with information if needed.
Thank you very much Jotne! When I get home I'll try it. BR.
 
holvoetn
Forum Guru
Forum Guru
Posts: 5405
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Script DHCP Lease not working

Tue Mar 01, 2022 8:31 pm

Practical question
How many of those registrations you get a day ?
Isn't it driving you mad all these notifications ?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Script DHCP Lease not working

Tue Mar 01, 2022 8:36 pm

That is why I do use Splunk. All in one place and nice graph view of everything.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script DHCP Lease not working

Tue Mar 01, 2022 8:41 pm

Practical question
How many of those registrations you get a day ?
Isn't it driving you mad all these notifications ?
No not at all.

I have a bot created in Telegram only for my Private Network, the leases are registered and when I am interested I review it.

BR.
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Script DHCP Lease not working

Tue Mar 01, 2022 8:59 pm

This should do:
:if ($leaseBound = 1) do={
	:do {
        :local Comment [/ip dhcp-server lease get value-name=comment number=[/ip dhcp-server lease find address=$leaseActIP]]
	:local interFace
	:do {
		:set $interFace [/caps-man/registration-table get [find where mac-address=$leaseActMAC] interface]
	} on-error={:set $interFace NotCapsMan}
       
        # START Send Telegram Module
        :local MessageText "\E2\84\B9 <b>Mikrotik: Info DHCP</b> %0D%0A Name: $"lease-hostname" %0D%0A IP: $leaseActIP %0D%0A MAC $leaseActMAC %0D%0A Interface: $interFace %0D%0A Comment: $Comment"
        local SendTelegramMessage [:parse [/system script  get MyTGBotSendMessage source]]
        $SendTelegramMessage MessageText=$MessageText
        #END Send Telegram Module
       
        :log warning "Send DHCP Alert $"lease-hostname" $leaseActMAC"
	
	} on-error={:log info "Telegram notification failure"}
}
If there are no caps-man information, set interface to NotCapsMan.
Since I do not have Telegram, I have not tested that part, but the other part did work on my test router.
I do send every log message and information to Splunk, and there I can see all who gets DHCP and when.
Also possible to send Alert email with information if needed.
It works perfectly!! thanks for your help.

I am going to study the Splunk option, I see it as very interesting.

BR.

Who is online

Users browsing this forum: No registered users and 17 guests