我有一个脚本,我用它检查存储在我的hosts.allow文件中的IP地址,以反映什么IP映射到我的dyndns主机名,这样我就可以登录到我的服务器,一旦我同步我的当前IP到那主机名。出于某种原因,尽管脚本似乎会造成真正的间歇性问题。替换丢弃字符
我的hosts.allow文件中我有这样一段:
#SOme.gotdns.com
sshd : 192.168.0.1
#EOme.gotdns.com
#SOme2.gotdns.com
sshd : 192.168.0.2
#EOme2.gotdns.com
我有一个cron运行的脚本(每分钟),看起来像这样:
#!/usr/bin/php
<?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);
}
}
?>
问题我的是间歇性字符正在被丢弃(我假设替换过程中),导致未来的更新失败,因为它插入类似的东西:
#SOme.gotdns.com
sshd : 192.168.0.1
#EOme.gotdn
注意失踪“s.com”
当然,这意味着我失去了对服务器的访问,任何想法,为什么这会发生?
谢谢。
你确定两个脚本不会在同一时间运行? – RageZ 2011-12-21 10:33:05
你的意思是在第一个实例完成之前再次运行相同的脚本吗? – robjmills 2011-12-21 10:42:52
是的,因为脚本使用相同的文件进行读写,所以同时运行的两个进程可能会产生一些有趣的结果。 – RageZ 2011-12-21 10:44:41