Fetching Host name and MAC address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    Fetching Host name and MAC address

    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
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    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('ifconfi g');). Be prepared to do some parsing, though.

    Comment

    • ajd335
      New Member
      • Apr 2008
      • 123

      #3
      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('ifconfi g');). 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 ,
      Code:
      <?php
      echo "IP Address Is : ";
      echo $_SERVER['HTTP_X_FORWARDED_FOR']; // Show IP
      
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
      
      echo " Host Name : ";
      echo gethostbyaddr($ip);
      $mac = returnMacAddress();
      function returnMacAddress() {
      
      // Get the arp executable path
      $location = `which arp`;
      // Execute the arp command and store the output in $arpTable
      $arpTable = `$location`;
      // 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 = $ip;
      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;
      }
      It gives IP address and hostname correct but , does not show MAC address..

      Comment

      Working...