当前位置: > shell编程 >

shell程序获取互联网公司web server 信息

时间:2014-08-09 17:49来源:blog.csdn.net 作者:凤凰舞者

一.问题来源

   想得到当前互联网公司的web server是哪个类型的?(Apache   Nginx   Lighttpd)所以编写此程序。主要统计baidu  Google  alibaba  tencent(qq)  taobao   renren    sina   京东  凡客  yahoo  盛大  网易  搜狐  畅游  等公司的web server 。当然,也有可能得到的是反向代理服务器。这得具体看公司的后台系统架构了。

 

二.程序

1.源码

vi ab.sh

 

[c-sharp] view plaincopy
 
  1. #!/bin/bash  
  2. #author         longxibendi   
  3. #blog           http://blog.csdn.net/longxibendi  
  4. #function       get the  web servers information of companies'  
  5. if [ -e  /tmp/web_server.log  ] ;then  
  6.      rm  -f  /tmp/web_server.log  
  7. fi  
  8. if [ -e  /tmp/ab.log ] ; then  
  9.      rm  -f   /tmp/ab.log  
  10. fi  
  11. for company_name in   taobao  360buy   dangdang  vancl  mbaobao alibaba google  baidu  yahoo  sina   sohu   ren  
  12. ren   changyou wanmei  sdo 126 163 139   
  13. do  
  14.    {  
  15.      ab -n 1 -c 1   http://www.${company_name}.com/index.html  | tee  /tmp/ab_${company_name}.log  2>&1  
  16.      if [ $? -eq 0  ] ; then  
  17.        printf  "%15s,   %20s/n"    "${company_name}.com",   " ` cat  /tmp/ab_${company_name}.log | grep "Server  
  18.  Software" `  " >> /tmp/web_server.log  
  19.      fi  
  20.      sleep  2  
  21.    }&  
  22. done  
  23. wait    
  24. echo "It is  OK "  
 

 

 

2.说明&执行程序

通过ab  网站主页,然后把得到信息重定向到 一个文件中,然后再从该文件中过滤信息,统一记录到 /tmp/web_server.log中

 

[c-sharp] view plaincopy
 
  1. [root@localhost Desktop]# chmod a+x ab.sh  
  2. [root@localhost Desktop]# ./ab.sh > /dev/null  
 

 

 

 

3.执行结果,通过查看 /tmp/web_server.log 得到网站web server信息(有可能是反向代理服务器)

 

[c-sharp] view plaincopy
 
  1. [root@localhost Desktop]# cat /tmp/web_server.log  
  2.     360buy.com,,    Server Software:        jdws    
  3.     taobao.com,,    Server Software:        nginx    
  4.    mbaobao.com,,    Server Software:            
  5.      baidu.com,,    Server Software:        BWS/1.0    
  6.     renren.com,,    Server Software:        nginx/0.7.67    
  7.     google.com,,    Server Software:        gws    
  8.   changyou.com,,    Server Software:        Apache    
  9.       sina.com,,    Server Software:        Apache/2.0.63    
  10.    alibaba.com,,    Server Software:        Apache/2.2.15    
  11.        126.com,,    Server Software:        Apache    
  12.      vancl.com,,    Server Software:        Microsoft-IIS/6.0    
  13.     wanmei.com,,    Server Software:        nginx    
  14.        sdo.com,,    Server Software:        SVS    
  15.       sohu.com,,    Server Software:        SWS    
  16.   dangdang.com,,    Server Software:        nginx/0.7.61    
  17.      yahoo.com,,    Server Software:        YTS/1.20.0    
  18.        139.com,,    Server Software:        Apache    
  19.        163.com,,    Server Software:        nginx    
 

 

 

4.简单分析结果

sohu.com       用的 web server 是 SWS  (这个有可能是自己开发的,有可能是用的SWS)

google.com    用的 web server 是 gws  

baidu.com      用的 web server 是 BWS

taobao.com   用的 web server 是 nginx

还有很多,看3的结果

 

 

三.伪多线程与单进程脚本对比

1.先看单进程的程序

