1

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

Kevin
  • 41,694
  • 12
  • 53
  • 70
Vijay
  • 93
  • 1
  • 2
  • 8

5 Answers5

2

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


Result from Terminal

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

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

code-8
  • 54,650
  • 106
  • 352
  • 604
1

This is the correct format:

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

or

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

(using the escapeshellarg call)

Rick77
  • 3,121
  • 25
  • 43
Krumple
  • 36
  • 5
0

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

So it turns into arp -a192.168.0.107

syl.fabre
  • 696
  • 1
  • 7
  • 20
  • No I have removed and adding the space and tried several times, but it is not working. – Vijay Jul 24 '14 at 08:58
  • update your answer accordingly because in your code, a space is missing ! – syl.fabre Jul 24 '14 at 08:59
  • have you tried to do var_dump(escapeshellarg($ip)); ? – syl.fabre Jul 24 '14 at 08:59
  • if i use var_dump, It is listing all the ip and their mac addresses. – Vijay Jul 24 '14 at 09:02
  • I meant, use var_dump(escapeshellarg($ip)) to be sure that 'arp -a' . escapeshellarg($ip) == 'arp -a 192.168.0.107' – syl.fabre Jul 24 '14 at 09:05
  • actually i alter the coding like this ** $mac = shell_exec('arp -a' . var_dump(escapeshellarg($ip)));** it will list all the IP and Mac address in my networks – Vijay Jul 24 '14 at 09:08
  • use the var_dump outside the call to shell_exec. – syl.fabre Jul 24 '14 at 09:10
  • the purpose is to verify that 'arp -a' . escapeshellarg($ip) equals to 'arp -a 192.168.0.107' as you're claiming that $mac = shell_exec('arp -a 192.168.0.107'); works but $mac = shell_exec('arp -a' . escapeshellarg($ip)); doesn't – syl.fabre Jul 24 '14 at 09:11
  • $ip = var_dump(escapeshellarg($ip)); $mac = shell_exec('arp -a' . escapeshellarg($ip)); no use... $ip = var_dump(escapeshellarg($ip)); $mac = shell_exec('arp -a' . $ip); no use... till all the ip are listed – Vijay Jul 24 '14 at 09:14
  • so how can i call the variable $ip inside shell_exec()? – Vijay Jul 24 '14 at 09:19
  • do you know var_dump function ? – syl.fabre Jul 24 '14 at 09:20
  • Actually i stored the client system ip address in $ip " $ip = $_SERVER['REMOTE_ADDR'];" – Vijay Jul 24 '14 at 09:20
  • Actually i am new to php... i didn't know deeply. – Vijay Jul 24 '14 at 09:21
  • ok var_dump is like a debug function to display the content of a variable. It's not supposed to be used like $my_var = var_dump($something). You can use it to check that 'arp -a' . escapeshellarg($ip) is equals to 'arp -a 192.168.0.107' by doing on another line of code var_dump('arp -a' . escapeshellarg($ip)); to see if you're really executing 'arp -a 192.168.0.107' as you think you are – syl.fabre Jul 24 '14 at 09:25
  • Actually my need is to get the client ip and mac address who use my php program and stored in my database with time, employee name and employee no. I will now able to store client ip using $_SERVER['REMOTE_ADDR']; in DB. However I can't get the MAC address of the client system/person whom using my programs. – Vijay Jul 24 '14 at 09:38
  • So the getting ip value are related to MAc address. If i give the ip address in the position "escapeshellarg($ip)". I can getting my answer... If I use "escapeshellarg($ip)" the answer is not generating. It is not possible to assign IP as $mac = shell_exec('arp -a 192.168.0.107'); since it is a variable. – Vijay Jul 24 '14 at 09:43
0

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);
roycruse
  • 1
  • 2
-1
shell_exec("arp -a ".escapeshellarg($_SERVER['REMOTE_ADDR'])." | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'");
HiDeoo
  • 10,353
  • 8
  • 47
  • 47
Vinay A
  • 11
  • 2