How to delete leases from dhcpd.conf?

  • A+
Category:Languages

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"; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: