<?php
function wakeOnLan($ip,$port,$mac)
{
    
$macregex '/^([a-fA-F0-9]{2}-){5}([a-fA-F0-9]{2})$/';
    
    
$hu=ip2long($ip);
    if(!
$hu){
        
$ip=gethostbyname($ip);
        
$hu=ip2long($ip);
        if(!
$hu)    return 'adresse invalide';
    }
    if(!
is_numeric($port)||$port>65535||$port<0)    return 'port invalide';
    if(!
preg_match($macregex,$mac))    return 'adresse mac invalide';
    
    
$mac=str_replace('-','',$mac);
    
$mac=pack('H*',$mac);
    
$ffs=str_pad('',6,"\xff");
    
$magic=str_pad($ffs,17*6,$mac);
    
    
$s=fsockopen('udp://'.$ip,$port,$errno,$errstr);
    if(!
$s) return;
    
fwrite($s,$magic);
    return 
'packet magique envoyé à '.$ip.':'.$port;
}

if(isset(
$_POST['ip']))
    echo 
wakeOnLan($_POST['ip'],$_POST['port'],$_POST['mac']);

?>
<form method="post">
Adresse MAC de la machine a reveiller :
<input type="text" name="mac" value="<?=$_POST['mac']?>"/><br/>
Adresse où envoyer le packet magique :
<input type="text" name="ip" value="<?=$_POST['ip']?>"/><br/>
Port :
<input type="text" name="port" value="<?=$_POST['port']?>"/><br/>
<input type="submit" value="ok"/>
</form>

page
Dernière modification : Sun, 18 Jun 2006 17:19:49