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?
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.
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);