Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace drops characters
    text
    copied!<p>I have a script i use that checks an IP address stored within my hosts.allow file against what IP is mapped to my dyndns hostname so i can log into my servers once i've synced my current IP to that hostname. For some reason though the script seems to cause really intermittent issues.</p> <p>within my hosts.allow file i have a section like this:</p> <pre><code>#SOme.gotdns.com sshd : 192.168.0.1 #EOme.gotdns.com #SOme2.gotdns.com sshd : 192.168.0.2 #EOme2.gotdns.com </code></pre> <p>I have a script running on a cron (every minute) that looks like this:</p> <pre><code>#!/usr/bin/php &lt;?php $hosts = array('me.gotdns.com','me2.gotdns.com'); foreach($hosts as $host) { $ip = gethostbyname($host); $replaceWith = "#SO".$host."\nsshd : ".$ip."\n#EO".$host; $filename = '/etc/hosts.allow'; $handle = fopen($filename,'r'); $contents = fread($handle, filesize($filename)); fclose($handle); if (preg_match('/#SO'.$host.'(.*?)#EO'.$host.'/si', $contents, $regs)) { $result = $regs[0]; } if($result != $replaceWith) { $newcontents = str_replace($result,$replaceWith,$contents); $handle = fopen($filename,'w'); if (fwrite($handle, $newcontents) === FALSE) { } fclose($handle); } } ?&gt; </code></pre> <p>The problem i have is that intermittently characters are being dropped (i assume during the replace) that causes future updates to fail as it inserts something like:</p> <pre><code>#SOme.gotdns.com sshd : 192.168.0.1 #EOme.gotdn </code></pre> <p><em>note the missing "s.com"</em></p> <p>This of course means i lose access to the server, any ideas why this would be happening?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload