从客户端的计算机获取MAC地址(Get MAC address from client's machine)

编程入门 行业动态 更新时间:2024-10-27 08:30:51
客户端的计算机获取MAC地址(Get MAC address from client's machine)

我是新手,我做了一些搜索,但大多数答案都有相同的结果:MAC地址输出显示为“Found”。

我的代码如下:

$ip = $_SERVER['REMOTE_ADDR']; $mac=shell_exec("arp -a ".$ip); $mac_string = shell_exec("arp -a $ip"); $mac_array = explode(" ",$mac_string); $mac = $mac_array[3]; if(empty($mac)) { die("No mac address for $ip not found"); } echo($ip." - ".$mac);

I'm new to this, and I did some searching, but most of the answers have the same results: the MAC address output is shown as "Found."

My code is below:

$ip = $_SERVER['REMOTE_ADDR']; $mac=shell_exec("arp -a ".$ip); $mac_string = shell_exec("arp -a $ip"); $mac_array = explode(" ",$mac_string); $mac = $mac_array[3]; if(empty($mac)) { die("No mac address for $ip not found"); } echo($ip." - ".$mac);

最满意答案

啊,旧的exec() vs shell_exec() vs passthru()问题。

要查看实际运行的命令以及系统实际返回的内容,请使用exec() ,并分别将int和array作为第2和第3个参数传递给它,然后在运行命令后将var_dump()传递给它们。

例如:

$cmd = "arp -a " . $ip; $status = 0; $return = []; exec($cmd, $return, $status); var_dump($status, $return); die;

如果一切正常,则$status应为零, $return可能为空,也可能为空。 但是,如果$status 不为零,那么请注意$return的值是什么,因为这将是系统在尝试运行命令时告诉您正在发生的事情。

Protip:将exec()传递给arp as-in的完整路径

#> which arp /usr/sbin/arp $cmd = "/usr/sbin/arp -a" . $ip;

另外,请记住,根据命令的运行位置, REMOTE_ADDR可能不会返回任何有用的内容。 还有其他几种获取IP地址的方法,如果您需要的IP地址落后于某种代理,这些方法尤其有用。

Ah, the old exec() vs shell_exec() vs passthru() question.

To see what command is actually being run, and what the system is actually returning, use exec(), and pass it an int and an array as its 2nd and 3rd params respectively, then var_dump() them both after running the command.

For example:

$cmd = "arp -a " . $ip; $status = 0; $return = []; exec($cmd, $return, $status); var_dump($status, $return); die;

If everything went OK, then $status should be zero and $return may or may not be empty. However if $status is non-zero then pay attention to what the value of $return is, as this will be what your system is telling you is happening when it tries to run your command.

Protip: Pass exec() the full path to arp as-in:

#> which arp /usr/sbin/arp $cmd = "/usr/sbin/arp -a" . $ip;

Also, bear in mind, depending on where the command is being run, REMOTE_ADDR may not return anything useful. There are several other ways of obtaining an IP address, which are especially useful if the IP address you need is behind some sort of proxy.

更多推荐

本文发布于:2023-08-05 05:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   地址   计算机   machine   MAC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!