Last active
September 2, 2022 00:53
-
-
Save mattpascoe/3988007 to your computer and use it in GitHub Desktop.
DHCP Leasequery via php -- initial attempts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Main class can be found here: | |
* https://www.phpclasses.org/package/6621-PHP-Send-queries-to-a-DHCP-server.html | |
* | |
* A simple test script to demonstrate how to use the dhcpLeaseQuery class. | |
* Please read the note below first then edit the variables below as appropriate. | |
* Then simply run: php LeaseQuery.php | |
* | |
* Note: ISC's dhcpd will *NOT* listen for LeaseQuery packets on the same system | |
* it is running on. Not even on the local loopback. This is because the LeaseQuery | |
* packet is intended to be sent from a true DHCP Relay such as a router or access server. | |
* Thus, make certain that you are running this from a different system other than the | |
* dhcp server itself and set the $gi variable to the IP of the box you are running this | |
* from. (if on a multi-homed system, be sure to set it to the IP you will be sending | |
* from). | |
* | |
* If this file looks funky to you, try setting tab stops=4. | |
* | |
* @author Pat Winn ([email protected]) | |
* @date 10/16/2010 | |
* @version 1.0 | |
*/ | |
// Date needs a timezone | |
date_default_timezone_set('UTC'); | |
require_once('dhcpLeaseQuery.php'); | |
/* | |
MDP notes: | |
I took Pats original leasequery script and added to it for my goals. He takes | |
all the credit for actually figuring this crap out. | |
must enable 'allow leasquery;' on dhcp server | |
The GI value here needs to have an existing subnet defined on the DHCP server you ask | |
the SV is simply the DHCP server.. nothing seems special at this time about its use | |
the CI works so far as follows: | |
gives nothing useful for fixed addresses???? | |
Must have a full discover/offer available. just an inform/ack wont show up it seems | |
I have no idea what this does with IPv6 stuff (ip2long yea) or large pool ranges. could get nasty with the server? | |
TODO: might be wise to do a lookup of DHCP options in ONA to get ones the user has created so they | |
map to something correctly in your environment | |
maybe use a sudoers file to allow this to be ran? its a security issue for sure. | |
TODO: set up 10. arpa domain on windows AD server. this way a directed DNS reverse lookup could | |
be done in addition to a standard one to try and figure out what name was associated. | |
*/ | |
/* USAGE STUFF | |
php ./LeaseQuery.php -r 10.1.14.31 -s 10.100.17.13 -b 10.100.6.10 -e 10.100.6.100 | |
r= local ip identifying who is asking for leasequery info (subnet it lives in must be defined on the dhcp server as well) | |
b= begin ip in range | |
e= end ip in range | |
or | |
i= single ip | |
u print unassigned leases | |
s= The DHCP server IP to send the query to | |
*/ | |
// maybe if we run this from within ONA it can just use the IP of the ONA system by default? | |
echo $_SERVER['SERVER_ADDR']; | |
$leases = array(); | |
$options = getopt("ub:e:i:s:r:"); | |
//$gi = "10.1.14.31"; // dhcp relay ip | |
if ($options['r']) { | |
$gi = $options['r']; | |
} else { | |
echo "-r <Relay IP> is required, this is simply an IP on this server that sends/receives the queries.\n"; | |
exit(1); | |
} | |
if ($options['s']) { | |
$sv = $options['s']; | |
} else { | |
echo "-s <Server IP> is required\n"; | |
exit(1); | |
} | |
// Setup connection to a server | |
$lq = new dhcpLeaseQuery($gi, $sv); | |
// Process a single IP | |
if ($options['i']) { | |
$leases[$options['i']] = doquery($options['i']); | |
} | |
// Process a begin/end range of IPs | |
if ($options['b'] and $options['e']) { | |
$begin = ip2long($options['b']); | |
$end = ip2long($options['e']); | |
while ($begin <= $end) { | |
$ip = long2ip($begin); | |
$leases[$ip] = doquery($ip); | |
$begin++; | |
} | |
} | |
// Print out the elements we have gathered in the leases array | |
// Printing the CID circuit ID as it is very useful | |
#print_r($leases); | |
foreach($leases as $lease) { | |
if (!isset($options['u']) and ($lease['message_type'] == 'DHCPLEASEUNASSIGNED')) { continue; } | |
echo "{$lease['ciaddr']} ({$lease['chaddr']}): {$lease['message_type']} {$lease['lease_time']} {$lease['cid']}\n"; | |
} | |
// Perform the actual query on specified IP address | |
function doquery($ci) { | |
global $lq; | |
if($lq->sendQuery($ci)) { | |
$lease = $lq->receive(); | |
if($lease !== false) { | |
return($lease); | |
} else { | |
echo "ERROR: There was an issue receiving the data.\n"; | |
exit(1); | |
} | |
} else { | |
echo "ERROR: There was an issue contacting the server specified.\n"; | |
exit(1); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment