【问题标题】:Reconnect wireguard to another VPN server if ping fails?如果 ping 失败,将wireguard 重新连接到另一个 VPN 服务器?
【发布时间】:2021-05-28 08:21:57
【问题描述】:

我正在使用 WireGuard 连接到 Mullvads(= vpn 服务)vpn 服务器。我总共使用了 5 种不同的wireguard vpn 配置(vpn 服务器*)。称为 server1、server2、server3、server4、server5。

所以要连接到 server3,我运行 wg-quick up server3,并断开与 当前服务器的连接 - server3 - wg-quick down server3

有时 vpn 服务器出现故障。我想要几乎立即重新连接到 5 台可用服务器中的另一台。我该怎么做才能自动完成这项工作?

crontab:

*/3 * * * * ping -c4 google.com || wg-command-here?

因此每 3 分钟 google.com 就会被 ping 一次。如果失败,那么它将运行wg-command-here。哪个会是什么=D?

所以每当 ping 失败 => 与 当前wireguard 服务器断开连接 => 连接到总共 5 个可用服务器中的另一个(随机,但 最好 不是它当前连接的那个出于显而易见的原因)。

有没有人有一个简洁的脚本可以解决这个问题?我在 Google 上搜索了 2 天,根本没有找到该死的解决方案。有可能是什么鬼:o。我的编程知识为零,所以我真的希望在这里有一个复制粘贴解决方案。

【问题讨论】:

    标签: bash shell scripting cron


    【解决方案1】:

    您需要运行一个脚本来不时检查连接 当服务器在线时脚本 ping 服务器它连接到 wiregaurd vpn 并不时检查连接的服务器如果连接的服务器 ping 失败 3 次它通过 ping 检查另一台服务器

    在运行前更改脚本中的 serverip

    脚本

    #!/bin/bash
    server1IP= 84.18.418.64 #change server1 ip or name 
    server2IP= 84.18.418.687 #change server2 ip or name 
    server3IP= 845.158.418.64 #change server3 ip or name 
    server4IP= 44.168.418.64 #change server4 ip or name 
    server5IP= 64.68.418.64 #change server5 ip or name 
    while true
    do
    if /bin/ping -c 1 $server1Ip
        then
                sudo wg-quick up server1
                tries=0
                while [[ $tries -lt 3 ]]
                do
                if /bin/ping -c 1 $server1Ip
                    then
                            tries=0
                            continue
                fi
                tries=$((tries+1))
                done
                sudo wg-quick down server1
    fi
    
    if /bin/ping -c 1 $server2Ip
        then
                sudo wg-quick up server2
                tries=0
                while [[ $tries -lt 3 ]]
                do
                if /bin/ping -c 1 $server2Ip
                    then
                            tries=0
                            continue
                fi
                tries=$((tries+1))
                done
                sudo wg-quick down server2
    fi
    
    if /bin/ping -c 1 $server3Ip
        then
                sudo wg-quick up server3
                tries=0
                while [[ $tries -lt 3 ]]
                do
                if /bin/ping -c 1 $server3Ip
                    then
                            tries=0
                            continue
                fi
                tries=$((tries+1))
                done
                sudo wg-quick down server3
    fi
    
    if /bin/ping -c 1 $server4Ip
        then
                sudo wg-quick up server4
                tries=0
                while [[ $tries -lt 3 ]]
                do
                if /bin/ping -c 1 $server4Ip
                    then
                            tries=0
                            continue
                fi
                tries=$((tries+1))
                done
                sudo wg-quick down server4
    fi
    
    if /bin/ping -c 1 $server5Ip
        then
                sudo wg-quick up server5
                tries=0
                while [[ $tries -lt 3 ]]
                do
                if /bin/ping -c 1 $server5Ip
                    then
                            tries=0
                            continue
                fi
                tries=$((tries+1))
                done
                sudo wg-quick down server5
    fi
    done
    

    将此脚本另存为wiregaurd.sh 或下载 https://anonfiles.com/p3Y94av1u6/wiregaurd_sh

    sudo chmod +x wiregaurd.sh

    sudo sed -i -e 's/\r$//'wiregaurd.sh

    sudo nohup "./wiregaurd.sh" &

    在启动时运行此脚本 把这个放到crontab里

    @reboot [wiregaurd 脚本的路径]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 2021-04-10
      • 1970-01-01
      • 2019-12-19
      • 2014-08-21
      • 2021-07-01
      • 1970-01-01
      • 2015-03-16
      相关资源
      最近更新 更多