从wget解析http响应头
题
我试图从wget的结果中提取一条线但是遇到了麻烦。 这是我的wget电话:
$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html
输出:
--18:24:12-- http://xxx.xxxx.xxxx:15000/myhtml.html => `-' Resolving xxx.xxxx.xxxx... xxx.xxxx.xxxx Connecting to xxx.xxxx.xxxx|xxx.xxxx.xxxx|:15000... connected. HTTP request sent, awaiting response... HTTP/1.1 302 Found Date: Tue, 18 Nov 2008 23:24:12 GMT Server: IBM_HTTP_Server Expires: Thu, 01 Dec 1994 16:00:00 GMT Location: https://xxx.xxxx.xxxx/siteminderagent/... Content-Length: 508 Keep-Alive: timeout=10, max=100 Connection: Keep-Alive Content-Type: text/html; charset=iso-8859-1 Location: https://xxx.xxxx.xxxx//siteminderagent/... --18:24:13-- https://xxx.xxxx.xxxx/siteminderagent/... => `-' Resolving xxx.xxxx.xxxx... failed: Name or service not known.
如果我这样做:
$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html | egrep -i "302" <br/>
它不会返回包含字符串的行。我只想检查网站或网站管理员是否已启动。
解决方案
你要找的wget的输出写在stderr上。你必须重定向它:
$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 | egrep -i "302"
其他提示
wget
将标题打印到stderr,而不是stdout。您可以按如下方式将stderr重定向到stdout:
wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 | egrep -i "302"
<!>“2 <!> gt; <!> amp; 1 <!> part表示将文件描述符2(stderr)重定向('<!> gt;')到文件描述符1(stdout)。
已提供解决方案的一点增强版本
wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml。 html 2 <!> gt; <!> amp; 1 <!> gt; / dev / null | grep -c 302
2>&1 >/dev/null
将删除不需要的输出。这样egrep将只解析wget`s stderr,这样可以消除从stdout捕获包含302的字符串的可能性(其中html文件本身输出+下载处理栏,结果字节数为e.t.c.)。)
egrep -c
计算匹配字符串的数量,而不是简单地输出它们。足以知道egrep匹配多少字符串。
wget --server-response http://www.amazon.de/xyz 2 <! > gt; <!> amp; 1 | awk'/ ^ HTTP / {print $ 2}'
Just to explicate a bit. The -S
switch in the original question is shorthand for --server-response
.
Also, I know the OP specified wget
, but curl
is similar and defaults to STDOUT .
curl --head --silent $yourURL
or
curl -I -s $yourURL
The --silent
switch is only needed for grep
-ability: (-s
turns off progress % meter)