批量检查多个网站地址是否正常 

    

    要求:shell数组方法实现,检测策略尽量模拟用户访问思路

    http://www.etiantian.org

    http://www.taobao.com

    http://oldboy.blog.51cto.com

    http://10.0.0.7 


    思路:

    1.url列表作为数组

    2.check_url

    3.判断输出。


    解答:

    脚本如下:

[root@db02 oldboy26]# cat ms6.sh 
#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#define array url list
array=(
http://www.etiantian.org
http://www.taobao.com
http://oldboy.blog.51cto.com
http://10.0.0.7 
)
#check_url
wait(){
echo -n "wait 3s"
for((i=0;i<3;i++))
do
  echo -n "."
  sleep 1
done
echo 
}
check_url(){
  wget -T 5 -t 2 --spider $1 &>/dev/null
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then
      action "check $1"  /bin/true
  else
      action "check $1"  /bin/false   
  fi
  return $RETVAL
}
main(){
  wait
  for((i=0;i<${#array[@]};i++))
  do
    check_url ${array[i]}
  done
}
main
验证:
[root@db02 oldboy26]# sh ms6.sh 
wait 3s...
check http://www.etiantian.org                             [  OK  ]
check http://www.taobao.com                                [  OK  ]
check http://oldboy.blog.51cto.com                         [  OK  ]
check http://10.0.0.7                                      [FAILED]
[root@db02 oldboy26]#

    如何实现只检测一个url

[root@db02 oldboy26]# cat ms6-01.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#check_url
wait(){
echo -n "wait 3s"
for((i=0;i<3;i++))
do
  echo -n "."
  sleep 1
done
echo 
}
check_url(){
  wget -T 5 -t 2 --spider $1 &>/dev/null
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then
      action "check $1"  /bin/true
  else
      action "check $1"  /bin/false   
  fi
  return $RETVAL
}
main(){
  wait
    check_url $1
}
main $*
验证:
[root@db02 oldboy26]# sh  ms6-01.sh http://oldboy.blog.51cto.com
wait 3s...
check http://oldboy.blog.51cto.com                         [  OK  ]
[root@db02 oldboy26]# sh  ms6-01.sh http://abc.134.com
wait 3s...
check http://abc.134.com                                   [FAILED]
[root@db02 oldboy26]#