Jump to content


Photo

editing a specific line in a file


  • Please log in to reply
8 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 08:15 AM

I am attempting to create a script that, when I purchase a new domain or add a subdomain, will update several files. I need to be able edit a specific line in a file.

here is an example of what I have to write a zonefile

<?php
$filename = 'C:\\windows\\system32\\dns\\etc\mysite1.com.zone';
$handle = fopen($filename, "x+");
$somecontent = "\$TTL 900		

@		SOA	    ns1 (		
			    postmaster	
			    2004041700	
			    21600		
			    1800		
			    604800		
			    900 

@		NS	    ns1		
@		NS	    ns2		

@		A	    xx.xx.xxx.xx	

@		MX	    10 mail		

ns1		A	    xx.xx.xxx.xx	
ns2		A	    xx.xx.xxx.xx	

mail	             A	    xx.xx.xxx.xx	

www		A	    xx.xx.xxx.xx";
fwrite($handle, $somecontent);
fclose($handle);
?>

Now if I want to add a cname I will need to open this file and edit a specific line. I would also want to do this with my named.conf and httpd.conf  here is an example:

say I want to add a domain, I will need to create a zone file and add 4 lines to my named.conf

lines 89 through 97
zone "mysite1.com" IN {
	type master;
	file "mysite1.com.zone";
};

zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

So when I create the zone file for my new domain(mysite3.com) I will need to add 4 more lines after line 97. Line 97 is not the last line in the file so I need to be able to add the info to a specific line. The same thing applies.

Any suggestions would be great.


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 11:16 AM

I tried this

<?php
$file = file('C:\\windows\\system32\\dns\\etc\\test_named.conf');
$filename = 'C:\\windows\\system32\\dns\\etc\\test_named.conf';
$lastsite = "mysite2.com";
$lastzone = "mysite2.com.zone";
$layout = "zone \"$lastsite\" IN {
	type master;
	file \"$lastzone\";
};";
$handle = fopen($filename, "w+b");
$newdomain = "mysite3.com";
$newzone = "mysite3.com.zone";
$somecontent = "
zone \"$newdomain\" IN {
	type master;
	file \"$newzone\";
};"; 
foreach($file as $line_number => $content) {
    if (trim($content) == $layout) {
        fwrite($handle, $somecontent);
        fclose($handle);
    }
}
?>

here is the contents of test_named.conf

zone "mysite.com" IN {
	type master;
	file "mysite.com.zone";
};

zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
	type master;
	file "xx.xx.xxx.rev";
};


it is not writing, it is erasing all of the content in test_named.conf . This only happens if I use "w",  "w+", "wb",  or "w+b" . If I use any variation of "r" nothing happens.

Anyone have any suggestions as to why this is happening or how I can fix it?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 11:35 AM

I think the best thing to do is read the whole file into a string/array then edit it and then write it back out to the file.

W,W+,WB etc. all truncate the data before writing. a is for appending, but because you are editing certain lines you can't use it.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 11:41 AM

can you give me an example of writing this file into an array or string

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 11:42 AM

Actually, you just need one modification to your code ... if the line doesn't match the one you're looking for, just write it out as is. Also move the fclose() to after the foreach loop.
<?php
foreach($file as $line_number => $content) {
    if (trim($content) == $layout) {
        fwrite($handle, $somecontent);
    }
    fwrite($handle,$content."\n");
}
fclose($handle);
?>

Ken

#6 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 12:02 PM

ken,

    I tired what you suggested and it just repeats mysite and mysite2

Here is test_named.conf before

zone "mysite.com" IN {
            type master;
            file "mysite.com.zone";
};

zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
	type master;
	file "xx.xx.xxx.rev";
};



and after



zone "mysite.com" IN {
	type master;
	file "mysite.com.zone";
};

zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
	type master;
	file "xx.xx.xxx.rev";
};zone "mysite.com" IN {

	type master;

	file "mysite.com.zone";

};



zone "mysite2.com" IN {

	type master;

	file "mysite2.com.zone";

};



zone "xxx.xx.xx.in-addr.arpa" {

	type master;

	file "xx.xx.xxx.rev";

};


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 12:15 PM

now I tried it this way

<?php
$file = file('C:\\windows\\system32\\dns\\etc\\test_named.conf');
$filename = 'C:\\windows\\system32\\dns\\etc\\test_named.conf';
$lastsite = "mysite2.com";
$lastzone = "mysite2.com.zone";
$layout = "zone \"$lastsite\" IN {
	type master;
	file \"$lastzone\";
};";
$handle = fopen($filename, "a");
$newdomain = "mysite3.com";
$newzone = "mysite3.com.zone";
$somecontent = "
zone \"$newdomain\" IN {
	type master;
	file \"$newzone\";
};"; 
foreach($file as $line_number => $content) {
    if (trim($content) == $layout) {
        fwrite($handle, $somecontent);
    }
    fwrite($handle,$somecontent."\n");
}
fclose($handle);
?>


The result


zone "mysite.com" IN {
	type master;
	file "mysite.com.zone";
};

zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
	type master;
	file "xx.xx.xxx.rev";
};
zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

zone "mysite3.com" IN {
	type master;
	file "mysite3.com.zone";
};

as you cab see it just repeats it's self over and over again

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 01:05 PM

Look at my suggestion again. The fwrite() when the if condition is not met is different than when the if condition is met. It will work.

Ken

#9 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 11 July 2006 - 03:58 PM

The Fix

I added a line in the test_named.php

#BEGIN AUTO RECORDS
zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

zone "mysite.com" IN {
	type master;
	file "mysite.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
	type master;
	file "xx.xx.xxx.rev";
};



Then I edited the update.php


<?php
$file = 'C:/windows/system32/dns/etc/test_named.conf'; 
$fp = fopen($file, 'r+'); 
$newdomain = "mysite3.com"; 
$newzone = "mysite3.com.zone"; 
$layout = " 
zone \"$newdomain\" IN { 
    type master; 
    file \"$newzone\"; 
}; 
"; 

while (!feof($fp)){ 
    $line = trim(fgets($fp, 1024)); 
    if ($line == '#BEGIN AUTO RECORDS'){ 
        //Find where we are so we can come back. 
        $fpos = ftell($fp); 
        //First, read the rest of the file, so we don't overwrite it. 
        $rest = fread($fp, filesize($file)); 

        //Go back 
        fseek($fp, $fpos, SEEK_SET); 
        //Write the new data after the marker. 
        fwrite($fp, "\n\n"); 
        fwrite($fp, $layout); 
        //Write back the rest of the data. 
        fwrite($fp, $rest); 
    } 
} 
fclose($fp);
?>

This will add the new domain like this

#BEGIN AUTO RECORDS
zone "mysite3.com" IN { //the new domain
	type master;
	file "mysite3.com.zone";
};

zone "mysite2.com" IN {
	type master;
	file "mysite2.com.zone";
};

zone "mysite.com" IN {
	type master;
	file "mysite.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
	type master;
	file "xx.xx.xxx.rev";
};

The only issue that I see that maybe a problem is that there is a large space after the #BEGIN AUTO RECORDS . I have created a basic form to add a new domain(this is just a test) You can see it here
http://www.owpt.biz/home/test/open.php
Add a domain to see what I mean

Any suggestions would be great.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users