Baruti Posted June 1, 2018 Share Posted June 1, 2018 Hello, can anyone have idea how to delete HOST from conf file, dhe conf file is a txt format i have dhcpd conf file is like this # interface eth0 subnet 10.0.10.0 netmask 255.255.255.0 { } host cm-test1 { hardware ethernet 78:8d:f7:2b:bc:79; fixed-address 172.17.12.80; filename "srv-050.cfg"; } host cm-test2 { hardware ethernet 5c:35:3b:4d:73:4b; fixed-address 172.17.13.119; filename "srv-042.cfg"; } and i want to delete only hosts host cm-instrumenti { hardware ethernet 78:8d:f7:2b:bc:79; fixed-address 172.17.12.80; filename "srv-050.cfg"; } host cm-nimonmehmetaj { hardware ethernet 5c:35:3b:4d:73:4b; fixed-address 172.17.13.119; filename "srv-042.cfg"; } i have tried with this function function deleteLineInFile($file,$string) { $i=0;$array=array(); $read = fopen($file, "r") or die("can't open the file"); while(!feof($read)) { $array[$i] = fgets($read); ++$i; } fclose($read); $write = fopen($file, "w") or die("can't open the file"); foreach($array as $a) { if(!strstr($a,$string)) fwrite($write,$a); } fclose($write); } echo deleteLineInFile("dhcpd.conf","host cm-test1"); and deletet this line host cm-test1 { I can delete row per row with this function, but i think is not a god way thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/307335-delete-host-from-conf-file-with-php/ Share on other sites More sharing options...
Barand Posted June 1, 2018 Share Posted June 1, 2018 Commencing with test_config.txt as # interface eth0 subnet 10.0.10.0 netmask 255.255.255.0 { } host cm-test1 { hardware ethernet 78:8d:f7:2b:bc:79; fixed-address 172.17.12.80; filename "srv-050.cfg"; } host cm-test2 { hardware ethernet 5c:35:3b:4d:73:4b; fixed-address 172.17.13.119; filename "srv-042.cfg"; } host cm-instrumenti { hardware ethernet 78:8d:f7:2b:bc:79; fixed-address 172.17.12.80; filename "srv-050.cfg"; } host cm-nimonmehmetaj { hardware ethernet 5c:35:3b:4d:73:4b; fixed-address 172.17.13.119; filename "srv-042.cfg"; } This is one way $unwanted = [ 'cm-instrumenti', 'cm-nimonmehmetaj' ]; // unwantedt host names $text = file_get_contents('test_config.txt'); $config = array_map('trim',explode('}', $text)); $new_config = array_filter($config, function ($v) use($unwanted) { foreach ($unwanted as $name) { if (strpos($v, $name)!==false) return false; // remove unwanted elements } return $v!=''; // remove blank entries }); file_put_contents('new_config.txt', join("\n}\n", $new_config) . "\n}\n"); giving new_config.txt as # interface eth0 subnet 10.0.10.0 netmask 255.255.255.0 { } host cm-test1 { hardware ethernet 78:8d:f7:2b:bc:79; fixed-address 172.17.12.80; filename "srv-050.cfg"; } host cm-test2 { hardware ethernet 5c:35:3b:4d:73:4b; fixed-address 172.17.13.119; filename "srv-042.cfg"; } 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/307335-delete-host-from-conf-file-with-php/#findComment-1558717 Share on other sites More sharing options...
Baruti Posted June 1, 2018 Author Share Posted June 1, 2018 it work like a charm, thank you Quote Link to comment https://forums.phpfreaks.com/topic/307335-delete-host-from-conf-file-with-php/#findComment-1558719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.