IP address range script
Moderator: General Moderators
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
IP address range script
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.
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.
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.
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
Here is what I have - This simply outputs the IP addresses...
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]
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]
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
Hi,
Thanks - i have done it like this but it does not appear to be correctly structure... any thoughts?
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;
}
}
?>- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
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
I know that this has not been defined, but I am not sure how to define it. Any thoughts?
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?
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).
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.
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
$ips is an array of all the ips found. Call:
rather than echo($ips) to see the structure.
Code: Select all
echo '<pre>';
print_r($ips);
echo '</pre>'Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am
What's your query look like? You shouldn't be getting blank entries in $ips.
You query should look something like:
That removes all the ip fields that are blank.
You query should look something like:
Code: Select all
SELECT
distinct(IP)
FROM
dedicatedThat's what should happen. The while loop then goes through and finds the first IP that isn't found.mhouldridge wrote: howver the taken IPs are the ones which are displayed.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- mhouldridge
- Forum Contributor
- Posts: 267
- Joined: Wed Jan 26, 2005 5:13 am