How to delete leases from dhcpd.conf?

I would like to delete leases from dhcpd.conf where I give the ip address to be deleted from a Bash variable. A dhcpd.conf example is at the button.

However I can't even get it to print a lease I want to delete

ip="192.168.234.5"; perl -0777snle 'print if /lease $ip \{.*?\}/' -- -ip=$ip dhcpd.leases 

Using Perl 5.16.3

Question

Can anyone see what I am doing wrong?

lease 192.168.234.5 {
  starts 3 2018/11/21 18:53:11;
  ends 3 2018/11/21 18:54:11;
  tstp 3 2018/11/21 18:54:11;
  cltt 3 2018/11/21 18:53:11;
  binding state free;
  hardware ethernet 14:99:e2:04:52:5c;
  uid "\001\024\231\342\004R\\";
}
lease 192.168.234.2 {
  starts 3 2018/11/21 20:42:48;
  ends 3 2018/11/21 20:52:48;
  tstp 3 2018/11/21 20:52:48;
  cltt 3 2018/11/21 20:42:48;
  binding state free;
  hardware ethernet 00:21:5e:90:13:fb;
  set MacAddress = "00:21:5E:90:13:FB";
  set output = "192.168.234.2,00:21:5E:90:13:FB,(host-decl-name n/a),5E9013FB,(dhcp-client-identifier n/a),(vendor-class-identifier n/a),(agent.remote n/a),(agent.circuit n/a)";
}
lease 192.168.234.13 {
  starts 3 2018/11/21 21:38:16;
  ends 3 2018/11/21 21:38:27;
  tstp 3 2018/11/21 21:38:27;
  cltt 3 2018/11/21 21:38:16;
  binding state free;
  hardware ethernet 00:e0:6f:11:7f:59;
  uid "\001\000\340o\021\177Y";
}

Based on @Kubator's awk answer:

# perl -lnse '$f = 0 if /^lease/; $f = 1 if /^lease $ip [{]/; print if not $f;' \
       -- -ip=192.168.234.2 leases
lease 192.168.234.5 {
  starts 3 2018/11/21 18:53:11;
  ends 3 2018/11/21 18:54:11;
  tstp 3 2018/11/21 18:54:11;
  cltt 3 2018/11/21 18:53:11;
  binding state free;
  hardware ethernet 14:99:e2:04:52:5c;
  uid "\001\024\231\342\004R\\";
}
lease 192.168.234.13 {
  starts 3 2018/11/21 21:38:16;
  ends 3 2018/11/21 21:38:27;
  tstp 3 2018/11/21 21:38:27;
  cltt 3 2018/11/21 21:38:16;
  binding state free;
  hardware ethernet 00:e0:6f:11:7f:59;
  uid "\001\000\340o\021\177Y";
}

How do I purge dhcpd.leases?, Hmm so I edited the lease time in etc/dhcp/dhcpd.conf, did "sudo chown -R dhcpd:dhcpd /var/lib/dhcp", then "sudo From the manual man dhcpd.leases : First, a temporary lease database is created and all known leases are dumped to it. You do not need to delete it, only remove what's not needed. I'm configuring DHCP server using dhcpd. When I remove a subnet from dhcpd.conf file, the leases of that subnet still in the dhcpd.leases file. Is there any parameter in the dhcpd.conf file tell the dhcpd delete leases from dhcpd.leases file when their subnet is removed from dhcpd.conf. Thanks!

Can anyone see what I am doing wrong?

Two things.

Firstly, you're reading in the whole file in one go (with -0777), which means you lose the ability to easily print a single stanza from your file. Better to use -0175 which is octal for "}". That way, your code is executed once for each stanza in the input file.

But it still doesn't work. That's because of your regex - /lease $ip \{.*?\}/. That dot, by default, matches any character except a newline. And as your '{' and '}' characters are always on different lines, that is never going to match.

You could add /s to the end of the match operator to change the meaning of . to match a newline as well. But I think it's easier to just truncate the regex to only match what's important to you - /lease $ip \{/. Actually, I'd add a ^ to the start, just to be really sure.

So, finally, we end up with this:

$ ip="192.168.234.5"; perl -0175snle 'print if /^lease $ip \{/' -- -ip=$ip dhcpd.leases
lease 192.168.234.5 {
  starts 3 2018/11/21 18:53:11;
  ends 3 2018/11/21 18:54:11;
  tstp 3 2018/11/21 18:54:11;
  cltt 3 2018/11/21 18:53:11;
  binding state free;
  hardware ethernet 14:99:e2:04:52:5c;
  uid "\001\024\231\342\004R\\";
}

Which seems to work :-)

Delete leases from dhcpd.leases when remove a subnet from dhcpd , Hi all! I'm configuring DHCP server using dhcpd. When I remove a subnet from dhcpd.conf file, the leases of that subnet still in the dhcpd.leases  It is possible to delete a declaration in the dhcpd.conf file; in this case, the rubout can never be eliminated from the dhcpd.leases file. COMMON STATEMENTS FOR LEASE DECLARATIONS While the lease file formats for DHCPv4 and DHCPv6 are different they share many common statements and structures.

In case You wanna AWK solution:

awk '/^lease/ { rem=0; } /^lease 192\.168\.234\.2/ { rem=1; } !rem' dhcpd.leases

Solution with passing BASH variable containing IP address to AWK:

