运维面试题(二)

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

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

#!/bin/bash

# 系统函数(yes、no之类的)
. /etc/init.d/functions

url_list=(
https://www.baidu.com
https://www.4399.com
https://www.qq.com
http://www.youtube.com
)


function chech_url() {
echo "checking..."
for ((i=0; i<`echo ${#url_list[*]}`; i++))
do
judge=($(curl -I -s ${url_list[$i]}|head -1|tr "\r" "\n"))
if [[ "${judge[1]}" == '200' && "${judge[2]}" == 'OK' ]]
        then
        action "${url_list[$i]}" /bin/true
else
        action "${url_list[$i]}" /bin/false
fi
done
}

chech_url

posted @ 2021-02-19 16:33  带着泥土  阅读(91)  评论(0编辑  收藏  举报