OVH Cloud OVH Cloud

gethostbyaddr ...

1 réponse
Avatar
pounch
Bonjour,

Quelle belle fonction que gethostbyaddr() qui permet de retrouver le nom
d'un pc à partir d'une IP.

Voici mon code:
<?php

$start = (isset($start)) ? $start : 1;

define("SUBNET", "192.168.0.", TRUE);
define("RANGE_MIN", $start, TRUE);
define("RANGE_MAX", $start+19, TRUE);

echo 'Your subnet: '.SUBNET.'XXX<br/>';
echo 'Start IP: <b>'.SUBNET.RANGE_MIN.'</b><br/>';
echo 'End IP : <b>'.SUBNET.RANGE_MAX.'</b><hr/>';

$host = '';

$time_start = microtime(true);
for ($i=RANGE_MIN; $i<=RANGE_MAX; $i++) {
$host = 'Host [<i>'.(SUBNET.$i).'</i>]: ';

$h = gethostbyaddr(SUBNET.$i);
if ($h != (SUBNET.$i)) {
if (eregi("^pc[0-9]", $h)) {
$a = explode('.', $h);
$host .= '<font color="#0000FF">'.strtoupper($a[0]).'</font>';
}
else {
// computer name
$host .= 'unknown (<font color="#FF0000">'.$h.'</font>)';
}
}
else {
$host .= '<font color="#FF0000">'.(SUBNET.$i).' ERROR!</font>';
}
}
$time_end = microtime(true);

echo '<hr/>';
echo 'Started at '.$time_start.'<br/>';
echo 'Stopped at '.$time_end.'<br/>';
echo 'Script execution time: '.($time_end - $time_start).'
seconds<br/><br/>';

echo '<a href="'.$PHP_SELF.'?start='.(RANGE_MIN - 20).'">Previous 20</a>
--- <a href="'.$PHP_SELF.'?start='.(RANGE_MIN + 20).'">Next 20</a>';

?>

Ca fonctionne! Cependant, ça coince sur certaines IP et le script se bloque.
Les IP qui sont connectées, il n'y a pas de probleme. Mais celle qui ne
sont visiblement pas connectées, le script s'arrete et ne se termine pas
(logique).

Comment eviter que le script s'arrete? J'aimerais obtenir uen reponse
sure pour chaque IP. Comment faire?

Merci,
Dams

1 réponse

Avatar
jawad.xos
Tu pourrais peut-être pinger la machine avant d'effectuer ton
gethostbyaddr.
Si la machine répond au ping, alors tu récupére son nom, sinon tu
continues et tu passes à la machine suivante (concluant que la machine
est éteinte).

Pas de fonction native en PHP pour exécuter un ping, jette un coup
d'oeil par là, tu devrais trouver ton bonheur:
http://www.hotscripts.com/PHP/Scripts_and_Programs/Networking_Tools/Ping/