Skip to content

Instantly share code, notes, and snippets.

@rxall
Created November 5, 2015 10:11
Show Gist options
  • Save rxall/5305bcaa008e51685998 to your computer and use it in GitHub Desktop.
Save rxall/5305bcaa008e51685998 to your computer and use it in GitHub Desktop.
A small script to grab floating IP's from Digital Ocean and check if they still have DNS entries pointing at them using Bing's ip: search
#!/usr/local/bin/php
<?php
//////////////////////////////////////////////////////
// A quick POC to grab IP's from Digital Ocean //
// that still have DNS records pointing to them //
// //
// Config: 1. Set BING_API_KEY //
// 2. Set DO_API_KEY //
// //
//danclifford.eu //
//////////////////////////////////////////////////////
define('BING_API_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('BING_API_URL', 'https://api.datamarket.azure.com/Bing/Search/Web?$format=json&Query=');
define('DO_API_KEY','XXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
//The current (at time of writing) list of regions floating IP's are available from
$do_region_array = array(
"AMS2",
"AMS3",
"FRA1",
"LON1",
"NYC1",
"NYC2",
"NYC3",
"SFO1",
"SGP1",
"TOR1"
);
//Minimal function to interact with the Digital Ocean API
function doAPI($url, $action, $droplet_id = null)
{
$ch = curl_init();
$headers = array(
"Authorization: Bearer " . DO_API_KEY,
"Content-Type: application/json"
);
if (isset($droplet_id)) {
$post_data = array(
"region" => $droplet_id,
);
$post_data_json = json_encode($post_data);
$headers[] = "Content-Length: " . strlen($post_data_json);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data_json);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url . $action);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
$response = curl_exec($ch);
if ($response === false) {
print("Error accessing Digital Ocean: " .curl_error($ch));
return 1;
}
return $response;
}
//Minimal function to interact with the Bing API
function bingReverseIP($ip_address)
{
$headers = array(
'Authorization: Basic ' . base64_encode(BING_API_KEY . ":" . BING_API_KEY)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, BING_API_URL . '%27ip%3a' . $ip_address . '%27');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
$response = curl_exec($ch);
return(json_decode($response));
}
while (1) {
foreach ($do_region_array as $region) {
$assign_ip = doAPI("https://api.digitalocean.com/v2/", "floating_ips", $region);
$request_response = json_decode($assign_ip);
if ($request_response !== 1) {
$fresh_ip = $request_response->floating_ip->ip;
print "Grabbed IP: " . $fresh_ip;
$bing_ip_search = bingReverseIP($fresh_ip);
//If no bing ip: records are found, release the IP and continue
//If bing ip: records are found, count the results, list them and retain the IP
if (empty($bing_ip_search->d->results)) {
print " no records found\n";
sleep(30); //Give time to grab the IP
doAPI("https://api.digitalocean.com/v2/", "floating_ips/" . $fresh_ip);
print "Released IP: " . $fresh_ip . "\n";
}else{
print " matched " . count($bing_ip_search->d->results) . " hosts\n";
print "Hosts:\n";
$found_hosts = listBingResults($bing_ip_search);
foreach ($found_hosts->d->results as $host) {
print $host->Url . "\n";
};
print "Keeping IP: " . $fresh_ip . "\n";
}
}
sleep(30);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment