Page 1 of 1

Send DHCP leases every day

Posted: Tue Jun 02, 2015 11:12 pm
by Krisken
Hello,

Is it possible for a Routerboard to email me the complete list of DHCP server leases to me every day?

Kris

Re: Send DHCP leases every day

Posted: Wed Jun 03, 2015 1:50 am
by BartoszP
This should work:
:local i;
:local hostip;
:local hostname;
:local dhcplist "";

/ip dhcp-server lease;
:foreach i in=[find where server=DHCPSERVER_192 ] do={
  :set hostname [get $i host-name];
  :set hostip [get $i address];
  :set dhcplist ( $dhcplist . $hostname . " " . "$hostip . "\n" )
}
/tool e-mail send to=somemail@nowhere.com subject="DHCPLIST" body=$dhcplist;
You need to:
- configure e-mail data in Tools/Email to use short send form.
- replace DHCP server name in the script
- configure System/Sheduler to invoke script periodically

Re: Send DHCP leases every day

Posted: Tue Jun 23, 2015 2:34 pm
by Krisken
So I have this:
:local i;
:local hostip;
:local hostname;
:local dhcplist "";

/ip dhcp-server lease;
:foreach i in=[find where server=dhcp1] do={
  :set hostname [get $i host-name];
  :set hostip [get $i address];
  :set dhcplist ( $dhcplist . $hostname . " " . "$hostip . "\n" )
}
/tool e-mail send to=info@mymail.eu subject="DHCP Leases KrIsa" body=$dhcplist;
Is this correct? Because that doesn't seem to work over here.
Another script works well, eg a backup and export to my mailaddress every night.

Re: Send DHCP leases every day

Posted: Tue Jul 07, 2015 12:45 am
by Krisken
Somebody elke that know the solution for this problem?
Would be very nice if somebody could help me!

Best regards,
Kris

Re: Send DHCP leases every day

Posted: Thu Aug 13, 2015 10:04 pm
by Krisken
Somebody? :-)

Re: Send DHCP leases every day

Posted: Thu Aug 13, 2015 10:41 pm
by nescafe2002
Hi there!

The hint "doesn't seem to work" is not very helpful for debugging this script... but I must say the "Run Script" option in the Scripts dialog of WinBox doesn't provide any further information.

If you call the script via the terminal, you get a much better explanation for failing:
[admin@MikroTik] > system script run MyScript
syntax error (line 10 column 61)
Which is this line and column:
  :set dhcplist ( $dhcplist . $hostname . " " . "$hostip . "\n" )
                                                            ^
Just remove the quote before $hostip and it will work.. remember to set the email server under Tools > Email

Re: Send DHCP leases every day

Posted: Thu Aug 13, 2015 10:51 pm
by BartoszP
Checked...there was extra " before $hostip
:local i;
:local hostip;
:local hostname;
:local dhcplist "";

/ip dhcp-server lease;
:foreach i in=[find where server=DHCPSERVER_192 ] do={
  :set hostname [get $i host-name];
  :set hostip [get $i address];
  :set dhcplist ($dhcplist . $hostname . " " . $hostip . "\n");
};
/tool e-mail send to=myaddress  subject="DHCPLIST" body=$dhcplist;
If you want to see where is the code error then write script, open CLI, goto /system script and then do print command.
all scripts will be listed with brown block showing syntax error.

Re: Send DHCP leases every day

Posted: Fri Aug 14, 2015 12:17 am
by Krisken
That work great!
But is there a possibility to mention the number of DHCP leases on top of it?

Re: Send DHCP leases every day

Posted: Fri Aug 14, 2015 1:31 am
by Krisken
I just found this out... At this moment there are 323 connected devices, and 299 of them have an IP from the EOIP pool...
[admin@IT2GO - Router] /ip dhcp-server lease> print count-only
323
[admin@IT2GO - Router] /ip dhcp-server lease> print count-only where server=EOIP
299
But how to i edit it in code for the ROS scriping :-)

Re: Send DHCP leases every day

Posted: Wed Aug 26, 2015 1:05 am
by Krisken
Any tips or idea's on this one?

Re: Send DHCP leases every day

Posted: Wed Aug 26, 2015 1:33 pm
by marting
Something like this:
:local i;
:local hostip;
:local hostname;
:local dhcplist "";
:local leasesall [ :len [ /ip dhcp-server lease find ] ];
:local leaseseoip [ :len [ /ip dhcp-server lease find where server=EOIP ] ];

/ip dhcp-server lease;
:foreach i in=[find where server=DHCPSERVER_192 ] do={
  :set hostname [get $i host-name];
  :set hostip [get $i address];
  :set dhcplist ($dhcplist . $hostname . " " . $hostip . "\n");
};
/tool e-mail send to=myaddress  subject="DHCPLIST" body=( "ALL: " . $leasesall . "\nEOIP: " . $leaseseoip . "\n\n" . $dhcplist );

Re: Send DHCP leases every day

Posted: Thu Aug 27, 2015 11:42 pm
by Krisken
Wow that did work indeed!
And then...the final question :-)

Is it possible to also mention the current firmware (system routerboard print) and the winbox version (system package print, column "version")?

Something like:

