---
title: "获取Web服务器类型的方法"
author: "wikimo"
date: "2010-04-14T12:48:04Z"
tags: []
categories: ["互联网"]
slug: "web"
draft: false
lastmod: "2010-04-14T12:48:04Z"
id: 643597
url: "https://wikimo.iteye.com/blog/643597"
---

转载自：[http://www.linuxorz.com/show-15-1.html](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  
&lt;HTML&gt;&lt;HEAD&gt;&lt;meta http-equiv="content-type" content="text/html;charset=utf-8"&gt;  
&lt;TITLE&gt;301 Moved&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;  
&lt;H1&gt;301 Moved&lt;/H1&gt;  
The document has moved  
&lt;A HREF="http://www.google.cn/webhp?source=g\_cn"&gt;here&lt;/A&gt;.  
&lt;/BODY&gt;&lt;/HTML&gt;  
\[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](http://www.linuxorz.com/html/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"&gt;/dev/null  
     then IIS=\`expr $IIS + 1\`  
   elif echo $server|grep -i "Apache"&gt;/dev/null  
     then Apache=\`expr $Apache + 1\`  
   elif echo $server|grep -i "nginx"&gt;/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