Connecting Tech Pros Worldwide Forums | Help | Site Map

getting mac address of client machines

pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#1: Sep 10 '09
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function returnmacaddress() {
  3. // This code is under the GNU Public Licence
  4. // Written by michael_stankiewicz {don't spam} at yahoo {no spam} dot com
  5. // Tested only on linux, please report bugs
  6.  
  7. // WARNING: the commands 'which' and 'arp' should be executable
  8. // by the apache user; on most linux boxes the default configuration
  9. // should work fine
  10.  
  11. // get the arp executable path
  12. $location = 'which arp';
  13. $location = rtrim($location);
  14. // Execute the arp command and store the output in $arpTable
  15. $arpTable = '$location -n';
  16. // Split the output so every line is an entry of the $arpSplitted array
  17. $arpSplitted = split("\n",$arpTable);
  18. // get the remote ip address (the ip address of the client, the browser)
  19. $remoteIp = $_SERVER['REMOTE_ADDR'];
  20. $remoteIp = str_replace(".", "\\.", $remoteIp);
  21. // Cicle the array to find the match with the remote ip address
  22. foreach ($arpSplitted as $value) {
  23. // Split every arp line, this is done in case the format of the arp
  24. // command output is a bit different than expected
  25. $valueSplitted = split(" ",$value);
  26. foreach ($valueSplitted as $spLine) {
  27. if (preg_match("/$remoteIp/",$spLine)) {
  28. $ipFound = true;
  29. }
  30. // The ip address has been found, now rescan all the string
  31. // to get the mac address
  32. if ($ipFound) {
  33. // Rescan all the string, in case the mac address, in the string
  34. // returned by arp, comes before the ip address
  35. // (you know, Murphy's laws)
  36. reset($valueSplitted);
  37. foreach ($valueSplitted as $spLine) {
  38. if (preg_match("/[0-9a-f][0-9a-f][:-]".
  39. "[0-9a-f][0-9a-f][:-]".
  40. "[0-9a-f][0-9a-f][:-]".
  41. "[0-9a-f][0-9a-f][:-]".
  42. "[0-9a-f][0-9a-f][:-]".
  43. "[0-9a-f][0-9a-f]/i",$spLine)) {
  44. return $spLine;
  45. }
  46. }
  47. }
  48. $ipFound = false;
  49. }
  50. }
  51. return false;
  52. }
  53. echo returnmacaddress();
  54. ?>
I found this code by googling! But its not printing anything!! :(

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#2: Sep 10 '09

re: getting mac address of client machines


then it’s probably returning false, which does not have a print representation (check with var_dump()).
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#3: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by Dormilich View Post

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()!!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#4: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by pradeepjain View Post

!i have never used var_dump()!!

shame on you, that’s probably the #1 debugging function in PHP. see ref.
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#5: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by Dormilich View Post

shame on you, that’s probably the #1 debugging function in PHP. see ref.

okie!!i will learn now!!thanks for the comments !!
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#6: Sep 10 '09

re: getting mac address of client machines


I tried to print the $location value!!! it had nothin in it!!!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#7: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by pradeepjain View Post

I tried to print the $location value

where did you try to print it?
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#8: Sep 10 '09

re: getting mac address of client machines


after this line

Expand|Select|Wrap|Line Numbers
  1. $location = rtrim($location);
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#9: Sep 10 '09

re: getting mac address of client machines


may I see the code?

my output:
Expand|Select|Wrap|Line Numbers
  1. // formatted through xdebug
  2. string 'which arp' (length=9)
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#10: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by Dormilich View Post

may I see the code?

my output:

Expand|Select|Wrap|Line Numbers
  1. // formatted through xdebug
  2. 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!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#11: Sep 10 '09

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.
Expand|Select|Wrap|Line Numbers
  1. $mac = `ping $ip && arp -a | grep $ip`;
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#12: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by Dormilich View Post

hasn’t there been a description?

besides the code usage looks odd.

this is what I found somewhere else and looks way more sensible.

Expand|Select|Wrap|Line Numbers
  1. $mac = `ping $ip && arp -a | grep $ip`;

okie thanks for help!will give it a try!
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#13: Sep 10 '09

re: getting mac address of client machines


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $ip = '192.168.2.213';
  3.  
  4.   $mac = `ping $ip && arp -a | grep $ip`;
  5. echo $mac;
  6. ?>
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!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#14: Sep 10 '09

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
Expand|Select|Wrap|Line Numbers
  1. man arp
hit enter and read the description.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#15: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by pradeepjain View Post

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.
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#16: Sep 10 '09

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!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#17: Sep 10 '09

re: getting mac address of client machines


do you get the same result if you try
Expand|Select|Wrap|Line Numbers
  1. $mac = `arp -a | grep $ip`;
?
pradeepjain's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: India
Posts: 407
#18: Sep 10 '09

re: getting mac address of client machines


Quote:

Originally Posted by Dormilich View Post

do you get the same result if you try

Expand|Select|Wrap|Line Numbers
  1. $mac = `arp -a | grep $ip`;
?

yeah!!!permission error!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,664
#19: Sep 10 '09

re: getting mac address of client machines


then you can’t do anything about it (although the concept is right)
Reply