ALL: 296
EOIP: 268
Winbox Version: 6.30.2
Firmware: 3.24

Best regards, and tnx for the help!

Re: Send DHCP leases every day

Posted: Fri Aug 28, 2015 4:07 pm
by marting
:local firmware [ /system routerboard get current-firmware ]
:local routeros [ /system package get number=0 value-name=version ]

Re: Send DHCP leases every day

Posted: Fri Aug 28, 2015 11:42 pm
by plisken
Something like this:
:local i;
:local hostip;
:local hostname;
:local dhcplist "";
:local leasesall [ :len [ /ip dhcp-server lease find ] ];
:local leaseseoip [ :len [ /ip dhcp-server lease find where server=EOIP ] ];

/ip dhcp-server lease;
:foreach i in=[find where server=DHCPSERVER_192 ] do={
  :set hostname [get $i host-name];
  :set hostip [get $i address];
  :set dhcplist ($dhcplist . $hostname . " " . $hostip . "\n");
};
/tool e-mail send to=myaddress  subject="DHCPLIST" body=( "ALL: " . $leasesall . "\nEOIP: " . $leaseseoip . "\n\n" . $dhcplist );
This works fine, but can MAC-address add to this report?

Re: Send DHCP leases every day

Posted: Sat Aug 29, 2015 12:32 am
by Krisken
:local i;
:local hostip;
:local hostname;
:local dhcplist "";
:local leasesall [ :len [ /ip dhcp-server lease find where server=Lan ] ];
:local firmware [ /system routerboard get current-firmware ];
:local routeros [ /system package get number=0 value-name=version ];

/ip dhcp-server lease;
:foreach i in=[find where server=Lan ] 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 . "\n");
};
/tool e-mail send to=info@it2go.eu  subject="DHCP Leases KrIsa Lan" body=( "DHCP Leases: " . $leasesall . "\n\n" "Firmware: " . $firmware . "\n\n" "RouterOS: " . $routeros . "\n\n" . $dhcplist );
I "tried" (*kuch*), but i failed :-)
I tought that the script above would print me a list like this
DHCP Leases : 200
Firmware : x.y.z
RouterOS : a.b.c

blablabla 10.10.10.10 aa:bb:cc:dd:ee:ff
The only thing it did...
RouterOS: 6.30.2

voip01 10.0.0.11
IT2GO-Fix 10.0.0.101
So it does print the ROS version, the hostname and the ip but it doesn't count the number of DHCP leases anymore, nor did it mention the firmware version or the mac addresses...

Again : somebody that know the solution? :$

Re: Send DHCP leases every day

Posted: Sat Aug 29, 2015 2:03 am
by BartoszP
If your script does not work the easiest way to check what could be wrong is to check if the syntax is correct.
Tha latest script has errors:

please add at the top... there is no macaddress variable declared so assigning to it fails and script stops:
:local macaddress
and change body text expression...there are missing dots (string concatenation):
("DHCP Leases: $leasesall\nFirmware: $firmware\nRouterOS: $routeros\n" . $dhcplist )
and all should work

Re: Send DHCP leases every day

Posted: Sun Aug 30, 2015 3:41 pm
by n21roadie
Is it possible to find "Busy" from DHCP lease list status apart from "Bound"

Re: Send DHCP leases every day

Posted: Mon Aug 31, 2015 11:32 am
by marting
yes, but I think you can do a little work, too.

Hint:
[me@somewhere] /ip dhcp-server lease> :foreach i in=[find ] do={ :put [get $i status ] } 
waiting
bound
waiting

Re: Send DHCP leases every day

Posted: Mon Aug 31, 2015 9:53 pm
by plisken
Can you post the complete script with add MAC-address and ROS please

Re: Send DHCP leases every day

Posted: Tue Sep 01, 2015 1:52 am
by Krisken
Plisken,

This should work if you only have one DHCP server
:local i;
:local hostip;
:local hostname;
:local dhcplist "";
:local leasesall [ :len [ /ip dhcp-server lease find where server=Lan ] ];
: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=Lan ] 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 . "\n");
};
/tool e-mail send to=your@mail.address  subject="Subject of mail" body=( "Amount of DHCP leases: $leasesall\n\nFirmware: $firmware\nRouterOS: $routeros\n\n" . $dhcplist );

Re: Send DHCP leases every day

Posted: Mon Sep 28, 2015 10:50 pm
by Krisken
So, this script does a great job:
:local i;
:local hostip;
:local hostname;
:local dhcplist "";
: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 . "\n");
};
/tool e-mail send to=my@e-mail.address subject="Subject of the mail" body=( "Total DHCP leases: $leasesall\n\nFirmware: $firmware\nRouterOS: $routeros\n\n" . $dhcplist );
Is it possible to create a script that connects to a MySQL server, and writes the total amount of DHCP leases to a database, each day at midnight?

Re: Send DHCP leases every day

Posted: Tue Sep 29, 2015 10:35 pm
by troffasky
You might be able to poll such a parameter with SNMP, and that being the case, you can do it from the NMS of your choice.

Re: Send DHCP leases every day

Posted: Tue Sep 29, 2015 10:44 pm
by Krisken
Is it possible to do this with eg PHP?