Last active
August 29, 2015 14:15
-
-
Save TJM/3ac28478da612cf14dc5 to your computer and use it in GitHub Desktop.
Lookup Akamai Staging Hosts to create a hosts file entry
This file contains 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
#!/usr/bin/php | |
<?php | |
# Convert a hostname to an "Akamai Staging" DNS entry | |
$parameters = array( | |
'h' => 'help', | |
'd' => 'debug' | |
); | |
$DEBUG = true; | |
$ip = array(); | |
$hosts = array(); | |
# Handle STDIN (list of sites) | |
stream_set_blocking(STDIN, 0); # Don't block on STDIN | |
while ( $host=trim(fgets(STDIN))) { | |
if (strpos($host, '#') === 0) { # Skip comments | |
continue; | |
} | |
# The following are to "clean up" the inputs | |
$host = preg_replace('/\s*#.*$/', '', $host); #strip trailing comments | |
$host = preg_replace('/^https?:\/\//', '', $host); # strip http:// or https:// | |
array_push($hosts, $host); | |
} | |
# HANDLE COMMAND LINE ARGS | |
$options = getopt(implode('', array_keys($parameters)), $parameters); | |
$DEBUG = isset($options['d']) || isset($options['debug']); | |
# The cleans up the argv array (removing options) | |
$pruneargv = array(); | |
foreach ($options as $option => $value) { | |
foreach ($argv as $key => $chunk) { | |
$regex = '/^'. (isset($option[1]) ? '--' : '-') . $option . '/'; | |
if ($chunk == $value && $argv[$key-1][0] == '-' || preg_match($regex, $chunk)) { | |
array_push($pruneargv, $key); | |
} | |
} | |
} | |
while ($key = array_pop($pruneargv)) unset($argv[$key]); | |
# Look for hosts as command line args | |
foreach ($argv as $arg) { | |
if ($arg == $argv[0]) continue; # skip command name | |
if ( preg_match('/(\w\.){1,}\w/', $arg) ) { # Looks like a hostname | |
array_push($hosts, $arg); | |
} else { | |
fputs (STDERR, "ERROR: $arg doesn't appear to be a host" . PHP_EOL); | |
} | |
} | |
if (count($hosts) < 1) { | |
fputs (STDERR, "ERROR: NO Hosts Specified" . PHP_EOL); | |
exit(1); | |
die; | |
} | |
foreach ($hosts as $host) { | |
$stagehost = get_staging_cname($host); | |
if ( ! $stagehost ) { | |
fputs (STDERR, "ERROR: Could not determine a staging CNAME for $host" . PHP_EOL); | |
continue; | |
} | |
if ( ! isset($ip["$stagehost"]) ) { | |
debug ("# gethostbyname: $stagehost ..."); | |
$ip["$stagehost"] = gethostbyname($stagehost); | |
} else { | |
debug ("# using cached value for $stagehost"); | |
} | |
fputs (STDOUT, $ip["$stagehost"] . "\t" . $host . PHP_EOL); | |
} | |
# debug: print a debug message if $DEBUG is true | |
# -- Args: msg - message to print | |
function debug($msg) { | |
global $DEBUG; | |
if ($DEBUG) fputs (STDERR, $msg . PHP_EOL); | |
} | |
# get_staging_cname: recursive function to return a staging CNAME that matches a regexp | |
# -- Args: host - hostname, match - regexp we are looking for | |
# -- Returns: CNAME target or FALSE | |
# NOTE: This chaos is not required on PHP 5.5+ | |
function get_staging_cname($host, $match='/\.edge(suite|key).net$/i') { | |
debug("# Querying $host ..."); | |
$results=dns_get_record($host, DNS_CNAME); | |
#debug(print_r($results, true)); | |
$target = ''; # So we can catch the last $target | |
foreach ($results as $result) { | |
$target = $result['target']; | |
debug("# target: $target"); | |
if ( preg_match($match, $target) ) { | |
$stagehost = preg_replace($match, '.edge${1}-staging.net', $target); | |
debug("# stagehost: $stagehost"); | |
return $stagehost; | |
} | |
} | |
if ( preg_match('/(\w\.){1,}\w/', $target) ) { # Looks like a hostname | |
return get_staging_cname($target, $match); | |
} else { | |
return FALSE; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: For some reason, this hangs on OSX 10.10.2 / PHP 5.5.14.
Wireshark is showing me that its querying CNAME against nothing (), instead of the name it is supposed to. I consider it a PHP bug versus a script problem.