4

VPS 路由测试脚本

 1 year ago
source link: https://johnrosen1.com/2022/04/18/route/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VPS 路由测试脚本

因为众所周知,我这个人极其懒,所以 VPS 路由测试这种事情怎么能让我手动干呢,因此就有了这个脚本。


为什么需要知道VPS路由

主要可以拿来评测VPS的线路质量。

#!/usr/bin/env bash

## 路由追踪模组

set +e

## 安装traceroute
apt-get -y install traceroute

## 路由ASN
routes=(AS4134 AS4837 AS9808 AS4538 AS4809 AS9929 AS2914 AS2497 AS2516 AS4725 AS3491 AS9269 AS4635 AS4760 AS58453 AS4637 AS64050 AS6939 AS174 AS3356 AS3257 AS6461 AS701 AS7018 AS1239 AS1299 AS6453 AS6830 AS5511 AS6762 AS3320)

## ASN对应的ISP名称
routes_name=(163 169 CMNET CERNET CN2 CU-VIP NTT IIJ KDDI SoftBank PCCW HKBN HKIX HKT CMI Telstra BGPNET HE Cogent LEVEL3 GTT Zayo Verizon ATT T-Mobile Arelion TATA Liberty Orange SPARKLE Deutsche)

## 路由测试目的地IP(对应电信,联通,移动,国际)
ct_ip="116.228.111.118"
cu_ip="210.22.70.3"
cm_ip="211.136.112.50"
inet_ip="1.1.1.1"

## 使用ipinfo.io获取ip对应的ASN
ipinfo_token="56c375418c62c9"

route_test(){

TERM=ansi whiptail --title "路由测试" --infobox "路由测试,请耐心等待。" 7 68

## 测试路由并汇总结果
traceroute ${ct_ip} -n -T -m 30 -w 2 | tail -n +2 &> route_all.txt
traceroute ${cu_ip} -n -T -m 30 -w 2 | tail -n +2 &>> route_all.txt
traceroute ${cm_ip} -n -T -m 30 -w 2 | tail -n +2 &>> route_all.txt
traceroute ${inet_ip} -n -T -m 30 -w 2 | tail -n +2 &> route_all.txt

ips=()
asns=()
route_vps=()

## 过滤出IP
while IFS="" read -r p || [ -n "$p" ]
do
ip=$(printf '%s\n' "$p" | cut -d' ' -f4)
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
ips+=( $ip )
fi
done < route_all.txt

## 获取IP对应的ASN
for ip in "${ips[@]}"; do
asns+=($(curl --retry 5 -s https://ipinfo.io/${ip}/org?token=${ipinfo_token} --connect-timeout 300 | cut -d' ' -f1 ))
if [[ $ip = 59.43* ]] ; then
route_vps+=(AS4809)
fi
done

## 与预定义列表比对,找出路由相关ASN
for asn in "${asns[@]}"; do
for route in "${routes[@]}"; do
if echo "$asn" | grep -q "$route"; then
route_vps+=($route)
fi
done
done

rm route_all.txt
rm route.txt &> /dev/null

## 将ASN映射为预定义的ISP名称
for i in "${!route_vps[@]}"; do
for o in "${!routes[@]}"; do
if [[ "${route_vps[$i]}" == "${routes[$o]}" ]]; then
echo "${routes_name[$o]}" >> route.txt
fi
done
done

## 结果去重
route_final=$(cat route.txt | sort | uniq | tr '\n' ' ')

## 输出结果
echo $route_final

rm route.txt
}
## 执行测试
route_test

下载脚本并执行

curl --retry 5 -LO https://raw.githubusercontent.com/johnrosen1/vpstoolbox/master/install/route.sh
source route.sh
route_test

等待输出结果

169 CMNET IIJ Telstra                                                     
root@cloud:~#

有这个脚本,我总算可以知道我随手买的垃圾 VPS 是什么垃圾了,2333。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK