colinireland Posted January 18, 2013 Share Posted January 18, 2013 Hello, I am trying to detemine the MAC address of a machine on a local network from the IP address. The following code does exactly that. <?php $IP="192.168.1.18"; exec("ping -c 1 -s 1 $IP"); $mac=exec("/usr/sbin/arp -an | /bin/grep $IP | /usr/bin/awk '{print $4}'"); echo "$IP - $mac"; ?> However I need to take the IP address from an HTML form. When using the $_SERVER['REMOTE_ADDR'] variable seems to screw things up for me. Maybe its not the correct type of variable? For example the following will not work. <?php $IP=$_SERVER['REMOTE_ADDR']; exec("ping -c 1 -s 1 $IP"); $mac=exec("/usr/sbin/arp -an | /bin/grep $IP | /usr/bin/awk '{print $4}'"); echo "$IP - $mac"; ?> Can anyone explain why this is? Our suggest an alternative method of doing this? Regards, Colin Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/ Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 Echo out the variable, preferably using var_dump (), and see if it is what you expect it to be. Also, do take note of the fact that the IP is provided by the client. Which means that you have to validate it before using it, as an attacker could quite simply spoof it. Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/#findComment-1406688 Share on other sites More sharing options...
requinix Posted January 18, 2013 Share Posted January 18, 2013 But rest assured that the REMOTE_ADDR will be a valid IP address. There's just no guarantee that it's actually the IP address of the user - could be a proxy, could be spoofed. Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/#findComment-1406768 Share on other sites More sharing options...
colinireland Posted January 18, 2013 Author Share Posted January 18, 2013 Gave that a shot with... <?php #$IP="192.168.1.18"; $IP=$_SERVER['REMOTE_ADDR']; var_dump($IP); ?><br /><?php exec("ping -c 1 -s 1 $IP"); $ans=exec("/usr/sbin/arp -an | /bin/grep $IP | /usr/bin/awk '{print $4}'"); echo "$IP - $ans"; ?> and got string(12) "192.168.1.30" 192.168.1.30 - Still no idea why I can't pass it to exec correctly. I'm just trying to get the basic mechanism of getting the mac address in place. Will validate any input at a later stage though. Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/#findComment-1406832 Share on other sites More sharing options...
Christian F. Posted January 18, 2013 Share Posted January 18, 2013 I reckon $4 is a parameter you want to send to awk, not a PHP variable. If that is the case, then you'll need to escape the dollar sign to ensure it isn't parsed by the PHP engine. Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/#findComment-1406861 Share on other sites More sharing options...
jazzman1 Posted January 19, 2013 Share Posted January 19, 2013 I think, the execution path to the arp table is wrong. Does ip address 192.168.1.18 belong to the local machine? There is nothing wrong about $_SERVER['REMOTE_ADDR']. Try to change /usr/sbin/arp -an to /sbin/arp -an Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/#findComment-1406867 Share on other sites More sharing options...
requinix Posted January 19, 2013 Share Posted January 19, 2013 Along with that goes the general question of "Have you tried running the exact same command from a terminal?" Quote Link to comment https://forums.phpfreaks.com/topic/273319-_serverremote_addr-help/#findComment-1406876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.