Only grab unique MAC addresses

Along with IP address's and PC names

Page 1 of 1

6 Replies - 1706 Views - Last Post: 09 August 2010 - 07:58 PM Rate Topic: -----

#1 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Only grab unique MAC addresses

Posted 08 August 2010 - 05:28 PM

We have setup a custom DHCP server for only allowing authorized mac address's. Reads a log file (that is attached, but renamed to .php instead of .log in order to uploadAttached File  dhcpd.php (29.6K)
Number of downloads: 79).

Ive got it to read all the mac address's, ip address's and PC name's. Now this log file WILL have (lots) of duplicates. What i cant get it to do, is filter out only unique address's.
The working copy that just reads the data:
<table width="200" border="1">
  <tr>
    <th scope="col">IP</th>
    <th scope="col">MAC</th>
    <th scope="col">Name</th>
  </tr>
<?php
//The source
$source="dhcpd.log";
//Open the file for use
$filez=fopen($source,"r",1) or exit("Unable to open file!");
//Read the entire contents to a file
$content=fread($filez,filesize($source));
//Close file
fclose($filez);
//Split the string with explode
$thearray = explode("\n",$content);
//The amount of content via line count
$count = count($thearray);
$i=0;
for ($i=0;($i<$count);$i++) 
{
	//print $thearray[$i];
	if (strstr($thearray[$i], "DHCPREQUEST"))
	{
		$sect = explode("from ", $thearray[$i]);
		$sect[0]=strrchr($sect[0],58);
		$sect[0]=str_replace(": DHCPREQUEST for ","",$sect[0]);
		$sect= explode(" (",$sect[0]);
		$ip=$sect[0];
		
		$sect = explode("from ", $thearray[$i]);		
		$secta = explode(" (", $sect[1]);
		$pcname=$secta[1];
		$pcname= str_replace(") via eth0","",$pcname); //del
		
		$sect = explode("from ", $thearray[$i]);
		$sect = explode(" (", $sect[1]);
		$macad = $sect[0];
		//$sect = explode(" from ", $sect[0]);
		
		//$sect[0]=str_replace(":","",$sect[0]);
		//$sect[0]=strrchr($sect[0],58);
		//$sect=strrchr();
		print "<tr>";
		print "<th>" . $ip . "</th><th>" . $macad . "</th><th>" . $pcname . "</th>";
		print "</tr>";

	}
}

?>
</table>


My attempt to filter out the data (but failed):
<table width="200" border="1">
  <tr>
    <th scope="col">IP</th>
    <th scope="col">MAC</th>
    <th scope="col">Name</th>
  </tr>
<?php
//The source
$source="dhcpd.log";
//Open the file for use
$filez=fopen($source,"r",1) or exit("Unable to open file!");
//Read the entire contents to a file
$content=fread($filez,filesize($source));
//Close file
fclose($filez);
//Split the string with explode
$thearray = explode("\n",$content);
//The amount of content via line count
$count = count($thearray);
$i=0;
$counter=0;
for ($i=0;($i<$count);$i++) 
{
	//print $thearray[$i];
	if (strstr($thearray[$i], "DHCPREQUEST"))
	{

		$counter++;
		
		//Mac Address
		$sect = explode("from ", $thearray[$i]);
		$sect = explode(" (", $sect[1]);
		$macad = $sect[0];
		//$macarray = array_push($macad);
		$macarray = array();
		$macarray[$counter]=$mac;
		array_unique($macarray);
		

		//IP Address
		$sect = explode("from ", $thearray[$i]);
		$sect[0]=strrchr($sect[0],58);
		$sect[0]=str_replace(": DHCPREQUEST for ","",$sect[0]);
		$sect= explode(" (",$sect[0]);
		$ip=$sect[0];
		//$iparray = array_push($ip);
		$iparray=array();
		$iparray[$counter]=$ip;
		array_unique($iparray);
		
		
		//PC Name
		$sect = explode("from ", $thearray[$i]);		
		$secta = explode(" (", $sect[1]);
		$pcname=$secta[1];
		$pcname= str_replace(") via eth0","",$pcname); //del
		$pcarray = array();
		//$pcarray = array_push($pcname);
		$pcarray[$counter] = $pcname;
		array_unique($pcarray);
		
		
	}
	
}
$index=0;
		while ($macarray > $counter)
		{
			
			print "<tr>";
			print "<th>" . $iparray[$index] . "</th><th>" . $macarray[$index] . "</th><th>" . $pcarray[$index] . "</th>";
			print "</tr>";
			$index++;
		}

?>
</table>



So can anyone give me a hand and point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Only grab unique MAC addresses

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Only grab unique MAC addresses

Posted 08 August 2010 - 05:48 PM

Hard to say without seeing what the file looks like, but I'd use the MAC addresses as the keys for an array:
<?php
$macs = file_get_contents("MACS.txt");
$mac_add = explode("\n",$macs);
print_r($mac_add);
echo "<br><br><br>";
foreach($mac_add as $key)
{
	if(isset($mac_list[$key]))
	{
		$mac_list[$key]++;
	} elseif($key != '')
	{
		$mac_list[$key] = 1;
	}
}
print_r($mac_list);
?>

Was This Post Helpful? 1
  • +
  • -

#3 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Only grab unique MAC addresses

Posted 09 August 2010 - 04:50 AM

View PostCTphpnwb, on 09 August 2010 - 09:48 AM, said:

Hard to say without seeing what the file looks like, but I'd use the MAC addresses as the keys for an array:
<?php
$macs = file_get_contents("MACS.txt");
$mac_add = explode("\n",$macs);
print_r($mac_add);
echo "<br><br><br>";
foreach($mac_add as $key)
{
	if(isset($mac_list[$key]))
	{
		$mac_list[$key]++;
	} elseif($key != '')
	{
		$mac_list[$key] = 1;
	}
}
print_r($mac_list);
?>

That works great, except print_r doesnt have good output (or control). I need to be able to go array[$countervar] and go from 0 to final output. With the method you provided i get 1 and 15 as the index (for two mac address's), which isnt that controllable. SO is there any way to reallocate everything into 0,1,2 (etc) index array

This post has been edited by mattisdada: 09 August 2010 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Only grab unique MAC addresses

Posted 09 August 2010 - 07:19 AM

print_r is just to show the array contents, which are stored in an associative array where the key is the MAC address and the value is the number of times it appeared in the file. You can do whatever you like with the array.
Was This Post Helpful? 0
  • +
  • -

#5 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Only grab unique MAC addresses

Posted 09 August 2010 - 06:36 PM

View PostCTphpnwb, on 09 August 2010 - 11:19 PM, said:

print_r is just to show the array contents, which are stored in an associative array where the key is the MAC address and the value is the number of times it appeared in the file. You can do whatever you like with the array.

Well then how do i make the array slot in 0,1, 2 (etc). Instead of having blank spaces where the duplicates were?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3079
  • View blog
  • Posts: 10,804
  • Joined: 08-August 08

Re: Only grab unique MAC addresses

Posted 09 August 2010 - 06:59 PM

See this:
http://us3.php.net/m....array-keys.php
Was This Post Helpful? 0
  • +
  • -

#7 mattisdada  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Only grab unique MAC addresses

Posted 09 August 2010 - 07:58 PM

View PostCTphpnwb, on 10 August 2010 - 10:59 AM, said:


Thanks that worked perfectly :). You have been most helpful!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1