代理速度测试脚本
感谢ghostry的脚本,感谢0xcb0.com上的脚本(这域名很奇特),感谢鸟哥,以及无数我没记住的网址。第一次写这么长的脚本……学了不少东西。
用法
三种指定代理的方式,可以结合使用。
- 直接在命令行指定:proxy_test 127.0.0.1:80/127.0.0.1:808
- 输入文件:proxy_test -f filename
- 自动获取proxycn上的最新代理:proxy_test -w
可以指定用来测试的地址(默认是lanuchpad):proxy_test -t http://forum.ubuntu.org.cn
输入文件格式:
127.0.0.1:80 127.0.0.1:808
获取帮助:不加参数或者proxy_test -h
更新:2010-8-24 调整了一下输出格式,修正了点小问题
更新:2010-8-26 果然问题多,又小调整了一下。输出格式还是不好,不过也只能这样了
更新:2010-9-6 小修改,之前对proxycn不能解析出域名表示的地址。
更新:2010-9-14 修正一个BUG,同时可以用-e参数指定仅测试教育网地址
脚本
#! /bin/bash help() { echo -e "\ 用法:proxy_test 代理地址 [选项] 设置代理地址 proxy_test 127.0.0.1:80/127.0.0.1:808 选项 -f 文件名\t指定代理列表文件 -w \t\t测试proxycn上的最新代理 -t 网址\t\t用来测试的网址 -e \t\t只测试教育网地址 -h \t\t显示这个帮助" } get_CERNET() { count=${#proxy_array[*]} for ((i=0;i<$count;i++));do proxy=`echo "${proxy_array[$i]}" | cut -d ":" -f 1` result=`curl -s -d "target=$proxy&Submit=查询&queryType=all" http://www.neu.edu.cn/network/queryip/index.php | iconv -f gbk -t utf8 | grep "不属于 CERNET IP 地址"` if [ -n "$result" ];then unset proxy_array[$i] fi done } parse_proxycn() { wget -q -O $weblist $weburl iconv -f gb18030 -t utf8 $weblist -o $weblist.utf8 proxy_array=(${proxy_array[*]} $( sed -n ' /更新日期/{ s/[^<]*// s/<[^>]*>//g s/ /\n/g s/\n// s/[0-9]*\n\([^.\n ]*\.\)/\1/g s/\n[^0-9][^\n]*//g s/\([0-9]*\.[^\n]*\)\n/\1:/g p } ' $weblist.utf8 )) } speedtest() { echo "总共 ${#proxy_array[*]} 个代理,开始速度测试..." rm -rf $tempout i=1 echo -e "编号\t代理地址\t\t\t速度(KB/s)" for proxy in ${proxy_array[*]};do export http_proxy=http://$proxy wget --dns-timeout=10 --connect-timeout=8 --read-timeout=15 -t 1 -o $logfile $testurl -O $tmpfile speed=`sed -n 's/[^(]*(\([^) ]*\) KB\/s).*/\1/p' $logfile` echo -e "$i\t\c" if [ $speed ];then printf "%-31s %-s\n" $proxy $speed | tee -a $tempout else printf "%-31s %-s\n" $proxy "失败" fi i=$(($i+1)) done } printresult(){ if [ ! -f $tempout ];then echo "所有代理均失败" exit 1 fi echo "测试完毕,请选择: 1 - 输出到$output 2 - 打印前10" rm -rf $output read select case $select in "1") sort -r -n -k 2 $tempout > $output ;; "2") echo -e "代理地址\t\t\t速度(KB/s)";cat $tempout | sort -r -n -k 2 | head -n 10 ;; *) echo "参数错误,结果输出到$output"; sort -r -n -k 2 $tempout > $output ;; esac } weburl="http://www.proxycn.com/cnallproxy/page1.htm" testurl="http://ppa.launchpad.net/wine-cn/ppa/ubuntu/dists/lucid/Release" base=/tmp weblist=$base/weblist tempout=$base/proxy_test.result output=$HOME/proxy_test.result tmpfile=$base/tmpfile logfile=$base/logfile if [ -z $1 ];then help exit 1 fi if [[ $1 != -* ]];then proxy_array=(${proxy_array[*]} $(echo $1 | sed 's/\//\n/g')) shift fi while getopts ":whef:t:" opt; do case $opt in f) proxy_array=(${proxy_array[*]} $(cat $OPTARG)) ;; w) parse_proxycn ;; t) testurl=$OPTARG ;; e) get_CERNET ;; h) help; exit 1 ;; \?) echo "无效选项:-$OPTARG"; exit 1 ;; :) echo "选项 -$OPTARG 需要参数"; exit 1 ;; esac done speedtest printresult
评论
lainme
2011/03/07 11:48 回复
@lzy: 我这里能用。你看看是怎么运行的,要用bash才行。
bash proxy_test
或者
./proxy_test
最好不要使用
sh proxy_test
因为在有些系统,比如Ubuntu,sh是被链接到dash的,有些脚本不能正常运行

添加本页到书籍
请问一下这个脚本确定能用吗 我执行后显示 proxy_test: 15: Syntax error: Bad for loop variable 不知道怎么解决