如何使用 grep 过滤 Apache 虚拟主机列表

发布于 2024-11-17 10:02:39 字数 1238 浏览 4 评论 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)

I have an Apache server configured with many virtual hosts and would like to be able to filter them using grep like so..

# httpd -S | grep "myStr"

.. however, this approach doesn't work - I'm still seeing all my virtual hosts listed. Any ideas how I can actually do this?

Here's a sample of the output from 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风铃鹿 2024-11-24 10:02:39

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

$ ./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)

Since you say that "I'm still seeing all my virtual hosts listed" I suspect you need to redirect STDERR to STDOUT as seen below (assuming bash) out.py is just a pythonscript that writes some output to STDERR using sys.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)

Now send STDERR to 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)
梦中的蝴蝶 2024-11-24 10:02:39

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

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

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

I just had the same need, and used the following:

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

It redirects SDTERR (2) into STDOUT (&1) before getting awk to clean it up

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文