Jump to content

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
https://forums.phpfreaks.com/topic/307335-delete-host-from-conf-file-with-php/
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";
}

 

  • Like 1
  • Great Answer 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.