0

我有一个配置有许多虚拟主机的 Apache 服务器,并且希望能够像这样使用 grep 过滤它们。

# httpd -S | grep "myStr"

.. 但是,这种方法不起作用 - 我仍然看到列出了我的所有虚拟主机。有什么想法我可以实际做到这一点吗?

这是 httpd -S 的输出示例

[root@224136 config]# httpd -S
VirtualHost configuration:
184.22.xxx.33:80       is a NameVirtualHost
         default server default (/etc/httpd/conf/kloxo/default.conf:4)
         port 80 namevhost default (/etc/httpd/conf/kloxo/default.conf:4)
         port 80 namevhost webmail (/etc/httpd/conf/kloxo/webmail.conf:6)
         port 80 namevhost xxxxxx.com (/home/httpd/xxxxxx.com/conf/kloxo.xxxxxxx.com:4)
         port 80 namevhost twptest.xxxxxx.com (/home/httpd/twptest.xxxxxx.com/conf/kloxo.twptest.xxxxxx.com:4)
         port 80 namevhost yyyyyyy.com (/home/httpd/yyyyyy.com/conf/kloxo.yyyyyyy.com:4)
         port 80 namevhost zzzzzzzz.com (/home/httpd/zzzzzzz.com/conf/kloxo.zzzzzzzz.com:4)
         port 80 namevhost expenses.zzzzzzzz.com (/home/httpd/expenses.zzzzzzz.com/conf/kloxo.expenses.zzzzzzz.com:4)
         port 80 namevhost aaaaaaaa.cz (/home/httpd/aaaaaaaaaaaa.cz/conf/kloxo.aaaaaaaaaaa.cz:4)
         port 80 namevhost aaaaaaaaaaa.cz (/home/httpd/aaaaaaaaaaaaa.cz/conf/kloxo.aaaaaaaaaa.cz:142)
4

2 回答 2

1

既然您说“我仍然看到列出的所有虚拟主机”,我怀疑您需要将 STDERR 重定向到 STDOUT,如下所示(假设 bash)out.py只是一个 pythonscript,它使用将一些输出写入 STDERRsys.stderr.write()

$ ./out.py | grep zzzzzzzz

VirtualHost configuration:
184.22.xxx.33:80       is a NameVirtualHost
         default server default (/etc/httpd/conf/kloxo/default.conf:4)
         port 80 namevhost default (/etc/httpd/conf/kloxo/default.conf:4)
         port 80 namevhost webmail (/etc/httpd/conf/kloxo/webmail.conf:6)
         port 80 namevhost xxxxxx.com (/home/httpd/xxxxxx.com/conf/kloxo.xxxxxxx.com:4)
         port 80 namevhost twptest.xxxxxx.com (/home/httpd/twptest.xxxxxx.com/conf/kloxo.twptest.xxxxxx.com:4)
         port 80 namevhost yyyyyyy.com (/home/httpd/yyyyyy.com/conf/kloxo.yyyyyyy.com:4)
         port 80 namevhost zzzzzzzz.com (/home/httpd/zzzzzzz.com/conf/kloxo.zzzzzzzz.com:4)
         port 80 namevhost expenses.zzzzzzzz.com (/home/httpd/expenses.zzzzzzz.com/conf/kloxo.expenses.zzzzzzz.com:4)
         port 80 namevhost aaaaaaaa.cz (/home/httpd/aaaaaaaaaaaa.cz/conf/kloxo.aaaaaaaaaaa.cz:4)
         port 80 namevhost aaaaaaaaaaa.cz (/home/httpd/aaaaaaaaaaaaa.cz/conf/kloxo.aaaaaaaaaa.cz:142)

现在将 STDERR 发送到 STDOUT:

$ ./out.py 2>&1 | grep zzzzzzzz
         port 80 namevhost zzzzzzzz.com (/home/httpd/zzzzzzz.com/conf/kloxo.zzzzzzzz.com:4)
         port 80 namevhost expenses.zzzzzzzz.com (/home/httpd/expenses.zzzzzzz.com/conf/kloxo.expenses.zzzzzzz.com:4)
于 2011-06-28T13:15:13.610 回答
1

我只是有同样的需求,并使用了以下内容:

 /usr/sbin/apache2ctl -S 2>&1 | awk '/namevhost/  {print $4;} '

它在 awk 清理之前将 SDTERR (2) 重定向到 STDOUT (&1)

于 2012-03-05T09:37:53.980 回答