如何从 Apache 配置文件中提取主机名和虚拟主机 IP 地址?

问题描述 投票:0回答:1

我需要管理域名(fqdn),将所有域名打印在某个地方,可能在文件 csv 中。所有信息都在apacheconf文件中,我需要将所有域名与ip和端口相关联。 这是示例文件

<VirtualHost 10.11.12.21:80>
ServerName host-6.mydomain.example.org
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-6.mydomain.example.org_access_log 86400" customssl
</VirtualHost>
<VirtualHost 10.11.12.21:80>
ServerName www.example.com
ServerAlias www.example.com
www.example.biz
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/www.example.com/www.example.com_access_log 86400" customssl
</VirtualHost>
<VirtualHost 10.11.12.21:80>
ServerName gimbo.customexample.org
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/gimbo.customexample.org/gimbo.customexample.org_access_log 86400" customssl
</VirtualHost>
<VirtualHost 10.11.12.22:80>
ServerName host-6.mydomain.example.org
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-6.mydomain.example.org_access_log 86400" customssl
</VirtualHost>
<VirtualHost 10.11.12.22:443>
ServerName host-6.mydomain.example.org
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-6.mydomain.example.org_ssl_access_log 86400" customssl
IncludeOptional conf.d/star_mydomain_example_org.cnf
</VirtualHost>
<VirtualHost 10.11.12.22:8080>
ServerName host-6.mydomain.example.org
ServerAlias www.example.org www.pollo.biz www.pollone.mondo
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-6.mydomain.example.org_access_log 86400" customssl
</VirtualHost>
<VirtualHost 10.11.12.22:80>
ServerName host-7.mydomain.example.org
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-7.mydomain.example.org/host-7.mydomain.example.org_access_log 86400" customssl
</VirtualHost>
<VirtualHost 10.11.12.22:443>
ServerName host-7.mydomain.example.org
ErrorLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-7.mydomain.example.org/host-7.mydomain.example.org_ssl_error_log 86400"
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/host-7.mydomain.example.org/host-7.mydomain.example.org_ssl_access_log 86400" customssl
IncludeOptional conf.d/star_mydomain_example_org.cnf
</VirtualHost>

这些是域名 (fqdn)

gimbo.customexample.org
host-6.mydomain.example.org
host-7.mydomain.example.org
www.example.biz
www.example.com
www.example.org
www.pollo.biz
www.pollone.mondo

我需要这个输出:

host-6.mydomain.example.org,10.11.12.21,80
www.example.com,10.11.12.21,80
www.example.com,10.11.12.21,80
www.example.biz,10.11.12.21,80
gimbo.customexample.org,10.11.12.21,80
host-6.mydomain.example.org,10.11.12.22,80
host-6.mydomain.example.org,10.11.12.22,443
host-6.mydomain.example.org,10.11.12.22,8080
www.example.org,10.11.12.22,8080
www.pollo.biz,10.11.12.22,8080
www.pollone.mondo,10.11.12.22,8080
host-7.mydomain.example.org,10.11.12.22,80
host-7.mydomain.example.org,10.11.12.22,443

我尝试过的:

awk -v n=2 -e '/^\s*Server(Name|Alias)/ { for (i=n; i<=NF; i++) printf "%s%s",   $i, (i<NF ? OFS : ORS)}' $1 | awk -v RS='[\n ]' '{print}'

现在我可以打印所有的 FQDN。 如何添加正确的 IP 地址和端口?

shell awk
1个回答
0
投票

这里有一个建议:

(文件

cmd.awk

BEGIN { 
  FS = "[ <>:]"
  OFS = ","
  }

/^<VirtualHost /    { IP = $3; Port = $4 }
/^ServerName /      { Names = $2 }
/^ServerAlias /     { sub(/ServerAlias /, "", $0); Names = Names " " $0 }
/^<\/VirtualHost>$/ { $0 = Names; for(i = 1; i <= NF; ++ i) { print $i, IP, Port } }

awk -f cmd.awk sample.input
应该产生所需的输出:

host-6.mydomain.example.org,10.11.12.21,80
www.example.com,10.11.12.21,80
www.example.com,10.11.12.21,80
www.example.biz,10.11.12.21,80
gimbo.customexample.org,10.11.12.21,80
host-6.mydomain.example.org,10.11.12.22,80
host-6.mydomain.example.org,10.11.12.22,443
host-6.mydomain.example.org,10.11.12.22,8080
www.example.org,10.11.12.22,8080
www.pollo.biz,10.11.12.22,8080
www.pollone.mondo,10.11.12.22,8080
host-7.mydomain.example.org,10.11.12.22,80
host-7.mydomain.example.org,10.11.12.22,443

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.