Community discussions

 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Send DHCP leases every day

Tue Jun 02, 2015 11:12 pm

Hello,

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

Kris
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 1702
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Send DHCP leases every day

Wed Jun 03, 2015 1:50 am

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
Real admins use real keyboards.
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Tue Jun 23, 2015 2:34 pm

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.
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Tue Jul 07, 2015 12:45 am

Somebody elke that know the solution for this problem?
Would be very nice if somebody could help me!

Best regards,
Kris
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Thu Aug 13, 2015 10:04 pm

Somebody? :-)
 
nescafe2002
Long time Member
Long time Member
Posts: 615
Joined: Tue Aug 11, 2015 12:46 pm
Location: Netherlands

Re: Send DHCP leases every day

Thu Aug 13, 2015 10:41 pm

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

Re: Send DHCP leases every day

Thu Aug 13, 2015 10:51 pm

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.
Real admins use real keyboards.
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Fri Aug 14, 2015 12:17 am

That work great!
But is there a possibility to mention the number of DHCP leases on top of it?
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Fri Aug 14, 2015 1:31 am

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 :-)
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Wed Aug 26, 2015 1:05 am

Any tips or idea's on this one?
 
marting
Member Candidate
Member Candidate
Posts: 145
Joined: Thu Aug 21, 2014 2:07 pm

Re: Send DHCP leases every day

Wed Aug 26, 2015 1:33 pm

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 );
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Thu Aug 27, 2015 11:42 pm

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!
 
marting
Member Candidate
Member Candidate
Posts: 145
Joined: Thu Aug 21, 2014 2:07 pm

Re: Send DHCP leases every day

Fri Aug 28, 2015 4:07 pm

:local firmware [ /system routerboard get current-firmware ]
:local routeros [ /system package get number=0 value-name=version ]
 
plisken
Forum Guru
Forum Guru
Posts: 2399
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Send DHCP leases every day

Fri Aug 28, 2015 11:42 pm

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?
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Sat Aug 29, 2015 12:32 am

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

Re: Send DHCP leases every day

Sat Aug 29, 2015 2:03 am

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
Real admins use real keyboards.
 
n21roadie
Forum Guru
Forum Guru
Posts: 1886
Joined: Fri Aug 07, 2009 10:36 pm
Location: Limerick,Ireland

Re: Send DHCP leases every day

Sun Aug 30, 2015 3:41 pm

Is it possible to find "Busy" from DHCP lease list status apart from "Bound"
N21roadie,
Network 100% MT for Now?
 
marting
Member Candidate
Member Candidate
Posts: 145
Joined: Thu Aug 21, 2014 2:07 pm

Re: Send DHCP leases every day

Mon Aug 31, 2015 11:32 am

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
 
plisken
Forum Guru
Forum Guru
Posts: 2399
Joined: Sun May 15, 2011 12:24 am
Location: Belgium
Contact:

Re: Send DHCP leases every day

Mon Aug 31, 2015 9:53 pm

Can you post the complete script with add MAC-address and ROS please
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Tue Sep 01, 2015 1:52 am

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 );
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Mon Sep 28, 2015 10:50 pm

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?
 
troffasky
Member
Member
Posts: 394
Joined: Wed Mar 26, 2014 4:37 pm

Re: Send DHCP leases every day

Tue Sep 29, 2015 10:35 pm

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.
 
Krisken
Member Candidate
Member Candidate
Topic Author
Posts: 132
Joined: Thu Oct 25, 2012 11:35 am

Re: Send DHCP leases every day

Tue Sep 29, 2015 10:44 pm

Is it possible to do this with eg PHP?

Who is online

Users browsing this forum: Bing [Bot] and 31 guests