Forum Programmation.php phpupdate

Posté par  .
Étiquettes : aucune
0
16
mar.
2007
Bonjour, j'essaye de greffer un script à ma page d'index apache de manière à rafraîchir l'IP de ma neuf box quand celle ci a une adresse IP WAN qui change.
Le script c'est celui là:
<?PHP
#version 0.02b 07/04/2003
# get-ip2 was originally written for a Dynalink RTA300W but the router died, It was then replaced
# and the wireless part of the router was faulty so I gave up and reconfigured it for use with an
# Alcatel Speedtouch Home that was upgraded to a Pro.
# It should work on most ADSL Routers providing there is a page that lists the remote IP on its own.
# Further modification may be made to this script however if you make any modifications please send
# me a copy ( send any updates or changes to updateip@borg.co.nz )
# the script can be setup as a cron job and called via wget or lynx. Its up to you how
# often you want it to refresh but I'm intending to have it refresh every minute.

# This scipt is copywrite (C) of Andrew Hooper 2003 updateip@borg.co.nz.

$stat_file = 'remote_ip'; # Name of the status file (apache must have write permissions in the directory)
$log_file = 'dynamic_ip.log'; # log the history to this file

$DSL_Username = 'admin'; # ADSL Router Username
$DSL_Password = 'XXXXX'; # ADSL Router Password
$DSL_Address = 'http://192.168.1.1/wanstatus.html'; #URL on the router that displays the remote IP
$IP_Update_URL[] = 'http://freedns.afraid.org/dynamic/update.php?QUERY_STRING'; # URL to open for updating the IP
$IP_Update_URL[] = 'http://freedns.afraid.org/dynamic/update.php?QUERY_STRING'; # URL to open for updating the IP
$IP_Update_URL[] = 'http://freedns.afraid.org/dynamic/update.php?QUERY_STRING'; # URL to open for updating the IP

$debug = 'ON'; #Debuging ON / OFF

##################################################################
## You should not need to edit beyond this point ##
##################################################################

if(ereg('(http:\/\/)(.*)',$DSL_Address,$DSL_regs)){

if(!$DSL_Username && !$DSL_Password){
$DSL_url = $DSL_regs[1] . $DSL_regs[2]; # If there is no Username and Password
}else{
$DSL_url = $DSL_regs[1] . $DSL_Username . ":" . $DSL_Password . "@" . $DSL_regs[2]; # if there is a Username and Password
}

$DSL_Status = implode('',file($DSL_url));
$DSL_Status = strip_tags($DSL_Status, '');

$ipok = 0;

if(ereg('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',$DSL_Status,$IP_regs)){
for($i=1; $i < 5; $i++){
$ip_segment[$i] = intval($IP_regs[$i]);

if($ip_segment[$i] <= 255) $ipok++;
}
$ip = implode(".", $ip_segment);
}
}

$lastIP_Status = implode('',file($stat_file));

if(ereg('([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})',$lastIP_Status,$lastIP_regs)){

if($debug='ON'){echo "Last IP was : " . $lastIP_regs[0] . "\n\r";}

$last_IP = $lastIP_regs[0];
}

if($ip == $last_IP){
if($debug='ON'){echo "No change to IP : " . $ip ."\n\r";}
}else{
# Create or Alter the status file.
if($debug='ON'){echo "Changing IP from : " . $last_IP . " to : " . $ip . "\n\r";}
$fname = $stat_file;
$fp = fopen($fname,'w+');
$log_ip = $ip . " Date: " . date("F j, Y, g:i a") . "\n\r";
fwrite($fp,$log_ip);
fclose($fp);

$fname = $log_file;
$fp = fopen($fname,'a');

while(list($key, $val) = each($IP_Update_URL)){
$dynamic_update = implode('',file($val));
# Create and or write to the log file.
$log_ip = "Date: " . date("F j, Y, g:i a") . " - Response :" . $dynamic_update . "\n\r";
if($debug='ON'){echo $dynamic_update . "\n\r";}
}
fwrite($fp,$log_ip);
fclose($fp);

}


?>

Mais bon rien ne s'affiche sur ma page .html par défaut et je n'y connais pas grand chose en php.
J'essaye néanmoins d'exécuter le script phpupdate.php à la main avec la ligne de commande:

php phpupdate.php et j'obtiens : Warning: file(remote_ip): failed to open stream: No such file or directory in /var/www/apache2-default/phpupdate.php on line 55

