我有一个配置了许多虚拟主机的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)
答案 0 :(得分:1)
因为你说“我仍然看到我列出的所有虚拟主机”我怀疑你需要将STDERR重定向到STDOUT,如下所示(假设是bash)out.py
只是一个pythonscript,它将一些输出写入STDERR使用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)
现在将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)
答案 1 :(得分:1)
我只是有同样的需求,并使用了以下内容:
/usr/sbin/apache2ctl -S 2>&1 | awk '/namevhost/ {print $4;} '
在获取awk之前将SDTERR(2)重定向到STDOUT(& 1)