Mullvad 自动连接 bash 脚本

问题描述 投票:0回答:1

我尝试制作一个 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 中偶数位置的每一项是国家/地区的缩写,后面的项目是相应城市的缩写。有时它连接到三台服务器,有时连接到两台服务器,有时只连接到一台服务器。

提前致谢

bash vpn
1个回答
0
投票

假设:

  • OP 的问题是脚本在退出之前处理了一些少量的城市/国家对(即
    for
    循环没有被处理 100 次);如果是这种情况,则当
    else break
    为奇数时,将触发
    randomEventInt
  • 由于
    break
    适用于
    for
    循环(而不是
    if/then/else/fi
    块),因此
    for
    循环不会被处理 100 次

虽然OP可以重新设计逻辑以确保始终生成偶数编号的

randomEventInt
,但我可能会采用不同的方法:

  • 将城市/国家对作为单个单元存储在数组中
  • 生成一个介于 0(数组中第一项的索引)和
    N-1
    (其中
    N
    是数组中的项目数);在OP的情况下,它看起来像
    N=40
  • 提取 arry 条目后,我们将其分为城市和国家部分,并进行
    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
    调用
  • OP 可以根据需要添加额外的代码(例如,
    echo
    调用、错误处理等)
© www.soinside.com 2019 - 2024. All rights reserved.