Warning: implode(): Bad arguments. in /var/www/apache2-default/phpupdate.php on line 55
No change to IP :
  • # Que veux tu faire ???

    Posté par  (site web personnel) . Évalué à 3.

    Si c'est obtenir ton adresse ip publique, te fait pas ch**r, ouvre un accès libre chez free.fr puis ouvre toi un compte page perso dessus.

    Connecte toi ensuite sur ftpperso.free.fr avec ton identifiant et mot de passe et pose un fichier ip.php dessus avec ce contenu :
    <?php
    echo $_SERVER['REMOTE_ADDR'];
    ?>

    Puis pour avoir ton ip publique il te suffira ensuite de faire un lynx --dump http://rapsys.free.fr/ip.php
    (remplace rapsys par ton compte)

    Bon j'ai mis ça en place pour moi donc tu peux l'utiliser je pense :
    http://rapsys.free.fr/ip.php
    (évite de faire des téléchargement avec un délais entre deux tentatives de moins d'une demi heure...)

    Ensuite si tu veux pouvoir contacter ta machine en permanence depuis l'extérieur, il te faut plusieurs choses :
    - mettre 192.168.1.2 en dmz par exemple
    - fixer l'ip locale de ton serveur sous linux a 192.168.1.2
    - ouvrir un compte dyndns ou un dynhost chez ovh par exemple

    Pour mettre a jour ton dyndns tu peux utiliser le script suivant :
    #! /usr/bin/php
    <?php
    //Define the domain
    define('DOMAIN', 'members.dyndns.org');

    //Define the path
    define('PATH', '/nic/update');

    //Update file
    define('FILE', '/tmp/.ipupdate.dyndns');

    //Check if lock file is present
    if (is_file(FILE.'.lock'))
    die('Last time: '.file_get_contents(FILE.'.lock'));

    //Define key and identification
    define('USERINFO', base64_encode('login:password'));
    define('USERAGENT', 'rapsys_php_ipupdate/0.3rc4 rapsys@free.fr');
    define('DATAINFO', '?system=dyndns&hostname=example.dyndns.org&wildcard=ON');

    //Check file function
    function checkfile()
    {
    //Check if file exist
    if(is_file(FILE))
    {
    //Return the file content
    return file_get_contents(FILE);
    }
    else
    //Return localhost to force update
    return '127.0.0.1';
    }

    //Checkip function
    function checkip()
    {
    //The socks
    $sock = @fsockopen('checkip.dyndns.org', 80, $errno, $errstr);

    //Exit if socket is in bad state
    if (!$sock) die("Update impossible: $errstr ($errno)\n");

    fputs($sock, "GET / HTTP/1.1\r\n");
    fputs($sock, "Host: checkip.dyndns.org\r\n");
    fputs($sock, "User-Agent: ".USERAGENT."\r\n");
    fputs($sock, "\r\n");

    //Get the header
    $headers = "";
    while ($str = trim(fgets($sock, 4096)))
    $headers .= "$str\n";

    //Get the body (ip)
    $remote = "";
    while (!feof($sock))
    $remote .= fgets($sock, 4096);

    //Close the socket
    fclose($sock);

    //Store result for investigate
    $fd = fopen(FILE.'.ip', 'w');
    fwrite($fd, $headers.$remote);
    fclose($fd);

    //Return the answer
    return preg_replace('/.*Current IP Address: ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/s', '\1', $remote);
    }

    if (checkip() != checkfile())
    {
    //Open a socket
    $sock = @fsockopen('ssl://'.DOMAIN, 443, $errno, $errstr, 30);

    //Exit if socket is in bad state
    if (!$sock) die("Update impossible dyndns: $errstr ($errno)\n");

    //Forge the request
    fputs($sock, "GET ".PATH.DATAINFO." HTTP/1.1\r\n");
    fputs($sock, "Host: ".DOMAIN."\r\n");
    fputs($sock, "Authorization: Basic ".USERINFO."\r\n");
    fputs($sock, "User-Agent: ".USERAGENT."\r\n");
    fputs($sock, "\r\n");

    //Get the header
    $headers = "";
    while ($str = trim(fgets($sock, 4096)))
    $headers .= "$str\n";

    //Get the body (ip)
    $remote = "";
    while (!feof($sock))
    $remote .= fgets($sock, 4096);

    //Close the socket
    fclose($sock);

    //Store result for invertigate
    $fd = fopen(FILE.'.update', 'w');
    fwrite($fd, $headers.$remote);
    fclose($fd);

    //Get the return code
    $retcode = preg_replace('/(?:.*\n)([a-z]+).*/s', '\1', $remote);

    //Touch lock file if something goes wrong
    if ($retcode != 'good' && $retcode != 'nochg')
    {
    $fd = fopen(FILE.'.lock', 'w');
    date_default_timezone_set('UTC');
    fwrite($fd, date("l d F Y G:i:s").': Update impossible dyndns: '.$remote);
    fclose($fd);
    echo date("l d F Y G:i:s").': Update impossible dyndns: '.$remote;
    }

    //Update FILE
    $fd = fopen(FILE, 'w');
    fwrite($fd, preg_replace('/.*(?:good|nochg) ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/s', '\1', $remote));
    fclose($fd);
    }
    ?>

    Ensuite enregistre le sous le mode 755 sous le nom /usr/bin/ipupdate

    Puis ajoute ça dans /etc/cron.d/dyndns :
    #!/bin/bash
    # /etc/cron.d/dyndns: crontab fragment for dyndns
    # This update dyndnds ip.

    # Look for and update dyndns ip every 30 minutes
    08,38 * * * * login [ -x /usr/bin/php ] && [ -x /usr/bin/ipupdate ] && /usr/bin/ipupdate

    Ps : pense a remplace login par ton nom d'user (unix pour le cron.d) et dyndns pour le script ipupdate
    pense a remplacer example.dyndns.org et password avec ton nom de domaine dyndns et ton password.

    Le script de mise a jour de dyndns n'utilise pas l'ip récupérée de chez mon compte free, mais de chez dyndns.

    Il se peux que en cas d'erreur un fichier /tmp/ipupdate.dyndns.lock soit créé, tu a intérêt a regarder pourquoi il est créé avant de l'effacer pour relancer les mises a jour.

Suivre le flux des commentaires

Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.