Community discussions

MikroTik App
 
plisken
Forum Guru
Forum Guru
Topic Author
Posts: 2509
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Mail DHCP-leases

Tue Jan 12, 2021 10:00 pm

I want to improve this script by having the DHCP servers automatically recognized. So that I don't have to enter DHCP1.
For clarity "ip dhcp server lease find where server = dhcp1"
I want all DHCP servers to be recognized so that I have the correct number of users and the appropriate DHCP server
Who can help me?
:local i;
:local hostip;
:local hostname;
:local dhcplist "";
:local date [/system clock get date];
:local time [/system clock get time];
:local leasesall [ :len [ /ip dhcp-server lease find where server=dhcp1 ] ];
:local firmware [ /system routerboard get current-firmware ];
:local routeros [ /system package get number=0 value-name=version ];
:local macaddress;
/ip dhcp-server lease;
:foreach i in=[find where server=dhcp1 ] do={
:set hostname [get $i host-name];
:set hostip [get $i address];
:set macaddress [get $i active-mac-address];
:set dhcplist ($dhcplist . $hostname . " " . $hostip . " " . $macaddress . " " . $date . " " . $time . "\n");
};
/tool e-mail send to=youremail@gmail.com subject="Mail subject" body=( "Aantal DHCP leases: $leasesall\n\n" . $dhcplist );
 
Sob
Forum Guru
Forum Guru
Posts: 9119
Joined: Mon Apr 20, 2009 9:11 pm

Re: Mail DHCP-leases

Tue Jan 12, 2021 11:16 pm

I don't know if it's the best way, but you can simply remove the condition, i.e. replace [find where server=dhcp1 ] with [find]. And to include server name in output, you get it from [get $i server] like all other properties.
 
plisken
Forum Guru
Forum Guru
Topic Author
Posts: 2509
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Mail DHCP-leases

Wed Jan 13, 2021 9:17 am

Thank you for your response, I am not that experienced in script creation.
Can you improve the full script here and put it online please? Thanks.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Mail DHCP-leases

Wed Jan 13, 2021 3:54 pm

You also do not need ; at end of all line.
Semicolon need to be used when you have multiple commands on same line.
Add TAB for all loops etc. Makes it simpler to read and understand.
:local i
:local hostip
:local hostname
:local dhcplist ""
:local date [/system clock get date]
:local time [/system clock get time]
:local leasesall [ :len [ /ip dhcp-server lease find where server=dhcp1 ] ]
:local firmware [ /system routerboard get current-firmware ]
:local routeros [ /system package get number=0 value-name=version ]
:local macaddress
/ip dhcp-server lease
:foreach i in=[find where server=dhcp1 ] do={
	:set hostname [get $i host-name]
	:set hostip [get $i address]
	:set macaddress [get $i active-mac-address]
	:set dhcplist ($dhcplist . $hostname . " " . $hostip . " " . $macaddress . " " . $date . " " . $time . "\n")
	}
/tool e-mail send to=youremail@gmail.com subject="Mail subject" body=( "Aantal DHCP leases: $leasesall\n\n" . $dhcplist )

Who is online

Users browsing this forum: No registered users and 21 guests