给定路径X或apache vhosts配置文件的副本,如何使用PHP解析该文件?
例如,给定一个包含字符串的变量,该字符串具有Apache vhosts配置的内容,我如何获得托管域/子域别名的列表?
例如,给定:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact@tomjn.com
DocumentRoot "/srv/www/localhost/
ServerName 127.0.0.1
ServerAlias localhost
CustomLog "/srv/www/logs/localhost-access_log.log" combined
ErrorLog "/srv/www/logs/localhost-error_log.log"
<Directory "/srv/www/localhost">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin contact@tomjn.com
DocumentRoot "/srv/www/2.7.localhost.com/
ServerName 2.7.localhost.com
ServerAlias 2.7.localhost.com
CustomLog "/srv/www/logs/2.7.localhost.com-access_log.log" combined
ErrorLog "/srv/www/logs/2.7.localhost.com-error_log.log"
<Directory "/srv/www/2.7.localhost.com">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
我将如何获得此输出:
这是用Python写的非常接近的东西:
# Get Vhosts files
$path = '/etc/apache2/sites-enabled'; # change to suit your needs
$a_directory = scandir($path);
$a_conf_files = array_diff($a_directory, array('..', '.'));
$info = array(); $x=0;
foreach($a_conf_files as $conf_file){
$Thisfile = fopen($path .'/'.$conf_file, 'r')or die('No open ups..');
while(!feof($Thisfile)){
$line = fgets($Thisfile);
$line = trim($line);
// CHECK IF STRING BEGINS WITH ServerAlias
$tokens = explode(' ',$line);
if(!empty($tokens)){
if(strtolower($tokens[0]) == 'servername'){
$info[$x]['ServerName'] = $tokens[1];
}
if(strtolower($tokens[0]) == 'documentroot'){
$info[$x]['DocumentRoot'] = $tokens[1];
}
if(strtolower($tokens[0]) == 'errorlog'){
$info[$x]['ErrorLog'] = $tokens[1];
}
if(strtolower($tokens[0]) == 'serveralias'){
$info[$x]['ServerAlias'] = $tokens[1];
}
}else{
echo "Puked...";
}
}
fclose($file);
$x++;
}
print_r($info);
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => Array
(
[ServerName] => bootstrap
[ServerAlias] => BootstrapProject
[DocumentRoot] => /data/sites/bootstrap/htdocs
[ErrorLog] => /data/sites/bootstrap/log/error.log
)
[1] => Array
(
[ServerName] => localhost
[ServerAlias] => dfs
[DocumentRoot] => /data/sites/scott/htdocs
[ErrorLog] => /data/sites/scott/log/error.log
)
[2] => Array
(
[ServerName] => wordpress
[ServerAlias] => wordpress
[DocumentRoot] => /data/sites/wordpress/public_html
[ErrorLog] => /data/sites/wordpress/log/error.log
)
)
Run Code Online (Sandbox Code Playgroud)