Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:04:01+00:00 2026-05-27T20:04:01+00:00

I have a script i use that checks an IP address stored within my

  • 0

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.

within my hosts.allow file i have a section like this:

#SOme.gotdns.com
sshd : 192.168.0.1
#EOme.gotdns.com

#SOme2.gotdns.com
sshd : 192.168.0.2
#EOme2.gotdns.com

I have a script running on a cron (every minute) that looks like this:

#!/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);
        }
}
?>

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:

#SOme.gotdns.com
sshd : 192.168.0.1
#EOme.gotdn

note the missing “s.com”

This of course means i lose access to the server, any ideas why this would be happening?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:04:01+00:00Added an answer on May 27, 2026 at 8:04 pm

    I would say that in order to do this safely, you should acquire an exclusive lock on the file at the beginning of the script, read it all into memory once, modify it in memory, then write it back to the file at the end. This would also be considerably more efficient in terms of disk I/O.

    You should also alter the cron job to run less frequently. It is likely that the reason you currently have this problem is because two processes are running at the same time – by locking the file, if this is the case, you risk having the processes stack up waiting to acquire a lock. Setting it for every 5 minutes should be good enough – your IP shouldn’t change that often!

    So do this (FIXED):

    #!/usr/bin/php
    <?php
    
      // Settings
      $hosts = array(
        'me.gotdns.com',
        'me2.gotdns.com'
      );
      $filename = '/etc/hosts.allow';
    
      // No time limit (shouldn't be necessary with CLI, but just in case)
      set_time_limit(0);
    
      // Open the file in read/write mode and lock it
      // flock() should block until it gets a lock
      if ((!$handle = fopen($filename, 'r+')) || !flock($handle, LOCK_EX)) exit(1);
    
      // Read the file
      if (($contents = fread($handle, filesize($filename)) === FALSE) exit(1);
    
      // Will be set to true if we actually make any changes to the file
      $changed = FALSE;
    
      // Loop hosts list
      foreach ($hosts as $host) {
    
        // Get current IP address of host
        if (($ip = gethostbyname($host)) == $host) continue;
    
        // Find the entry in the file
        $replaceWith = "#SO{$host}\nsshd : {$ip}\n#EO{$host}";
        if (preg_match("/#SO{$host}(.*?)#EO{$host}/si", $contents, $regs)) {
          // Only do this if there was a match - otherise risk overwriting previous
          // entries because you didn't reset the value of $result
          if ($regs[0] != $replaceWith) {
            $changed = TRUE;
            $contents = str_replace($regs[0], $replaceWith, $contents);
          }
        }
    
      }
    
      // We'll only change the contents of the file if the data changed
      if ($changed) {
        ftruncate($handle, 0); // Zero the length of the file
        rewind($handle); // start writing from the beginning
        fwrite($handle, $contents); // write the new data
      }
    
      flock($handle, LOCK_UN); // Unlock
      fclose($handle); // close
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that makes use of a package (PKG_MY_PACKAGE). I will change
I have a Perl script I wrote for my own personal use that fetches
I have a script like that genhash --use-ssl -s $IP -p 443 --url $URL
I have a Ruby script in my Rails app that I use to load
I have a script that uses $(document).ready , but it doesn't use anything else
I have a script that checks the class (integer) of a , runs a
In SSIS 2008 I have a Script Task that checks if a table exists
I have bash script that I use regularly in my job to automate a
So I have a script that does multiple checks for 32, 48 and 72
Bit of a problem here, basically I have a web address that I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.