vi  1_ab.sh

 

[c-sharp] view plaincopy
 
  1. #!/bin/bash  
  2. #author         longxibendi   
  3. #blog           http://blog.csdn.net/longxibendi  
  4. #function       get the  web servers information of companies'  
  5. if [ -e  /tmp/web_server.log  ] ;then  
  6.      rm  -f  /tmp/web_server.log  
  7. fi  
  8. if [ -e  /tmp/ab.log ] ; then  
  9.      rm  -f   /tmp/ab.log  
  10. fi  
  11. for company_name in   taobao  360buy   dangdang  vancl  mbaobao alibaba google  baidu  yahoo  sina   sohu   renren   changyou wanmei  sdo 126 163 139   
  12. do  
  13.  #  {  
  14.      ab -n 1 -c 1   http://www.${company_name}.com/index.html  | tee  /tmp/ab.log  2>&1  
  15.      if [ $? -eq 0  ] ; then  
  16.        printf  "%10s,   %20s/n"    "${company_name}.com",   " ` cat  /tmp/ab.log | grep "Server Software" `  " >> /tmp/web_server.log  
  17.      fi  
  18. #     sleep  3  
  19.  #  }&  
  20. done  
  21. wait    
  22. echo "It is  OK "  
 

 

 

2.说明

上面的单进程程序,只用一个进程实现,很简单就不解释了。具体统计web server实现与伪多线程程序类似

 

3.两程序执行效率对比

 

[c-sharp] view plaincopy
 
  1. [root@localhost Desktop]# time -p ./1_ab.sh > /dev/null  
  2. real 12.54  
  3. user 0.03  
  4. sys 1.74  
  5. [root@localhost Desktop]# time -p ./ab.sh > /dev/null  
  6. real 11.20  
  7. user 0.07  
  8. sys 2.00  
 

 

 

4.分析

可以看到 伪多线程程序明显比单进程程序快

其实,如果在执行 ab.sh的时候可以 ps -ef | grep  ab.sh一下,就会发现不同了

 

[c-sharp] view plaincopy
 
  1. [root@localhost ~]# ps -ef | grep  ab.sh  
  2. root     26846  5761  1 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  3. root     26854 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  4. root     26857 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  5. root     26859 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  6. root     26865 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  7. root     26871 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  8. root     26874 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  9. root     26875 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  10. root     26886 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  11. root     26889 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  12. root     26892 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  13. root     26895 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  14. root     26898 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  15. root     26899 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  16. root     26916 26846  0 02:15 pts/1    00:00:00 /bin/bash ./ab.sh  
  17. root     26952 22657  0 02:15 pts/3    00:00:00 grep ab.sh  
 

 

可以发现: 伪多线程程序 ab.sh 是 由 进程号为  26846 的进程生成一些子进程,这些子进程来执行,完成统计工作。

而单进程程序 1_ab.sh  是由 一个进程自己完成所有工作。

 

如果仔细阅读上述程序  ab.sh   和   1_ab.sh   ,会发现另一个不同点,1_ab.sh 只需要写入两个文件 ,而 ab.sh 则需要写入19个文件,这是因为ab.sh每个子进程自己维护一个  ab_$company_name.log文件,也就是说,每一次 ab 一个 网址,比如 baidu 那么这次执行结果就 重定向到 ab_baidu.com.log中。这样 ab.sh 要打开的文件次数比1_ab.sh多。所以下面总结一下这两程序的不同点。

5.ab.sh与1_ab.sh不同

其一.ab.sh是伪多线程的(一个母进程,多个子进程),而 1_ab.sh是单一进程  所以ab.sh是不是应该比1_ab.sh快呢?是不是可以实现并行处理呢?

 

其二. ab.sh打开的文件多,需要写入的文件多,而1_ab.sh写入的文件少。是不是1_ab.sh连续写的数据多,ab.sh随机写的数据多?那为什么ab.sh比1_ab.sh快呢?

 

声明:本文档可以随意更改,但必须署名原作者

作者:凤凰舞者 qq:578989855

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容