我尝试制作一个 bash 脚本,每 40 秒自动连接到另一台服务器(Mullvad):
locationList=( "al" "tia" "au" "syd" "at" "vie" "be" "bru" "br" "sao" "bg" "sof" "ca" "tor" "co" "bog" "hr" "zag" "cz" "prg" "dk" "cph" "ee" "tll" "fi" "hel" "fr" "par" "de" "fra" "gr" "ath" "hk" "hkg" "hu" "bud" "ie" "dub" "il" "tlv" "it" "mil" "jp" "tyo" "lv" "rix" "mx" "qro" "nl" "ams" "nz" "akl" "no" "osl" "pl" "waw" "pt" "lis" "ro" "buh" "res" "beg" "sg" "sin" "sk" "bts" "za" "jnb" "es" "mad" "se" "mma" "ch" "zrh" "gb" "lon" "ua" "iev" "us" "dal")
for i in {1..100};
do
randomEvenInt=$((RANDOM % 80))
if ((randomEvenInt % 2 == 0));
then
mullvad connect
randomLocation=${locationList[$randomEvenInt]}
city=${locationList[$((randomEvenInt + 1))]}
echo "Connecting to $randomLocation ..."
mullvad relay set location $randomLocation $city
echo "Connected to Server in $randomLocation, $city"
else
break
sleep 40
fi
done
$locationList 中偶数位置的每一项是国家/地区的缩写,后面的项目是相应城市的缩写。有时它连接到三台服务器,有时连接到两台服务器,有时只连接到一台服务器。
提前致谢
假设:
for
循环没有被处理 100 次);如果是这种情况,则当 else break
为奇数时,将触发 randomEventInt
break
适用于 for
循环(而不是 if/then/else/fi
块),因此 for
循环不会被处理 100 次虽然OP可以重新设计逻辑以确保始终生成偶数编号的
randomEventInt
,但我可能会采用不同的方法:
N-1
(其中
N
是数组中的项目数);在OP的情况下,它看起来像N=40
mullvad
调用实施这种新方法的一个想法:
locationList=( 'tia:al' 'syd:au' 'vie:at' 'bru:be' 'sao:br' 'sof:bg' 'tor:ca' 'bog:co' 'zag:hr' 'prg:cz' 'cph:dk' 'tll:ee' 'hel:fi' 'par:fr' 'fra:de' 'ath:gr' 'hkg:hk' 'bud:hu' 'dub:ie' 'tlv:il' 'mil:it' 'tyo:jp' 'rix:lv' 'qro:mx' 'ams:nl' 'akl:nz' 'osl:no' 'waw:pl' 'lis:pt' 'buh:ro' 'beg:res' 'sin:sg' 'bts:sk' 'jnb:za' 'mad:es' 'mma:se' 'zrh:ch' 'lon:gb' 'iev:ua' 'dal:us' )
count="${#locationList[@]}"
for ((i=1; i<=100; i++))
do
index=$((RANDOM % count))
IFS=':' read -r city country <<< "${locationList[index]}" # split city:country on ':' delimiter, store results in variables 'city' and 'country'
mullvad connect
mullvad relay set location "${country}" "${city}"
sleep 40
done
注意事项:
mullvad
调用;我将把它留给 OP 根据需要验证/修改 mullvad
调用echo
调用、错误处理等)