Page 1 of 2
IP address range script
Posted: Tue Jun 28, 2005 11:35 am
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.
Posted: Tue Jun 28, 2005 11:58 am
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...)
Posted: Tue Jun 28, 2005 1:50 pm
by tuanfishy
You could use something like:
$get_ip = ip2long (getenv('REMOTE_ADDR'));
Posted: Tue Jun 28, 2005 3:01 pm
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;
}
}
Posted: Wed Jun 29, 2005 8:44 am
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]
Posted: Wed Jun 29, 2005 9:41 am
by pickle
Throw my code in where your 'where' loop is.
Posted: Wed Jun 29, 2005 9:53 am
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;
}
}
?>
Posted: Wed Jun 29, 2005 10:03 am
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.
Posted: Wed Jun 29, 2005 10:55 am
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?
Posted: Wed Jun 29, 2005 11:02 am
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).
Posted: Wed Jun 29, 2005 11:25 am
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!
Posted: Wed Jun 29, 2005 11:34 am
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.
Posted: Wed Jun 29, 2005 11:59 am
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...
Posted: Wed Jun 29, 2005 12:05 pm
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.
Posted: Thu Jun 30, 2005 3:50 am
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