从Apache vhosts中提取唯一的域名和文档根

时间:2016-08-08 22:22:00

标签: bash awk

我有一个Apache vhost看起来有点像这样:

<VirtualHost *:80>
ServerName aaaaaa.com
ServerAlias www.aaaaaa.com upload.aaaaaa.com publisher.aaaaaa.com
ServerAlias aaaaaa.no upload.aaaaaa.no www.aaaaaa.no publisher.aaaaaa.no
ServerAlias bbbbbb.no www.bbbbbb.no upload.bbbbbb.no publisher.bbbbbb.no
ServerAlias cccccc.live upload.cccccc.live www.cccccc.live publisher.cccccc.live
ServerAlias dddddd.com *.dddddd.com
ServerAlias aaaaaa.cccccc.live *.aaaaaa.cccccc.live
ServerAlias eeeeee.com *.eeeeee.com
ServerAlias ffffff.com *.ffffff.com
ServerAlias aaaaaa-com.bbbbbb.no

ServerAdmin webmaster@bbbbbb.no

ErrorLog /var/log/apache2/aaaaaa.com-error.log
CustomLog /var/log/apache2/aaaaaa.com-access.log combined
ServerSignature Off

<IfModule mod_php5.c>
    AddType application/x-httpd-php .php .phtml .php3
    AddType application/x-httpd-php-source .phps
</IfModule>

DocumentRoot /var/www/html/aaaaaa.com/current/www/
<Directory /var/www/html/aaaaaa.com/current>
    AllowOverride all
</Directory>

Alias  /oc-publisher-framework /var/www/releases/oc-publisher-framework-www/

    # Fake https to become on for Cloudflare special headers.
    SetEnvIf X-Forwarded-Proto https HTTPS=on
    SetEnvIf CF-Visitor {"scheme":"https"} HTTPS=on
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost _default_:443>

ServerName aaaaaa.com
ServerAlias aaaaaa.no
ServerAlias www.aaaaaa.no
ServerAlias bbbbbb.no
ServerAlias www.bbbbbb.no
ServerAlias cccccc.live
ServerAlias www.cccccc.live publisher.cccccc.live
ServerAlias dddddd.com
ServerAlias www.dddddd.com
ServerAlias www.aaaaaa.com
ServerAlias gc-aaaaaa.aaaaaa.com
ServerAlias www.gc-aaaaaa.aaaaaa.com
ServerAlias aaaaaa.cccccc.live
ServerAlias www.aaaaaa.cccccc.live
ServerAlias eeeeee.com www.eeeeee.com

    ServerAdmin webmaster@bbbbbb.no

    ErrorLog /var/log/apache2/aaaaaa.com-error.log
    CustomLog /var/log/apache2/aaaaaa.com-access.log combined
    ServerSignature Off

    <IfModule mod_php5.c>
      AddType application/x-httpd-php .php .phtml .php3
      AddType application/x-httpd-php-source .phps
    </IfModule>

    DocumentRoot /var/www/html/aaaaaa.com/current/www/
    <Directory /var/www/html/aaaaaa.com/current>
      AllowOverride all
    </Directory>

    Alias  /oc-publisher-framework /var/www/html/releases/oc-publisher-framework-www/

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/bbbbbb.no.crt
    SSLCertificateKeyFile /etc/apache2/ssl/bbbbbb.no.key.txt
    <FilesMatch "\.(cgi|shtml|phtml|php)$">
            SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
            SSLOptions +StdEnvVars
    </Directory>
    BrowserMatch "MSIE [2-6]" \
            nokeepalive ssl-unclean-shutdown \
            downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</VirtualHost>
</IfModule>

我想从这里提取唯一的域名和文档根,所以它会输出如下内容:

/var/www/html/aaaaaa.com/current/www/ - aaaaaa.com www.aaaaaa.com upload.aaaaaa.com publisher.aaaaaa.com aaaaaa.no upload.aaaaaa.no www.aaaaaa.no publisher.aaaaaa.no bbbbbb.no www.bbbbbb.no upload.bbbbbb.no publisher.bbbbbb.no ...

如何使用AWK,BASH或Python实现此目的?

2 个答案:

答案 0 :(得分:0)

您可以使用awk

awk '$1 ~ /^(ServerName|ServerAlias)/ {
   for (i=2; i<=NF; i++)
      hosts[$i]
}
$1 == "DocumentRoot" {
   dr = $2
}
END {
  printf "%s -", dr
  for (i in hosts)
     printf " %s", i
  print ""
}' httpd.conf

/var/www/html/aaaaaa.com/current/www/ - www.aaaaaa.cccccc.live dddddd.com www.cccccc.live www.eeeeee.com *.ffffff.com upload.cccccc.live www.bbbbbb.no bbbbbb.no ffffff.com eeeeee.com upload.aaaaaa.no gc-aaaaaa.aaaaaa.com publisher.aaaaaa.no publisher.cccccc.live upload.aaaaaa.com www.aaaaaa.com aaaaaa-com.bbbbbb.no www.gc-aaaaaa.aaaaaa.com aaaaaa.cccccc.live www.aaaaaa.no aaaaaa.no publisher.aaaaaa.com aaaaaa.com www.dddddd.com *.eeeeee.com *.aaaaaa.cccccc.live *.dddddd.com cccccc.live upload.bbbbbb.no publisher.bbbbbb.no

答案 1 :(得分:0)

我只想列出所有启用的域,所以我稍微修改了@anubhava的答案,以列表形式显示域。如果那是其他人想要的,那么您可以:

#!/bin/bash
awk '$1 ~ /^(ServerName|ServerAlias)/ {
   for (i=2; i<=NF; i++)
      hosts[$i]
}
END {
  printf ""
  for (i in hosts)
     printf "%s\n", i
}' /etc/apache2/sites-enabled/*

这是基于Debian的系统,但是如果您运行其他设置,只需指向其他路径即可。