getting mac address of client machines  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| |
Hii,
I need to know is there any way to get the mac id's of the machines accessing ma site! I want to make a access lock using the mac id's -
<?php
-
function returnmacaddress() {
-
// This code is under the GNU Public Licence
-
// Written by michael_stankiewicz {don't spam} at yahoo {no spam} dot com
-
// Tested only on linux, please report bugs
-
-
// WARNING: the commands 'which' and 'arp' should be executable
-
// by the apache user; on most linux boxes the default configuration
-
// should work fine
-
-
// get the arp executable path
-
$location = 'which arp';
-
$location = rtrim($location);
-
// Execute the arp command and store the output in $arpTable
-
$arpTable = '$location -n';
-
// Split the output so every line is an entry of the $arpSplitted array
-
$arpSplitted = split("\n",$arpTable);
-
// get the remote ip address (the ip address of the client, the browser)
-
$remoteIp = $_SERVER['REMOTE_ADDR'];
-
$remoteIp = str_replace(".", "\\.", $remoteIp);
-
// Cicle the array to find the match with the remote ip address
-
foreach ($arpSplitted as $value) {
-
// Split every arp line, this is done in case the format of the arp
-
// command output is a bit different than expected
-
$valueSplitted = split(" ",$value);
-
foreach ($valueSplitted as $spLine) {
-
if (preg_match("/$remoteIp/",$spLine)) {
-
$ipFound = true;
-
}
-
// The ip address has been found, now rescan all the string
-
// to get the mac address
-
if ($ipFound) {
-
// Rescan all the string, in case the mac address, in the string
-
// returned by arp, comes before the ip address
-
// (you know, Murphy's laws)
-
reset($valueSplitted);
-
foreach ($valueSplitted as $spLine) {
-
if (preg_match("/[0-9a-f][0-9a-f][:-]".
-
"[0-9a-f][0-9a-f][:-]".
-
"[0-9a-f][0-9a-f][:-]".
-
"[0-9a-f][0-9a-f][:-]".
-
"[0-9a-f][0-9a-f][:-]".
-
"[0-9a-f][0-9a-f]/i",$spLine)) {
-
return $spLine;
-
}
-
}
-
}
-
$ipFound = false;
-
}
-
}
-
return false;
-
}
-
echo returnmacaddress();
-
?>
I found this code by googling! But its not printing anything!! :(
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines
then it’s probably returning false, which does not have a print representation (check with var_dump()).
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines Quote:
Originally Posted by Dormilich then it’s probably returning false, which does not have a print representation (check with var_dump()). I did not get u!! :( !i have never used var_dump()!!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines Quote:
Originally Posted by pradeepjain !i have never used var_dump()!! shame on you, that’s probably the #1 debugging function in PHP. see ref. |  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines Quote:
Originally Posted by Dormilich shame on you, that’s probably the #1 debugging function in PHP. see ref. okie!!i will learn now!!thanks for the comments !!
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines
I tried to print the $location value!!! it had nothin in it!!!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines Quote:
Originally Posted by pradeepjain I tried to print the $location value where did you try to print it?
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines
after this line - $location = rtrim($location);
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines
may I see the code?
my output: - // formatted through xdebug
-
string 'which arp' (length=9)
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines Quote:
Originally Posted by Dormilich may I see the code?
my output: - // formatted through xdebug
-
string 'which arp' (length=9)
sorry for the mistake i am also getting 'which arp' ...
i dono y he he is using 'which arp ' here! i am not getting the logic in that code! so i am facing problem!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines
hasn’t there been a description?
besides the code usage looks odd.
this is what I found somewhere else and looks way more sensible. - $mac = `ping $ip && arp -a | grep $ip`;
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines Quote:
Originally Posted by Dormilich hasn’t there been a description?
besides the code usage looks odd.
this is what I found somewhere else and looks way more sensible. - $mac = `ping $ip && arp -a | grep $ip`;
okie thanks for help!will give it a try!
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines - <?php
-
$ip = '192.168.2.213';
-
-
$mac = `ping $ip && arp -a | grep $ip`;
-
echo $mac;
-
?>
i wrote this code !!its printing nothing! i am getting an error in error log like
ping: icmp open socket: Permission denied when i run this code!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines
to know what arp does go to a posix machine (linux, unix, bsd, mac), open a terminal (on linux you can also use ALT+F2), type
hit enter and read the description.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines Quote:
Originally Posted by pradeepjain i wrote this code !!its printing nothing! i am getting an error in error log like
ping: icmp open socket: Permission denied when i run this code! that’s always the problem with shell commands, you need permission.
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines
so we cannot generally find the MAC id's of a machine connected to net as one of the problems will be permission!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines
do you get the same result if you try - $mac = `arp -a | grep $ip`;
?
|  | Needs Regular Fix | | Join Date: Jul 2007 Location: India
Posts: 407
| | | re: getting mac address of client machines Quote:
Originally Posted by Dormilich do you get the same result if you try - $mac = `arp -a | grep $ip`;
? yeah!!!permission error!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,664
| | | re: getting mac address of client machines
then you can’t do anything about it (although the concept is right)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,537 network members.
|