Connecting Tech Pros Worldwide Forums | Help | Site Map

Fetching Host name and MAC address

Member
 
Join Date: Apr 2008
Location: United states
Posts: 123
#1: Jun 26 '08
Hey all,
I am trying to make one script which I can run on different computers and give me the hostname as well as the MAC address.

Can you please help me how to do so ??script in Perl or PHP will work...
Thanks..

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Jun 26 '08

re: Fetching Host name and MAC address


Heya, AJD.

The hostname is available in your $_SERVER array.

You can get the MAC address on a Unix system via the `ifconfig` command (i.e., system('ifconfig');). Be prepared to do some parsing, though.
Member
 
Join Date: Apr 2008
Location: United states
Posts: 123
#3: Jun 27 '08

re: Fetching Host name and MAC address


Quote:

Originally Posted by pbmods

Heya, AJD.

The hostname is available in your $_SERVER array.

You can get the MAC address on a Unix system via the `ifconfig` command (i.e., system('ifconfig');). Be prepared to do some parsing, though.


Hey
thanks pbmod, But I am looking for some PHP script , as i need IP address, hostname and MAC address also ,
The script i used is ,
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "IP Address Is : ";
  3. echo $_SERVER['HTTP_X_FORWARDED_FOR']; // Show IP
  4.  
  5. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  6.  
  7. echo " Host Name : ";
  8. echo gethostbyaddr($ip);
  9. $mac = returnMacAddress();
  10. function returnMacAddress() {
  11.  
  12. // Get the arp executable path
  13. $location = `which arp`;
  14. // Execute the arp command and store the output in $arpTable
  15. $arpTable = `$location`;
  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.  
  20. $remoteIp = $ip;
  21. foreach ($arpSplitted as $value) {
  22. // Split every arp line, this is done in case the format of the arp
  23. // command output is a bit different than expected
  24. $valueSplitted = split(" ",$value);
  25. foreach ($valueSplitted as $spLine) {
  26. if (preg_match("/$remoteIp/",$spLine)) {
  27. $ipFound = true;
  28. }
  29. // The ip address has been found, now rescan all the string
  30. // to get the mac address
  31. if ($ipFound) {
  32. // Rescan all the string, in case the mac address, in the string
  33. // returned by arp, comes before the ip address
  34. // (you know, Murphy's laws)
  35. reset($valueSplitted);
  36. foreach ($valueSplitted as $spLine) {
  37. if (preg_match("/[0-9a-f][0-9a-f][:-]".
  38. "[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]/i",$spLine)) {
  43. return $spLine;
  44. }
  45. }
  46. }
  47. $ipFound = false;
  48. }
  49. }
  50. return false;
  51. }
  52.  
  53.  
It gives IP address and hostname correct but , does not show MAC address..
Reply


Similar PHP bytes