Skip to content

Instantly share code, notes, and snippets.

@tuxmartin
Last active January 16, 2017 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxmartin/a36c1d1ca127e94ab61125c22f07dc3b to your computer and use it in GitHub Desktop.
Save tuxmartin/a36c1d1ca127e94ab61125c22f07dc3b to your computer and use it in GitHub Desktop.
<?php
function apache_vhosts($binary='sudo /usr/sbin/apache2ctl')
{
$command = " -S 2>&1 | grep 'port ' | awk {'print $2,$4'} | sort -u -t' ' -k2,2 | grep -v 'localhost'";
$vhosts = shell_exec(sprintf("%s %s", $binary, $command));
$vhosts = explode("\n", trim($vhosts));
$results = array();
foreach($vhosts as $vhost)
{
$x = explode(' ', $vhost, 2);
$new_entry['{#SERVERNAME}'] = $x[1];
$new_entry['{#SERVERPORT}'] = $x[0];
$results['data'][] = $new_entry;
}
return $results;
}
function nginx_vhosts() {
$vhosts = shell_exec("grep -Pro '\bserver_name\s*\K[^;]*' /etc/nginx | sed 's/:/ /' | awk {'print $1,$2'} | grep -v 'localhost'");
$vhosts = explode(PHP_EOL, trim($vhosts));
$results = array();
foreach($vhosts as $vh) {
$x = explode(' ', $vh, 2);
$domain = $x[1];
$file = $x[0];
# Grab ports from file
$ports = shell_exec( sprintf("grep -Pro '\blisten\s*\K[^;]*' %s | awk {'print $1'} | grep -o '[0-9]*' | sort | uniq", $file));
$ports = explode(PHP_EOL, trim($ports), 2);
foreach($ports as $port)
{
$result = array();
$result['{#SERVERNAME}'] = $domain;
$result['{#SERVERPORT}'] = $port;
$results['data'][] = $result;
}
}
return $results;
}
function main() {
if (file_exists('/etc/nginx_DISABLE')) {
echo json_encode( nginx_vhosts() );
}
elseif (file_exists('/usr/sbin/apache2ctl'))
{
echo json_encode( apache_vhosts() );
} elseif (file_exists('/usr/sbin/apachectl')) {
echo json_encode( apache_vhosts('/usr/sbin/apachectl') );
}
}
main();
<?php
$results = array();
function apache_vhosts(array $results) {
$binary='sudo /usr/sbin/apache2ctl';
$command = " -S 2>&1 | grep 'port ' | awk {'print $2,$4'} | sort -u -t' ' -k2,2 | grep -v 'localhost'";
$vhosts = shell_exec(sprintf("%s %s", $binary, $command));
$vhosts = explode("\n", trim($vhosts));
foreach($vhosts as $vhost) {
$x = explode(' ', $vhost, 2);
$new_entry['{#SERVERNAME}'] = $x[1];
$new_entry['{#SERVERPORT}'] = $x[0];
$results['data'][] = $new_entry;
}
}
function nginx_vhosts(array $results) {
$vhosts = shell_exec("grep -Pro '\bserver_name\s*\K[^;]*' /etc/nginx | sed 's/:/ /' | awk {'print $1,$2'} | grep -v 'localhost'");
$vhosts = explode(PHP_EOL, trim($vhosts));
foreach($vhosts as $vh) {
$x = explode(' ', $vh, 2);
$domain = $x[1];
$file = $x[0];
# Grab ports from file
$ports = shell_exec( sprintf("grep -Pro '\blisten\s*\K[^;]*' %s | awk {'print $1'} | grep -o '[0-9]*' | sort | uniq", $file));
$ports = explode(PHP_EOL, trim($ports), 2);
foreach($ports as $port) {
$result = array();
$result['{#SERVERNAME}'] = $domain;
$result['{#SERVERPORT}'] = $port;
$results['data'][] = $result;
}
}
}
if (file_exists('/usr/sbin/apachectl')) {
echo json_encode( apache_vhosts($results) );
}
if (file_exists('/etc/nginx')) {
echo json_encode( nginx_vhosts($results) );
}
return $results;
?>
{
"data":[
{
"{#SERVERNAME}":"example.com",
"{#SERVERPORT}":"443"
},
{
"{#SERVERNAME}":"example.com",
"{#SERVERPORT}":"80"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment