IP address range script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

IP address range script

Post by mhouldridge »

Hi,

I am creating an audit site for our company auditing our servers. I would like the site to be able to show available IP addresses for a particular range, i.e;

193.128.48.1 - 193.128.48.255

The audit program so far allows the user to insert an IP address which is stored within a mySQL database.

I would like to create a loop script which looks through the taken IP addresses within my field called IP, and then output the available ones.

Any ideas?

my database is called audit, my table is called dedicated and then IP address field is called IP.

Any help would be greatly appreciated.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I wouldn't store them as varchar, but as int32 (If you are going to support ipv6, make that int128)


This way you can easily select values in a range (BETWEEN operator)

So all you need to do is write ipstring2ipnumber and ipnumber2ipstring functions (or find someone who already has done this...)
tuanfishy
Forum Newbie
Posts: 4
Joined: Tue Jun 28, 2005 1:47 pm
Location: Canada

Post by tuanfishy »

You could use something like:

$get_ip = ip2long (getenv('REMOTE_ADDR'));
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

This is a simple loop:

Code: Select all

//move the result set into an array
$resultset = a query getting all ips;
while($row = mysql_fetch_assoc($resultset))
{
  $ips[] = $row['ip'];
}

//search through the array of assigned ips for a hole
for($counter = i;$counter >= 255; ++$counter)
{
 if(!in_array('193.128.48.'.$counter',$ips));
 {
   $found_ip = '193.128.48.'.$counter;
   break;
 }
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Here is what I have - This simply outputs the IP addresses...

Code: Select all

<?
						 
						 //Connection to database
						   	$db = mysql_connect("localhost","","") or die("Problem connecting");
							mysql_select_db("audit") or die("Problem selecting database");
						 	
							$result = mysql_query('select IP from dedicated');
							if(!$result){
								exit('<p>Error performing query: '.mysql_error().'</p>');
								}
							while ($row = mysql_fetch_array($result)){
								echo '<p>' .$row['IP'] . '</p>';
								}
									
							?>

I need this to output IP addresses available from a range, i.e 192.168.0.1 - 192.168.0.255

Please could you show me how this can be done within my code?[/b]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Throw my code in where your 'where' loop is.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Hi,

Thanks - i have done it like this but it does not appear to be correctly structure... any thoughts?

Code: Select all

<?
						 
$db = mysql_connect("localhost","giacom-admin","snooker") or die("Problem connecting");
mysql_select_db("audit") or die("Problem selecting database");			 	
$result = mysql_query('select IP from dedicated');
if(!$result){
exit('<p>Error performing query: '.mysql_error().'</p>');
}
while($row = mysql_fetch_assoc($resultset)){ 
 $ips[] = $row['ip'];
 } 
 for($counter = i;$counter >= 255; ++$counter){ 
 if(!in_array('193.128.48.'.$counter',$ips)); 
 {   
 $found_ip = '193.128.48.'.$counter;   
 break;
 }
}
									
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Look at the syntax highlighting and it'll show you - you've got an extra single quote after $counter in the last 'if' statement. It should have told you that in the error it kicked out.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Excellent - I think we're getting somewhere now, however i get the following error;

Notice: Use of undefined constant i - assumed 'i' in F:\Auditwebsite\IP.php on line 64

Code: Select all

<?
						   	$db = mysql_connect("localhost","","") or die("Problem connecting");
							mysql_select_db("audit") or die("Problem selecting database");
						 	
							$result = mysql_query('select IP from dedicated');
								if(!$result){
								exit('<p>Error performing query: '.mysql_error().'</p>');
								}
									while($row = mysql_fetch_assoc($result)){ 
									$ips[] = $row['IP'];
									} 
									[b]for($counter = i;[/b]
									$counter >= 255; ++$counter){ 
									if(!in_array('193.128.48.'.$counter,$ips)); 
									{ 
									$found_ip = '193.128.48.'.$counter;   
									break;
								}
							}
							?>


I know that this has not been defined, but I am not sure how to define it. Any thoughts?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That isn't a fatal error - at lower levels of error reporting, that doesn't even show up. I actually don't get those errors because my reporting level is set lower.

Try just declaring the variable ( "i;" or "i = NULL;" or "i = '';" should work).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

The page loads now without errors and I have added an echo $isp, but this displays "Array".

Hmm... not sure if I have echoed the correct part.

thanks again!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

$ips is an array of all the ips found. Call:

Code: Select all

echo '<pre>';
print_r($ips);
echo '</pre>'
rather than echo($ips) to see the structure.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Yes - The output is shown now, howver the taken IPs are the ones which are displayed.

I also get;

[31] =>
[32] =>
[33] =>
[34] =>
[35] =>
[36] =>
[37] =>
[38] =>
[39] =>

And so on...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What's your query look like? You shouldn't be getting blank entries in $ips.

You query should look something like:

Code: Select all

SELECT
  distinct(IP)
FROM
  dedicated
That removes all the ip fields that are blank.


mhouldridge wrote: howver the taken IPs are the ones which are displayed.
That's what should happen. The while loop then goes through and finds the first IP that isn't found.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

The thing is, I need to show the IP fields that are not taken. Therefore if for instance I had inserted;

192.168.1.1
192.168.1.5

I would want the page to display the other IPs that are available from the range 192.168.1.1 - 255
Post Reply