Results 1 to 4 of 4
  1. #1

    Search for unused IP's in SQL

    Hey, I'm developing a IT Administration site, and I am adding a feature where it will automatically generate an UNUSED IP address and assign it to whatever you want. Now what I need is a way to have PHP go through SQL and assign an IP address that isn't already listed, does anyone know if this is possible? Basically I want the user to input the reason for the IP and the machines NETBIOS name and the script will pull up an unused IP address for them to assign to it. I'm simply having trouble figuring out how to get PHP to get an UNUSED one, can some one help me out? - for sale!

  2. #2
    Join Date
    Jun 2003
    I am assuming you have a list of IP's that are available to you? Why not just create a table with all of the available IP's, then do a sub query against your table you have assigned IP's to and utilize the NOT IN feature of SQL to determine what IP's have not been assigned/in use.

    What kind of database are you using? Depending on what version of MySQL your using it may not support sub queries. There are numerous alternatives to getting this work. In your table that stores available IP's, simply create a column that stores a boolean value 0=available, 1=in use to let you know.
    HostXM - Going the extra mile since 2001

  3. #3
    Join Date
    Jul 2003
    It really begs the question -- why don't you use a DHCP server for all this?

    Anyway, to find out all used IP addresses, you can ping the netmask and get a list of all respondents. This is assuming that ping is enabled on your net.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  4. #4
    Join Date
    Dec 2004
    Be aware that some firewall software will drop ICMP requests, and your pings will time out and indicate that there isn't a machine at that address, when in actuality there is.

    I'll second fyrestrtr's comment...this is exactly the problem that DHCP was intended to solve. If you must implement your own system, I'd recommend pretty much copying how it's done in DHCP. Your database contains a list of all the IPs that are available for use. When a user requests one, a lease record is added to a separate table with the machine's details and whatever else you want to relate to it. When the user no longer needs the IP, flag the lease as revoked. To find an unused IP, simply find one that doesn't have a matching lease, something like this, perhaps:
    SELECT * FROM available_ips WHERE id NOT IN (SELECT ip_id FROM leases WHERE revoked=0);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts