Community discussions

 
millenium7
Member Candidate
Member Candidate
Topic Author
Posts: 200
Joined: Wed Mar 16, 2016 6:12 am

Script for DHCP leases without terminal width issue

Thu May 30, 2019 10:52 am

I've written a small script that solves the problem of values being cut off in the output, as MikroTik sometimes infers an 80 character column width
Note to MikroTik devs: Please add a console command to manually set terminal width like almost every network vendor has. I know you can set it via the username but this is a messy and often incompatible way when working with other software that uses credentials from a central repository. And doesn't always work anyway i.e. like when trying to run '/ip dhcp-server lease print where dynamic' via SSH terminal
/ip dhcp-server lease
:put "[IP ADDRESS]\t[MAC ADDRESS]\t\t[DHCP SERVER]\t[STATE]\t[HOSTNAME]" ; :foreach i in=[find where dynamic=yes] do={:put ([get $i address]."\t".[get $i mac-address]."\t".[get $i server]."\t".[get $i status]."\t".[get $i host-name])}
I am using Solarwinds Network Configuration Manager to push the /ip dhcp-server lease command to a whole bunch of routers simultaneously to find devices that havn't been set statically yet (and others such as looking for unbound leases that I can remove) and then getting the results emailed to me. Problem is the text is getting cut off and one of the key values I need in the report is the hostname.
i.e. Problem before was I was getting output text this like
   0 D 172.16.15.55                                 FC:15:B4:34:BA:97 AF dhc..
   1 D 172.16.15.85                                 00:15:65:F3:A8:D8 SI dhc..
   2 D 172.16.15.99                                 94:C6:91:19:F5:CE DE dhc..
   3 D 172.16.15.41                                 00:15:65:F3:AC:1E SI dhc..
After the script i'm getting output like this
[IP ADDRESS]    [MAC ADDRESS]           [DHCP SERVER]   [STATE] [HOSTNAME]
172.16.15.55    FC:15:B4:34:BA:97       dhcpserver      bound   MyDevice
172.16.15.85    00:15:65:F3:A8:D8       dhcpserver      bound   SIP-T58
172.16.15.84    30:05:5C:A1:DF:F6       dhcpserver      bound   BRN30055CA1DFF6
172.16.15.99    94:C6:91:19:F5:CE       dhcpserver      bound   DESKTOP-G9816N0
filtering can be adjusted by editing the "[find where dynamic=yes]" part. For example finding all the currently bound leases you would use "[find where status=bound]" or the opposite "[find where status!=bound]" and you can combine criteria i.e. disabled, static, server instance called 'mymaindhcp' = "[find where disabled !dynamic server=mymaindhcp]"

Who is online

Users browsing this forum: No registered users and 4 guests