使用 PhP 获取网络客户端 MAC 地址、语法问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24929212/
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
Getting Network Client MAC address, Syntax issue using PhP
提问by Vijay
Actually the below coding is working fine, if I provide the ip address directly inside the shell_exec()
实际上下面的编码工作正常,如果我直接在里面提供IP地址 shell_exec()
$mac = shell_exec('arp -a 192.168.0.107');
If, I get the ip of the client from his system and stored in a variable and call the same, as given below,
如果,我从他的系统中获取客户端的 ip 并存储在一个变量中并调用它,如下所示,
$mac = shell_exec('arp -a' . escapeshellarg($ip));
The output is not generating.
输出没有生成。
Here is the Full code:
这是完整的代码:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$mac = shell_exec('arp -a'. escapeshellarg($ip));
//Working fine when sample client IP is provided...
//$mac = shell_exec('arp -a 192.168.0.107');
$findme = "Physical";
$pos = strpos($mac, $findme);
$macp = substr($mac,($pos+42),26);
if(empty($mac))
{
die("No mac address for $ip not found");
}
// having it
echo "mac address for $ip: $macp";
?>
Please advise, why escapeshellarg($ip)
does not work in the shell_exec()
.
请指教,为什么escapeshellarg($ip)
在shell_exec()
.
回答by cyber8200
shell_exec('arp '.$ip.' | awk \'{print $4}\'');
shell_exec('arp '.$ip.' | awk \'{print $4}\'');
Result from Terminal
结果来自终端
└── arp 10.1.10.26 | awk '{print $4}'
└── arp 10.1.10.26 | awk '{print $4}'
a4:5e:60:ee:29:19
a4:5e:60:ee:29:19
回答by Krumple
This is the correct format:
这是正确的格式:
$mac=shell_exec("arp -a ".$ip);
or
或者
$mac=shell_exec("arp -a ".escapeshellarg($ip));
(using the escapeshellarg
call)
(使用escapeshellarg
调用)
回答by roycruse
This is working for me...
这对我有用...
$ip=$_SERVER['REMOTE_ADDR'];
$mac_string = shell_exec("arp -a $ip");
$mac_array = explode(" ",$mac_string);
$mac = $mac_array[3];
echo($ip." - ".$mac);
回答by syl.fabre
A space is missing just after the -a in 'arp -a'.escape...
'arp -a'.escape 中的 -a 之后缺少一个空格...
So it turns into arp -a192.168.0.107
所以它变成了 arp -a192.168.0.107
回答by Vinay A
shell_exec("arp -a ".escapeshellarg($_SERVER['REMOTE_ADDR'])." | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'");