Jump to content

Delete HOST from CONF file with PHP


Baruti

Recommended Posts

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 :)

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.