An-Najah National University

Razi Alsayyed Blog

This is a blog about my experience with programming, networking and systems

  • Bookmark and Share Email
  • Sunday, January 18, 2009
  • PHP script to find a PC location in a network based on its MAC address
  • Following is a PHP script I wrote to find a PC location in a farm of CISCO switches based on its MAC address, the script connects to a central switch, trys to find where the PC is connected "on which port", if it does not find it, then the script connects to the next switch based on CDP protocol. NOTE: Some regular expressions may be not accurate in all circumstances, take it as is at your risk, or feel free to change it.
    function login($telnet,$username,$password) {
       $response = readBuffer($telnet);
    if($username != null) fputs($telnet,"{$username}\r\n"); fputs($telnet,"{$password}\r\n"); $response .= readBuffer($telnet); // if the switch in user mode // then go to exec mode if(preg_match("/[a-z0-9_\(\)\-]+>$/i",$response) ) { $response .= sendCommand($telnet,"en"); $response .= sendCommand($telnet,$password); } preg_match("/[a-z0-9_\(\)\-]+[#>]$/i",$response,$out); return str_replace("#","",$out[0]); // return hostname } function sendCommand($telnet,$c) { fputs($telnet,"{$c}\r\n"); return readBuffer($telnet); } function readBuffer($telnet) { $str = ""; while(true) { $result = fgets($telnet,2); if(ord($result) != 8) //backspace $str .= $result; if( substr($str,-8) == "--More--" ) { $str = preg_replace("/[\-]{2}More[\-]{2}/i","", $str); fputs($telnet," "); } if( ( $result == "#" || $result == ">" || $result == ":" || ) && ( preg_match("/[a-z0-9_\(\)\-]+[#>]$/i",$str) preg_match("/Password:\s$/i",$str) ) ) break; } return $str; } // the following function are executed through // regular expression to replace short interface // names with full names // you can add other short names here function fullname($shortname) { switch($shortname) { case "fa": return "FastEthernet"; case "gi": return "GigabitEthernet"; } return $shortname; } function findMAC($switch,$mac) { $telnet = fsockopen($switch,"23",$errno,$errstr,60*30); $hostname = login($telnet,null,"PASSWORD"); // execute show mac-address-table on the switch $result = sendCommand($telnet, "sh mac-address-table | include ".$mac); // get the interface where the PC // connects to (it may be the next switch // based on cdp output below) preg_match("/\s([A-Z]+([0-9]+\/)+[0-9]+)\s/i",$result,$out); $interface = $out[1]; // if the interface is in short name format // convert it to full name $interface = preg_replace("/^([a-z]{2})([0-9])/ie", "fullname(strtolower('\\1')).'\2'", $interface); // execute show cdp neighbors detail on the switch $result = sendCommand($telnet,"sh cdp ne de"); // get all neighbors $deviceid = "\sDevice\sID:\s+?([a-z0-9_\(\)\-]+)"; $ipaddress = "IP\saddress:\s+?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"; $port = "Interface:\s+?([A-Z]+([0-9]+\/)+[0-9]+)"; $neighbors_count = preg_match_all("/{$deviceid}(.\s)*?". "{$ipaddress}(.\s)*?". "{$port}/i", $result, $out); // if the interface in the cdp neighbors output // then the PC is not connected to this switch // repeat the process on the neighbor switch for($i=0;$i<$neighbors_count;$i++) { if(strcasecmp ($out[5][$i],$interface) == 0) { echo "Device ID: ".$hostname."<br />"; echo "IP Address: ".$switch."<br />"; echo "Interface: ".$interface."<br />"; echo "Next Device: ".$out[1][$i]."<hr />"; findMAC($out[3][$i],$mac); return; } } // if we reach here then we find the interface // print it out echo "Device ID: ".$hostname."<br />"; echo "IP Address: ".$switch."<br />"; echo "Interface: ".$interface."<hr />"; } // how to use $mainswitch = ""; $macaddress = "xxxx.xxxx.xxxx"; findMAC($mainswitch,$macaddress); ?>
  • Bookmark and Share Email
  • Ben Johnson said...
  • I applaud your effort here, but the syntax looks a bit wrong. I've run the script and found that it didn't work as expected (or at all). Would you be prepared to send the script as a text file me at [email protected] (substitute fname for ben and sname for johnson, and leave the "."). Cheers, Ben.
  • Wednesday, January 28, 2009
  • Ben Johnson said...
  • Thanks for attaching the script, Razi. I haven't got it working in our environment yet. May be because of AAA, although you're passing a username and password. I'll do some more troubleshooting and let you know if I find anything useful. Cheers, Ben.
  • Friday, January 30, 2009
  • Erina said...
  • Ben did you get this script to work with through AAA. I am writing a script which will log into a switch, sh cdp, grab all details on any SEP (cisco ip phones) devices, and sav it to a csv.. I\'m going to try this using some of the basics you have demonstrated in your script... let me know if you can have me with any ideas! cheers, Erina
  • Wednesday, May 19, 2010
Leave a Comment



Razi F. Alsayyed
Computer Engineer
I am a 26 years old computer enginner, graduated from An-Najah National University in 2005, and working in it since that.
Show Full Profile


Please do not email me if you do not know me
Please do not e-mail me if you do not know me