Dynamic IP and cron job

Discussion in 'Server Operation' started by satimis, Dec 13, 2006.

  1. satimis

    satimis Member

    Hi folks,

    Ubuntu-6.06.1-LAMP-server-amd64

    I have a script.php which is used to update the IP address of my domain on registrar's website. I keep it on /etc/. The IP address of the server only changes on connection. Please advise how to create a "cron job" which will run the script after booting to update the IP address.

    Is /etc/ the right directory for holding the script?

    TIA

    B.R.
    satimis
     
  2. sjau

    sjau Local Meanie Moderator

    Have a look at this (I think this also goes for Ubuntu)

    http://www.debian-administration.org/articles/28

    1) Basically you create the script in /etc/init.d/ip_update
    2) Make it executable
    3) Symlink it to the according runlevels (on that website the K means as much as Kill and S means as much as Start... the number is the sequence of starting/killing in that runlevel)
     
  3. satimis

    satimis Member

    Hi sjau,

    Tks for your advice and URL.

    The script.php was download from the site where I registered the domain. I don't think they allow third party program. There is a short instruction on the file download instructing running the script.php on cron.

    B.R.
    satimis
     
  4. sjau

    sjau Local Meanie Moderator

    Well, the content of your boot-up script could be:

    Code:
    #!/bin/sh
    lynx /path/to/php-script.php
    
    And then you would symlink it only in the starting processes R2-5 I think...
    Just make sure, you also have lynx installed...
     
  5. satimis

    satimis Member

    Hi sjau,

    Content of the script;
    <?
    //* Package: changeip.com Auto IP updater
    //* Version 1.1
    //* System Requirements: Linux, (PHP 3, PHP 4 >= 4.0.0)
    //* and the ability to run crontabs
    //* File: update.php

    -snip-

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////BRIEF OVERVIEW PLEASE READ //////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //*
    //*
    //* This script will update your dynamic ip address by the use of a cron job. There are two ways this script can
    //* be used.
    //*
    //* 1. No Router, This is if your PC Running Linux is connected directly to your ISP. Using this setup option will
    //* allow you to set the cron job to run every few minutes. Only when your IP changes an update will be sent.
    //*
    //* NOTE: IF YOUR CRON JOB IS SET TO EVERY FEW MINUTES SET YOUR EMAIL OPTIONS TO "0" Or you will fill the log file
    //* along with your email box.
    //*
    //* 2. Router, - snip -
    //*
    //* NOTE: If you are on a CABLE MODEM or DSL your IP
    -snip-
    //* IF YOUR CRON JOB IS SET TO EVERY FEW MINUTES SET YOUR EMAIL OPTIONS TO "0" Or you will fill the log file
    //* along with your email box.
    //*
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////CHECK YOUR CHANGEIP.COM SETTINGS ////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //* 1. Make Changes to your domains so that ALL Options update on SET 1. The first column of check boxes.
    //*
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////// SETUP INSTRUCTIONS ///////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //*
    //* 1. Edit the variables below in this file to match your server and your needs.
    //* 2. Upload update.php and updatedns.log to your root web.
    //* 3. chmod updatedns.log to 666
    //* 3. Create a cron job to execute update.php. See the following example.
    //*
    //* Telnet into your server create a new cronjob. The following would
    //* run every night at 12:59pm. Depending on your needs you will have
    //* to modify the run time variables.
    //*
    //* 59 23 * * * wget -q http://www.yoursite.com/update.php
    //*
    //* minute (0-59)
    //* hour (0-23)
    //* day of the month (1-31)
    //* month of the year (1-12)
    //* day of the week (0-6 with 0=Sunday)
    //*
    //* NOTE: A cron can also be setup using WEBMIN if you have it installed. This is easyier for most.
    //*
    //*
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////// SETUP OPTION DESCRIPTIONS DO NOT EDIT THESE HERE //////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //* $username = "changeip.com login username";
    //* $password = "changeip.com login password";
    //*
    //* $Adminemail = "[email protected]";
    //* $Sendmail = "/path/to/sendmail -t -i";
    //*
    //* $Logfile = "/full/server/path/to/updatedns.log";
    //*
    //*
    ////////////////////////////////////////// IF USING A ROUTER ////////////////////////////////////////////////
    -snip-
    ////////////////////////////////////// NO ROUTER, DIRECT CONNECTION /////////////////////////////////////////
    //*
    //*
    //*
    //* $Log1 = "0"; No log file output
    //* $Log1 = "1"; Appends a new line to the log file ONLY when a update is sent to changeip.com
    //* $Log2 = "0"; No log file output
    //* $Log2 = "1"; Appends a new line to the log file even when there is no update sent to changeip.com
    //* $Email1 = "0"; No Email Will be sent
    //* $Email1 = "1"; Sends an email to the address specified. when a update is sent to changeip.com
    //* $Email2 = "0"; No Email Will be sent
    //* $Email2 = "1"; Sends an email to the address specified. when a update is sent to changeip.com
    //*
    //* $IPfile = "/full/server/path/to/updatednsIP.log"; <--- This is not an option if using direct connect
    //* $Logfile = "/full/server/path/to/updatedns.log";
    //*
    //*
    //*
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////<-------- EDIT USER VARIABLES HERE --------> ////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    $Bypass = "0";

    $username = "username";
    $password = "password";

    $Adminemail = "[email protected]";
    $Sendmail = "/path/to/sendmail -t -i";

    $IPlogfile = "/full/server/path/to/updatednsIP.log";
    $Logfile = "/full/server/path/to/updatedns.log";

    $Log1 = "1";
    $Email1 = 1;

    $Log2 = "1";
    $Email2 = 1;

    $Log3 = "1";
    $Email3 = "1";

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////<--------- END USER VARIABLES --------> /////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING//////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

    $UPDATEURL = "https://www.changeip.com/dynamic/dns/update.asp?u=$username&p=$password&cmd=update&set=1&offline=0";


    if ($REMOTE_ADDR == "") $Boxaddress = "Server address Unknown";
    else $Boxaddress = $REMOTE_ADDR;

    $tdate = date("h:i:s");
    $datecode = date("m-d-Y");

    if($Bypass == "0"){


    if (file_exists($IPlogfile)){

    $file=file("$IPlogfile","r");
    foreach($file as $line){
    list($Lastip,$blank) = split("\:",chop($line));
    if($Lastip != $REMOTE_ADDR){


    header("location: $UPDATEURL");

    $createFile = fopen("$IPlogfile", "w");
    fwrite($createFile,"$REMOTE_ADDR:\r\n");
    fclose($createFile);

    if($Log1 == "1"){
    $filelog = fopen("$Logfile", "a");
    flock($filelog, 2);
    fputs($filelog,"DNS Update set 1, Current IP: $REMOTE_ADDR, Previous IP: $Lastip, Date: $datecode, Time: $tdate. Update Successful\r\n");
    flock($filelog, 3);
    fclose($filelog);
    }

    if($Email1=="1"){

    $to = "$Adminemail";
    $subject = "IP Auto update Report";
    $body ="Hello, $username\r\n\r\nYour IP address has changed and an update request was sent to changeip.com.\r\n\r\n Current IP: $REMOTE_ADDR\r\n Previous IP: $Lastip\r\n Time: $tdate\r\n Date: $datecode\r\n\r\nChangeip.com IP Auto Updater by [email protected]";

    $mailer = popen($Sendmail,"w") or die ("error writing");
    fwrite ($mailer,"Subject: $subject\nFrom: \"Changeip-AutoUpdater\" $Adminemail\nTo: $to\n\n$body");
    pclose($mailer);
    }

    }
    }
    }


    else
    {
    header("location: $UPDATEURL");

    $createFile = fopen("$IPlogfile", "w");
    fwrite($createFile,"$REMOTE_ADDR:\r\n");
    fclose($createFile);

    if($Log1 == "1"){
    $filelog = fopen("$Logfile", "a");
    flock($filelog, 2);
    fputs($filelog,"DNS Update set 1, Current IP: $REMOTE_ADDR, Previous IP: Unknown, Date: $datecode, Time: $tdate. Update Successful\r\n");
    flock($filelog, 3);
    fclose($filelog);
    }

    if($Email1=="1"){

    $to = "$Adminemail";
    $subject = "IP Auto update Report";
    $body ="Hello, $username\r\n\r\nYour IP address has changed and an update request was sent to changeip.com.\r\n\r\n Current IP: $REMOTE_ADDR\r\n Previous IP: Either this is the first time you run this script or you deleted the updatednsip.log. This is normal\r\n Time: $tdate\r\n Date: $datecode\r\n\r\nChangeip.com IP Auto Updater by [email protected]";

    $mailer = popen($Sendmail,"w") or die ("error writing");
    fwrite ($mailer,"Subject: $subject\nFrom: \"Changeip-AutoUpdater\" $Adminemail\nTo: $to\n\n$body");
    pclose($mailer);
    }
    }



    if($Lastip == $REMOTE_ADDR){

    if($Log2 == "1"){
    $filelog = fopen("$Logfile", "a");
    flock($filelog, 2);
    fputs($filelog,"DNS Update set 1, Current IP: $REMOTE_ADDR, Previous IP: $REMOTE_ADDR, Date: $datecode, Time: $tdate. No Update Required\r\n");
    flock($filelog, 3);
    fclose($filelog);
    }

    if($Email2=="1"){

    $to = "$Adminemail";
    $subject = "IP Auto update Report";
    $body ="Hello, $username\r\n\r\nYour IP address did not change since the last update.\r\nThere was no need to send a update request to changeip.com\r\n\r\n Current IP: $REMOTE_ADDR\r\n Previous IP: $Lastip\r\n Time: $tdate\r\n Date: $datecode\r\n\r\nChangeip.com IP Auto Updater by [email protected]";

    $mailer = popen($Sendmail,"w") or die ("error writing");
    fwrite ($mailer,"Subject: $subject\nFrom: \"Changeip-AutoUpdater\" $Adminemail\nTo: $to\n\n$body");
    pclose($mailer);
    }
    }
    }

    if($Bypass == "1"){

    header("location: $UPDATEURL");

    if($Log3 == "1"){
    $filelog = fopen("$Logfile", "a");
    flock($filelog, 2);
    fputs($filelog,"DNS Update set 1, Current IP: $REMOTE_ADDR, Date: $datecode, Time: $tdate. Update Successful\r\n");
    flock($filelog, 3);
    fclose($filelog);
    }

    if($Email3=="1"){

    $to = "$Adminemail";
    $subject = "IP Auto update Report";
    $body ="Hello, $username\r\n\r\nYour IP has been updated,\r\n\r\n Current IP: $REMOTE_ADDR\r\n Time: $tdate\r\n Date: $datecode\r\n\r\nChangeip.com IP Auto Updater by [email protected]";

    $mailer = popen($Sendmail,"w") or die ("error writing");
    fwrite ($mailer,"Subject: $subject\nFrom: \"Changeip-AutoUpdater\" $Adminemail\nTo: $to\n\n$body");
    pclose($mailer);
    }
    }

    ?>s
     
  6. sjau

    sjau Local Meanie Moderator

    Just get lynx (a text based browser) and then do a call of your php script through lynx... alternatively you can setup a cron where you invoke the php binary with the php script but right now I have no clue how to do that :)
     
  7. satimis

    satimis Member

    Hi sjau,

    I have lynx running on this server. But I'm not experienced on text browser.

    $ sudo lynx /usr/local/bin/update.php
    started a blank page with following line on screen:-
    Code:
    application/x-httpd-php  D)ownload, or C)ancel
    
    Pressed D (download)
    Code:
    Download Options (Lynx Version 2.8.5rel.1), help
    
    Downloaded link: file://localhost/usr/local/bin/update.php
    Suggested file name: update.php
    
    Standard download options:
       Save to disk
    
    Local additions:
    
    So I have to quit

    re-run
    $ sudo lynx /usr/local/bin/update.php
    same screen displayed. Pressed "C" (cancel)
    Code:
    $ sudo lynx /usr/local/bin/update.php
    
    Can't Access `file://localhost/usr/local/bin/update.php'
    Alert!: Unable to access document.
    
    lynx: Can't access startfile
    

    Having previously performed following test;

    $ apt-get install sendmail
    After installation completed "sendmail" became MTA and "postfix" did not work anymore. It is completely out of my control.

    My intention is just installing "sendmail" and retaining "postfix" as MTA. Because following line needs "sendmail"

    Edited /usr/local/bin/update.php
    Code:
    .......
    .....
    ////////////////////////////////<-------- EDIT USER VARIABLES HERE --------> //////////////////////$///////////////////////////////////////////////////////////////////////////////////////////////////$
    $Bypass = "0";
    
    $username = "satimis";
    $password = "xxxxxxx";
    
    $Adminemail = "[email protected]";
    $Sendmail = "/usr/sbin/sendmail -t -i";
    
    $IPlogfile = "/var/log/updatedns/updatednsIP.log";
    $Logfile = "/var/log/updatedns/updatedns.log";
    
    $Log1 = "1";
    $Email1 = 1;
    
    $Log2 = "1";
    $Email2 = 1;
    
    $Log3 = "1";
    $Email3 = "1";
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////$////////////////////////////////<--------- END USER VARIABLES --------> ///////////////////////////$
    ...
    .....
    
    Remark:
    Code:
     -t     Read  message  for recipients.  To:, Cc:, and Bcc: lines will be
                  scanned for recipient addresses.  The Bcc: line will be  deleted
                  before transmission.
    
    
    -i     Ignore dots alone on lines by themselves in  incoming  messages.
                  This should be set if you are reading data from a file.
    
    On console/terminal
    $ sudo php /usr/local/bin/update.php
    Code:
    
    Warning: file() expects parameter 2 to be long, string given in /usr/local/bin/update.php on line 183
    
    Warning: Invalid argument supplied for foreach() in /usr/local/bin/update.php on line 184
    
    $ cat /usr/local/bin/update.php
    Code:
    .....
    ..........
            if($Bypass == "0"){
    
    
                    if (file_exists($IPlogfile)){
    
                            $file=file("$IPlogfile","r");		(line 183)
                            foreach($file as $line){			(line 184)
                            list($Lastip,$blank) = split("\:",chop($line));
                                    if($Lastip != $REMOTE_ADDR){
    
    
                            header("location: $UPDATEURL");
    
                            $createFile = fopen("$IPlogfile", "w");
                            fwrite($createFile,"$REMOTE_ADDR:\r\n");
                            fclose($createFile);
    
                    if($Log1 == "1"){
    ....
    ......
    
    Yes, even if the script runs without problem I have to run cron in combination. Because the connection is on 24 hrs basis, I suppose.


    If there is no solution I'll come back to satimis.homelinux.com" of dyndns.org/com making it to work. This server is for test not for production.

    "satimis.freeddns.com" is working seamlessly apart the IP address has to be updated manually on connection. "Ping it" and "satimis.freeddns.com:8080 browsing homepage" both work without problem.


    P.S. Is there any way to make "postfix" as MTA again instead of running;
    $ sudo apt-get remove sendmail

    TIA


    B.R.
    satimis
     
  8. sjau

    sjau Local Meanie Moderator

    ok, I assume that your PHP file is running fine.

    Put it into your a webfolder and call it then through your browser. Check that it's loaded fine, no errors and stuff...

    Once you have done that, you can do use Lynx:

    Code:
    lynx http://localhost/path/to/file.php
    
    Lynx will then behave the same as your browser and call it... and if the script is running fine, then it will do what it's supposed to :)
     
  9. edge

    edge Active Member Moderator

    You can also execute PHP directly on your server (so no need for lynx)

    Lets say you want to run script.php than all you need to type is php script.php You will need to be in the directory where script.php is located.

    I guess you can also run php /location/of/script/script.php
     
    Last edited: Dec 14, 2006
  10. satimis

    satimis Member

    Hi edge,

    Yes, your are correct. I did it before. Pls see my posting on #7

    On console/terminal
    $ sudo php /usr/local/bin/update.php

    It ran without problem but the IP address on changeip.com can't be updated.
    Also I don't understand how to fix the problem on the script re;

    $ sudo php /usr/local/bin/update.php
    Password:
    Code:
    Warning: file() expects parameter 2 to be long, string given in /usr/local/bin/update.php on line 183
    
    Warning: Invalid argument supplied for foreach() in /usr/local/bin/update.php on line 184
    
    Lines 183 and 184
    # cat /usr/local/bin/update.php
    Code:
    .....
    .....
                    if (file_exists($IPlogfile)){
    
                            $file=file("$IPlogfile","r");                                (line 183)
                            foreach($file as $line){                                  (line 184)
                            list($Lastip,$blank) = split("\:",chop($line));   
                                    if($Lastip != $REMOTE_ADDR){
    
    
                            header("location: $UPDATEURL");
    
                            $createFile = fopen("$IPlogfile", "w");
                            fwrite($createFile,"$REMOTE_ADDR:\r\n");
                            fclose($createFile);
    ....
    .....
    
    Besides postfix is now not working. sendmail takes its place as MTA. I need to restore postfix without clue. Any suggestion?

    B.R.
    satimis
     
  11. satimis

    satimis Member

    Hi sjau]ok,

    It is executable. After putting it on webfolder I can re-run;
    $ sudo chmod + /var/www/update.php

    I suppose you refer to /var/www ?

    I think the critical part is here:-
    $ cat /usr/local/bin/update.php
    Code:
    ....
    ....
    ///////////////////////////////////////////////////////////////////////////////$////////////////////////////////<-------- EDIT USER VARIABLES HERE --------> //$///////////////////////////////////////////////////////////////////////////////$
    $Bypass = "0";
    
    $username = "satimis";
    $password = "xxxx";
    
    $Adminemail = "[email protected]";
    $Sendmail = "/usr/sbin/sendmail -t -i";
    #$Postfix = "/etc/init.d/postfix restart";
    
    $IPlogfile = "/var/log/updatedns/updatednsIP.log";
    $Logfile = "/var/log/updatedns/updatedns.log";
    
    $Log1 = "1";
    $Email1 = 1;
    
    $Log2 = "1";
    $Email2 = 1;
    
    $Log3 = "1";
    $Email3 = "1";
    
    ///////////////////////////////////////////////////////////////////////////////$////////////////////////////////<--------- END USER VARIABLES --------> ///////$
    ....
    
    I have no idea whether the entries are correct!!!!!

    Others noted with tks.


    B.R.
    satimis
     
  12. tycho

    tycho New Member

    Hi satamis,

    I don't have a good solution for your problem, but I can hand you a good workaround.
    Have a look at this:

    http://forum.sitelutions.com/viewtopic.php?t=2433&sid=12557cdc1567cba672388c79b01d9df8

    Of course you would have to move your domain to this registrar (that is: registration in DNS), which I did some years ago and I'm totally satisfied. Even their support is great, wonder how they do it for such a low price. They also provide a very good and affordable MX backup service, which you will probably need.

    I've installed the script (slightly modified) as a cron job, works perfectly!

    (Oh, and NO, I don't have shares. Just try it, you'll love it)
     
    Last edited: Dec 19, 2006

Share This Page