Created
March 7, 2022 03:57
-
-
Save SpareSimian/4f2f3a7111c3d277964b092a900ac11d to your computer and use it in GitHub Desktop.
Convert ISC dhcpd.leases file to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use JSON; | |
use experimental qw(switch); | |
my %leases; | |
my %current_lease; | |
my $current_lease_ip; | |
sub save_lease { | |
if ($current_lease_ip) { | |
$leases{$current_lease_ip} = { %current_lease }; | |
%current_lease = (); | |
$current_lease_ip = undef; | |
} | |
} | |
foreach (<>) { | |
chomp; # eat the newlines | |
when (/^lease ([0-9.]+) {$/) { | |
save_lease(); | |
$current_lease_ip = $1; | |
} | |
save_lease() when (/^}$/); | |
$current_lease{"starts"} = $1 when (/^ +starts (.+);$/); | |
$current_lease{"ends"} = $1 when (/^ +ends (.+);$/); | |
$current_lease{"tstp"} = $1 when (/^ +tstp (.+);$/); | |
$current_lease{"cltt"} = $1 when (/^ +cltt (.+);$/); | |
$current_lease{"binding state"} = $1 when (/^ +binding state (.+);$/); | |
$current_lease{"next binding state"} = $1 when (/^ +next binding state (.+);$/); | |
$current_lease{"rewind binding state"} = $1 when (/^ +rewind binding state (.+);$/); | |
$current_lease{"hardware ethernet"} = $1 when (/^ +hardware ethernet (.+);$/); | |
$current_lease{"uid"} = $1 when (/^ +uid "(.+)";$/); | |
$current_lease{"ddns-rev-name"} = $1 when (/^ +set ddns-rev-name = "(.+)";$/); | |
$current_lease{"ddns-txt"} = $1 when (/^ +set ddns-txt = "(.+)";$/); | |
$current_lease{"ddns-fwd-name"} = $1 when (/^ +set ddns-fwd-name = "(.+)";$/); | |
$current_lease{"client-hostname"} = $1 when (/^ +client-hostname "(.+)";$/); | |
next when (/^server-duid "(.*)";$/); | |
next when (/^ *#/); # comment | |
next when (/^ *$/); # blank lines | |
default { print "Unrecognized line '$_'\n"; } | |
} | |
my $json = JSON->new->utf8->pretty->encode(\%leases); | |
print $json; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment