检查shell脚本中的http头文件

我该如何编写一个脚本,向网页发送获取请求并返回格式良好的http头?

尝试这个讨厌的黑客:

wget -O - -o /dev/null --save-headers http://google.com | \ awk 'BEGIN{skip=0}{ if (($0)=="\r") {skip=1;}; if (skip==0) print $0 }' 

一个选项可能是使用curl和–dump-header选项。

在许多不同的语言中有相当多的模块会为你检索HTTP标题。

  • Python – urllib2
  • Perl – LWP :: Simple

等等

curl支持在下载时查看标题,或者可以使用-I选项将标题保存到文件中。

如果你只是想查看标题..(不是编程),只需使用[live http headers] [1]插件为Mozilla Firefox。

https://addons.mozilla.org/en-US/firefox/addon/3829

这个bash函数将采用URL +方法,或者服务器+path+方法+端口。 如果使用“HEAD”方法,它将返回标题,如果使用GET,它将返回所有标题和整个回复。 通过openssl支持https。

 #!/bin/bash function httpreq () { if [ $# -eq 0 ]; then echo -e "httpreq SERVER PATH [GET/HEAD] [PORT]\nOR\nhttpreq URL [GET/HEAD]"; return 1; fi if echo $1 | grep -q "://" then SUNUCU=$(echo $1 |cut -d '/' -f3 | cut -d':' -f1) YOL=/$(echo $1 |cut -d '/' -f4-) PROTO=$(echo $1 |cut -d '/' -f1) METHOD=${2:-GET} PORT=$(echo $1| sed -n 's&^.*://.*:\([[:digit:]]*\)/.*&\1&p') if [ -z $PORT ] then if [ $PROTO == "https:" ]; then PORT=443; else PORT=80; fi fi else SUNUCU=$1 YOL=$2 METHOD=${3:-GET} PORT=${4:-80} fi if [ $PROTO == "https:" ]; then echo -e "$METHOD $YOL HTTP/1.1\r\nHOST:$SUNUCU\r\n\r\n" | openssl s_client -quiet -connect $SUNUCU:$PORT else echo -e "$METHOD $YOL HTTP/1.1\r\nHOST:$SUNUCU\r\n\r\n" | nc $SUNUCU $PORT fi }