-1

I currently have a raspberry pi running Arch Linux acting as a wifi access point. There is a web server running on the pi with a LAN web app (PHP).

I want my web app to know the MAC address of a user making a request. Am I able to get the MAC address of the connecting user, send it to lighttpd and pass it as an environment variable?

The web server will know the LAN IP of the requesting user. Could this be used to get the MAC address from the system?

Thanks

Prash
  • 101
  • 5
  • I don't think there is an easy way to do this - if you're not on the same network you'll have no idea what the MAC is if you're using IPv4. On the same network it will be in your ARP table but the HTTP server shouldn't know what it is. – TheFiddlerWins Dec 13 '16 at 14:57
  • @TheFiddlerWins The web server is going to be running on the pi that the users will be connecting to. All on the same network. – Prash Dec 13 '16 at 15:10

1 Answers1

1

This can be done easily. First way I will show grabs a list of all IP/MAC pairs in case you need that. It then parses the list for the currently connected client's MAC. The second way, just quickly grabs the IP for the currently connected client.

First way:

Run this at the command line to check it gives you a list of IPs=MAC addresses:

arp -en | grep ether | sed -e "s/ \+ether \+/=/g" | grep -ioE "[0-9]{1,3}+(\.[0-9]{1,3}){3}=[0-9a-f]{2}(\:[0-9a-f]{2}){5}"`

Once you know it works, you can incorporate it into your code. First you get the client ip address.

$client_ip=$_SERVER['REMOTE_ADDR'];

Then get a list of MAC addresses:

$arp_output=`arp -en | grep ether | sed -e "s/ \+ether \+/=/g" | grep -ioE "[0-9]{1,3}+(\.[0-9]{1,3}){3}=[0-9a-f]{2}(\:[0-9a-f]{2}){5}"`;

Then parse them to find the right one:

$mac_addresses=explode("\n", $arp_output);

foreach($mac_addresses as $mac_address)
{
  $values=explode('=', trim($mac_address));
  if ($values[0]==$client_ip)
   {
     $client_mac_address=$values[1];
   }
}

Second way

Put $client_ip into the command line to get the MAC address directly:

$client_ip=escapeshellarg($_SERVER['REMOTE_ADDR']);

$client_mac_address=`arp -en | grep {$client_ip} | grep -ioE "[0-9a-f]{2}(\:[0-9a-f]{2}){5}" | head -c-1`
bao7uo
  • 1,704
  • 12
  • 24
  • Should work @Prash I tested the command line stuff on my Raspberry Pi 3.. also running Arch ;-) But I don't have php on there, so I tested that bit on a Kali vm. – bao7uo Dec 13 '16 at 18:43