awk '/^lease/ { rem=0; } /^lease '${var//./\\.}'/ { rem=1; } !rem' dhcpd.leases

As You see above in addition You have to escape each occurrence of dot (that's done by bash variable substitution ${var//./\\.} ), similar You did in PERL, because is part of regexp.

Test:

$ var='192.168.234.2'
$ awk '/^lease/ { rem=0; } /^lease '${var//./\\.}'/ { rem=1; } !rem' dhcpd.leases
lease 192.168.234.5 {
  starts 3 2018/11/21 18:53:11;
  ends 3 2018/11/21 18:54:11;
  tstp 3 2018/11/21 18:54:11;
  cltt 3 2018/11/21 18:53:11;
  binding state free;
  hardware ethernet 14:99:e2:04:52:5c;
  uid "\001\024\231\342\004R\\";
}
lease 192.168.234.13 {
  starts 3 2018/11/21 21:38:16;
  ends 3 2018/11/21 21:38:27;
  tstp 3 2018/11/21 21:38:27;
  cltt 3 2018/11/21 21:38:16;
  binding state free;
  hardware ethernet 00:e0:6f:11:7f:59;
  uid "\001\000\340o\021\177Y";
}

ISC DHCP 4.4 Manual Pages - dhcpd.leases, It is possible to delete a declaration in the dhcpd.conf file; in this case, the rubout can never be eliminated from the dhcpd.leases file. COMMON STATEMENTS  Edit your dhcpd.leases file and remove what entries you no longer need Save the file and start DHCPD server. You can optimize this by editing the file (and not save until ready) and then perform DHCPD stop / file save / DHCPD start in a matter of seconds or even faster.

ISC DHCP 4.4 Manual Pages - dhcpd.conf, If we wanted to test the DHCP leasing mechanism, we might set the lease than one DHCP server to update the DNS database without accidentally deleting A  How to clear the dhcp leases in Cisco Network Registrar You would issue "dhcp-subnet force-available" to make available the whole subnet you specified in the command. Or if you want to do it for a specific lease, you would issue "lease address force-available".

gawk comes with multi character record delimiters:

gawk -v ip="${ip}" 'BEGIN{RS=ORS="}\n"} $0 !~ ip' dhcp.leases

We split the input file into records delimited by }\n. It is safe to do so because the input file is auto-generated by the dhcp server and guaranteed to be free of newlines and other garbage.

Having that, we can just print those records which do not match the ip.


PS: If you are willing to accept an additional empty newline, which doesn't hurt and only happens in case the first record gets deleted, you can use a portable POSIX awk command:

awk -v ip="${ip}" 'BEGIN{RS=ORS="}"} $0 !~ ip' dhcp.leases

Note: POSIX defines RS as a single char.

dhcpd.leases(5) — isc-dhcp-server — Debian testing, dhcpd.leases - DHCP client lease database It is possible to delete a declaration in the dhcpd.conf file; in this case, the rubout can never be  3. Deleted the "dhcpd.leases" and "dhcpd.leases~" files. 4. Recreated the "dhcpd.leases" file. (touch dhcpd.leases) 5. Started the dhcpd service. (service dhcpd stop) Conclusion: The server started leasing IP's in the environment again. Part-II Issue: Again the server stopped after some time. Workaround was the same as mentioned in the Part-I.

dhcpd.leases: DHCP client lease database, It is possible to delete a declaration in the dhcpd.conf file; in this case, the rubout can never be eliminated from the dhcpd.leases file. THE LEASE DECLARATION. delete dhcp.conf entry using sed. I am trying to use sed to remove entries from my dhcpd.conf file. The form of the file is: Code: host foo { option 1 option 2 } host

dhcpd.conf(5): dhcpd config file, When the client's lease expires, the DHCP server (if it is operating at the time, or when next it operates) will remove the client's A and PTR records from the DNS  Lease descriptions are stored in a format that is parsed by the same recursive descent parser used to read the dhcpd.conf(5) and dhclient.conf(5) files. Lease files can contain lease declarations, and also group and subgroup declarations, host declarations and failover state declarations.

dhcpd(8), On startup, after reading the dhcpd.conf file, dhcpd reads the dhcpd.leases When an address is properly leased, dhcpd will remove the address from this table. It's possible to delete a declaration in the dhcpd.conf file; in this case, the rubout can never be eliminated from the dhcpd.leases file. The lease declaration. A lease declaration takes this form: lease ip-address { statements } Each lease declaration includes the single IP address that has been leased to the client.

Comments
  • This might be interesting for you: The dhcp server supports a management protocol call omapi. If enabled, you can manage certain types of objects (like leases) while the dhcp server is running. There is a command line client called omshell which can be used.
  • Or, perhaps, a bit shorted, print if /^lease (?!$ip )/ .. /\}/
  • @Dada - im not a heavy perl user and never know it has a .. operator. cool!
  • @Dada What does (?!$ip ) mean?
  • see perlre doc: (?!pattern) - A zero-width negative lookahead assertion. For example /foo(?!bar)/ matches any occurrence of foo that isn't followed by bar.
  • The -0175 is really cool! However it doesn't print anything for me, when I try your version.
  • @SandraSchlichting: Hmm... I'm using a newer Perl (5.26.1) but I can't see anything in my code that would be affected by that.
  • Just tested with 5.26.2 and doesn't work there either. Could it be an encoding issue? file says it is just "ASCII text".
  • I added solution with passing bash variable into answer.