获取Web服务器类型的方法

转载自:http://www.linuxorz.com/show-15-1.html

Curl是一款用于在网络上传输数据的工具,支持HTTP, HTTPS, FTP, FTPS, TFTP, DICT,TELNET,LDAP等协议。通过curl你既可以将服务器的数据下载下来,也可以将本地的数据上传到服务器。curl的选项很多,大家可以参考curl manual。

最简单的用法,获取一个网站页面的源代码:

[root@linuxorz ~]# curl www.g.cn
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.cn/webhp?source=g_cn">here</A>.
</BODY></HTML>
[root@linuxorz ~]# curl www.google.cn

 言归正传,下面将告诉大家如何使用-Is选项来获取网站的web服务器类型。
-I :获取网站的HTTP-header信息
-s :静默模式。不显示进度及错误信息
--connect-timeout :设置连接超时秒数

[root@linuxorz ~]#  curl -Is www.163.com
HTTP/1.0 200 OK
Server: nginx
Date: Fri, 08 Jan 2010 14:40:23 GMT
Content-Type: text/html; charset=GBK
Vary: Accept-Encoding
Expires: Fri, 08 Jan 2010 14:41:43 GMT
Cache-Control: max-age=80
Vary: User-Agent
Vary: Accept
X-Cache: MISS from cache.163.com
Connection: close

首先创建一个site.txt文件,里面输入相应的网站。

[root@linuxorz ~]#  head site.txt
www.google.com
www.baidu.com
youa.baidu.com
post.baidu.com
hi.baidu.com
www.sogou.com
www.youdao.com
www.soso.com
www.sohu.com
www.sina.com

之后结合下面的curl.sh脚本,就可以获知site.txt中网站的服务器类型了。

#!/bin/sh
IIS=0
nginx=0
apache=0
other=0
if [ ! -f site.txt ]; then 
   echo "ERROR:site.txt NOT exists!" 
exit 1
fi

total=`wc -l site.txt|awk '{print $1}'`
for website in `cat site.txt`
do
   server=`curl -Is --connect-timeout 15 $website|awk -F":" '/^Server:/{print $2}'`
   echo -e $website":" $server
   if echo $server|grep -i "IIS">/dev/null
     then IIS=`expr $IIS + 1`
   elif echo $server|grep -i "Apache">/dev/null
     then Apache=`expr $Apache + 1`
   elif echo $server|grep -i "nginx">/dev/null
     then nginx=`expr $nginx + 1`
   else other=`expr $other + 1`
   fi
done
echo "--------------------------------------------"
echo -e "Total\tApache\tIIS\tnginx\tother"
echo -e "$total\t$Apache\t$IIS\t$nginx\t$other"
echo -e "100%\t"`echo "scale=5;$Apache/$total*100"|bc|cut -c1-5`"%\t"`echo "scale=5;$IIS/$total*100"|bc|cut -c1-5`"%\t"`echo "scale=5;$nginx/$total*100"|bc|cut -c1-5`"%\t"`echo "scale=5;$other/$total*100"|bc|cut -c1-5`"%\t"
echo "--------------------------------------------"
exit 0

猜你喜欢

转载自wikimo.iteye.com/blog/643597