Getting Network Client MAC address, Syntax issue using PhP

柔情痞子 提交于 2019-12-04 14:25:17

问题


Actually the below coding is working fine, if I provide the ip address directly inside the 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,

$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().


回答1:


shell_exec('arp '.$ip.' | awk \'{print $4}\'');


Result from Terminal

└── arp 10.1.10.26 | awk '{print $4}'

a4:5e:60:ee:29:19




回答2:


This is the correct format:

$mac=shell_exec("arp -a ".$ip);

or

$mac=shell_exec("arp -a ".escapeshellarg($ip));

(using the escapeshellarg call)




回答3:


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);



回答4:


A space is missing just after the -a in 'arp -a'.escape...

So it turns into arp -a192.168.0.107




回答5:


shell_exec("arp -a ".escapeshellarg($_SERVER['REMOTE_ADDR'])." | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'");


来源:https://stackoverflow.com/questions/24929212/getting-network-client-mac-address-syntax-issue-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!