使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:33:27  来源:igfitidea点击:

Getting Network Client MAC address, Syntax issue using PhP

phpnetworkingsyntaxmac-addressshell-exec

提问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 escapeshellargcall)

(使用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}'");