一个验证代理并自动用选定的代理替换掉脚本中相应代理的脚本

sh/bash/dash/ksh/zsh等Shell脚本
回复
zhuzhzh
帖子: 265
注册时间: 2005-07-28 15:08

一个验证代理并自动用选定的代理替换掉脚本中相应代理的脚本

#1

帖子 zhuzhzh » 2006-08-21 21:50

申明:我参考了论坛里另外两人的脚本

我的环境是这样的:
一、能直接访问外面,但访问国内不要钱,访问国外按流量1RMB/1M
二、我写了个代理脚本(假设名叫getproxy.sh),判断国内的直接访问,国外的通过代理

代理脚本的名字叫proxy.pac,和下面的验证脚本在同一目录
在proxy.pac中,有一句为:

代码: 全选

var ProxySvr="PROXY 233.11.2.3:80"        
#其中数字部分是我随便写的,不是真实的代理

我把代理按快慢保存在文件ptime.txt中,你可以通过

代码: 全选

./getproxy -s -2
来直接用其中第二快的代理替换proxy.pac中的代理 #就不验证了

也可以通过

代码: 全选

./getproxy -3 "www.opera.com"
来选择先验证,然后用验证好的第三快的代理替换proxy.pac中的代理 #有人说为什么不用最快的?因为有可能比较快的是假代理,以为我经常遇到这种情况,而一般第二或第三快的都没有问题(不能保证)

代码: 全选

#!/bin/bash
#Copyright (c) 2005 
#Auther: zhuzhzh@gmail.com

usage(){
    echo "usage 1:`basename $0` [-<digit number>] [<website link>]"
    echo "example:`basename $0` -1 \"www.opera.com\" "
    echo "usage 2:`basename $0` [-s] [-<digit number>]"
    echo "example:`basename $0` -s -2"
}

debug=1
TIME_FILE="ptime.txt"
TEMP_FILE="ptemp.txt"

getproxy(){

    wget -O proxy.html -q -i weblist.txt
    if [ $? = 1 ]; then
        exit 1
    fi

    PROXIES=`grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:[0-9]\{1,5\}" proxy.html|sort -u -n`

    WGET_COM="wget --cache=off -q -T 20 -t 1 -w 0 -O /dev/null $CHECKWEB"

    if [ "$debug" = "1" ]; then
        echo $WGET_COM
        echo $PROXIES    
    fi

    #>$TIME_FILE
    #>$TEMP_FILE

    if [ -e proxy.html ];then
        rm proxy.html
    fi

    OLD_PROXY=$http_proxy

    for proxy in $PROXIES ; do
        echo "Testing http://$proxy..."
        export http_proxy="http://$proxy"

        >$TIME_FILE
        /usr/bin/time -o $TEMP_FILE -f %e $WGET_COM

        if [ "$?" = 0 ] ; then
            read TEMPTIME < $TEMP_FILE
            echo -n -e "$proxy\t$TEMPTIME"  >> $TIME_FILE 
            echo -e "The time spent is: $TEMPTIME"
        else
            echo "This is a pseudo proxy!"
        fi
    done 

    if [ -e $TEMP_FILE ]; then
        rm $TEMP_FILE
    fi

    export http_proxy=$OLD_PROXY

    #sort  -n +1 < proxy_test.txt > proxy_result.txt
    # -t : define seprerator    -n : sort as number   +1 : sort field 1 (count from 0)
}

substitute(){
    if [ -e $TIME_FILE ]; then
        BEST_PROXY=`sort -n +1 $TIME_FILE |awk '{if(NR=="'$NUM'")print $1}'`
    fi

#    if [ -e $TIME_FILE ]; then
#        rm $TIME_FILE
#    fi
    if [ -n $BEST_PROXY ]; then
        sed -n -i "s/\(var ProxySvr=\"PROXY \)[0-9.:]*\(\";\)/\1$BEST_PROXY\2/p" proxy.pac
    fi

    exit 0
}

if [ $# -gt 2 ]; then
    usage
    exit 1
fi

if [ $# -eq 2 ]; then
    if [ $1 = "-s" ]; then
       if [ -e $TIME_FILE ]; then
            NUM=`cut -c2, $2`
            substitue
       fi
    else
        if [ $2 = "-s" ]; then
            NUM=`cut -c2, $1`
            substitue
        else
            first=`cut -c1 $1`
            if [ "$first" = "-" ]; then
                NUM=`cut -c2, $1`
                CHECKWEB=$2
                getproxy
                substitue
            fi
        fi
    fi
fi

if [ $# -eq 1 ]; then
    first=`cut -c1 $1`
    if [ "$first" -eq "-" ]; then
        NUM=`cut -c2, $1`
        CHECKWEB="www.yahoo.com"
        getproxy
        substitute
    else
        NUM=1
        CHECKWEB=$2
        getproxy
        substitute
    fi
fi

if [ $# -eq 0 ]; then
    NUM=1
    CHECKWEB="www.yahoo.com"
    getproxy
    substitute
fi
无声胜有声
回复