从一个host列表或者IP列表中对给定的URL进行遍历-shell

#! /bin/sh
# 由于数组在shell函数中传递实在蛋疼,尤其是还包括其他参数时,所以干脆变量全部用全局变量

random_v=$RANDOM
trap "rm -f /tmp/$$.$random_v.http_code.out" EXIT
function curl_url(){
        url_line="http://$url_host/$line"
        /usr/bin/curl -s -o /dev/null --connect-timeout $time_out -m $time_out -w %{http_code} $url_line > /tmp/$$.$random_v.http_code.out
        return $?
}

function deal_url(){
        error_status=7
        n=0
        max_fail=30
        ips_length=${#ips[@]}
        url_host=${ips[$n]}
        cat $urls_txt | while read line;
        do
                curl_url
                if [ $? -eq $error_status ];then
                        echo "当前IP:${url_host}无法连通,尝试下一个IP连接$line"
                        if [ $n -lt $max_fail ];then
                                n=$(( $n+1 ))
                                visit_ip=$(( $n % $ips_length )) #应该使用哪一个ips数组下标
                                url_host=${ips[$visit_ip]}
                                curl_url
                                while [ $? -eq $error_status ]
                                do
                                        echo "当前IP:${url_host}无法连通,尝试下一个IP连接$line"
                                        n=$(( $n+1 ))
                                        visit_ip=$(( $n % $ips_length ))
                                        url_host=${ips[$visit_ip]}
                                        if [ $n -ge $max_fail ];then
                                                echo "尝试次数达到最大值,放弃连接"
                                                exit
                                        fi
                                        curl_url
                                done
                        else
                                echo "尝试次数达到最大值,放弃连接"
                                exit
                        fi
                fi
                echo "通过IP:${url_host}完成对${line}的访问,返回状态为$(cat /tmp/$$.$random_v.http_code.out)"
        done
}

work_dir=''
cd $work_dir
ips=( '10.12.12.11:8090' '10.12.12.12:8080' '10.12.12.13:8080' '10.12.12.14:8080' '10.12.12.15:8080' )
urls_txt="$work_dir/url.list"
time_out=5
deal_url
一次输出可能如下所示:
当前IP:10.120.12.11:8090无法连通,尝试下一个IP连接/
当前IP:10.120.12.12:8080无法连通,尝试下一个IP连接/
当前IP:10.120.12.13:8080无法连通,尝试下一个IP连接/
通过IP:10.120.12.14:8080完成对/的访问,返回状态为200
通过IP:10.120.12.14:8080完成对/a.html的访问,返回状态为200
通过IP:10.120.12.14:8080完成对/b.html的访问,返回状态为200
通过IP:10.120.12.14:8080完成对/c.html的访问,返回状态为200
"""
此时14中断
"""
当前IP:10.120.12.14:8080无法连通,尝试下一个IP连接/
通过IP:10.120.12.15:8080完成对/d.html的访问,返回状态为200
通过IP:10.120.12.15:8080完成对/e.html的访问,返回状态为200
通过IP:10.120.12.15:8080完成对/f.html的访问,返回状态为200
通过IP:10.120.12.15:8080完成对/g.html的访问,返回状态为200
通过IP:10.120.12.15:8080完成对/h.html的访问,返回状态为200
""""
此时15中断,13恢复
"""
当前IP:10.120.12.15:8080无法连通,尝试下一个IP连接/
当前IP:10.120.12.11:8090无法连通,尝试下一个IP连接/
当前IP:10.120.12.12:8080无法连通,尝试下一个IP连接/
通过IP:10.120.12.13:8080完成对/i.html的访问,返回状态为200
通过IP:10.120.12.13:8080完成对/j.html的访问,返回状态为200

最大无法连通次数由max_fail变量控制,达到最大失败次数,脚本终止

转载于:https://my.oschina.net/u/273053/blog/111627

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值