php 局域网唤醒脚本有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6055293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wake on lan script that works
提问by Michael
is there a wake on lan script using a web language preferably php that works? Also one that has some documentation on how to get it to work like what needs to be enabled on your server etc
是否有使用网络语言(最好是 php)的 lan 脚本唤醒?还有一个有一些关于如何让它工作的文档,比如需要在你的服务器上启用什么等
回答by Mez
function wol($broadcast, $mac)
{
$hwaddr = pack('H*', preg_replace('/[^0-9a-fA-F]/', '', $mac));
// Create Magic Packet
$packet = sprintf(
'%s%s',
str_repeat(chr(255), 6),
str_repeat($hwaddr, 16)
);
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($sock !== false) {
$options = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, true);
if ($options !== false) {
socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7);
socket_close($sock);
}
}
}
Should work - call it with a broadcast IP address, and a MAC address
应该工作 - 使用广播 IP 地址和 MAC 地址调用它
回答by Ryan Steffer
I know this is an old questions, but it's still the first Google result, so here's what I ended up doing after a bit of research:
我知道这是一个老问题,但它仍然是第一个谷歌结果,所以这是我经过一些研究后最终做的:
Prerequisites:
先决条件:
- Linux box on the same network
- Install the
wakeonlan
package from your system's package manager
(i.e.sudo apt-get install wakeonlan
)
- 同一网络上的 Linux 机器
wakeonlan
从系统的包管理器安装包
(即sudo apt-get install wakeonlan
)
Now the script is as easy as this:
现在脚本就像这样简单:
<?php
# replace with your target MAC address
$mac = 'aa:bb:cc:11:22:33';
exec("wakeonlan $mac");
?>
Hope that helps someone.
希望能帮助某人。
回答by Ken H
HTML (test.htm)
HTML (test.htm)
<body>
<a href="test.php?mymac=XX:XX:XX:XX:XX:XX">Click to WOL XX:XX:XX:XX:XX:XX</a>
</body>
PHP (test.php)
PHP (test.php)
<?php
$mymac = $_REQUEST['mymac'];
wol("255.255.255.255", $mymac);
echo 'WOL sent to '.$mymac;
function wol($broadcast, $mac){
$mac_array = preg_split('#:#', $mac); //print_r($mac_array);
$hwaddr = '';
foreach($mac_array AS $octet){
$hwaddr .= chr(hexdec($octet));
}
//Magic Packet
$packet = '';
for ($i = 1; $i <= 6; $i++){
$packet .= chr(255);
}
for ($i = 1; $i <= 16; $i++){
$packet .= $hwaddr;
}
//set up socket
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($sock){
$options = socket_set_option($sock, 1, 6, true);
if ($options >=0){
$e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7);
socket_close($sock);
}
}
} //end function wol
?>
Since the split()
function was removed from PHP 7.0.0, this script uses preg_split()
to be compatible with current and previous PHP versions.
由于该split()
函数已从 PHP 7.0.0 中删除,因此该脚本用于preg_split()
与当前和以前的 PHP 版本兼容。
Replace XX:XX:XX:XX:XX:XX
in the HTML with your target MAC to test the script.
XX:XX:XX:XX:XX:XX
用您的目标 MAC替换HTML 以测试脚本。