执行mtr网络路由测试,并为每个路由节点 IP 查询属地信息,最终输出包含 IP 属地的 mtr 测试结果 bash shell脚本

mtr -C -r -n 107.174.51.158 2>/dev/null | while read line; do
    # 提取IP
    npk=$(echo "$line" | grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}")

    if [ -n "$npk" ]; then
        # 生成随机伪造IP
        fake_ip=$(awk -v seed=$RANDOM 'BEGIN{srand(seed);print int(rand()*254)+1 "." int(rand()*254)+1 "." int(rand()*254)+1 "." int(rand()*254)+1}')
        
        # 查询IP归属地(国家-省份)
        location=$(curl -m 10 -s "http://ip-api.com/json/${npk}?lang=zh-CN" | jq -r '.country + "-" + .regionName')
        
        # 输出结果
        echo "$line | $location"
    else
        echo "$line"
    fi
done

命令行版本:

mtr -C -r -n 107.174.51.158 2>/dev/null |while read line;do npk="$(echo "$line" |grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}")";if [ -n "$npk" ];then sdihepe="X-Forwarded-For: $(echo |awk -v A=$(date +%S%N) '{srand();a=int(A*rand())%254+1;srand();b=int(A*rand())%255+1;srand();c=int(A*rand())%253+1;srand();d=int(A*rand())%252+1;printf"%s.%s.%s.%s\n",a,b,c,d}')";ounerilesal="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36";roltsarsn="$(curl -m 60 --retry 3 --header "$sdihepe" -A "$ounerilesal" -sLk -o - "http://ip-api.com/json/${npk}?lang=zh-CN" |jq -r ".country+\"-\"+.regionName")";echo "$line $roltsarsn";else echo "$line";fi;done
Categories: 系统运维