公网私网脚本守护连接

网络工具下载部署

SoftEther VPN Project - SoftEther VPN Project

脚本守护连接

服务端和客户端部署的脚本一致,自动守护连接,60秒检测一次,连接异常后自动修复

ubuntu系统为英文显示

#!/bin/bash

VPN_SERVER_PATH="/usr/local/vpnserver/vpnserver"
VPN_CLIENT_PATH="/usr/local/vpnclient/vpnclient"
VPN_CLIENT_CMD_PATH="/usr/local/vpnclient/vpncmd"
VPN_CONNECTION_NAME="vpn"

vpn_server_installed=false
vpn_client_installed=false

# 检查 SoftEther VPN Server 是否为可执行文件
if [ -x "$VPN_SERVER_PATH" ]; then
    vpn_server_installed=true
fi

# 检查 SoftEther VPN Client 是否为可执行文件
if [ -x "$VPN_CLIENT_PATH" ]; then
    vpn_client_installed=true
fi

# 检测 SoftEther VPN Server 是否在运行
check_vpn_server_status() {
    if pgrep -x vpnserver > /dev/null; then
        echo "SoftEther VPN Server is running."
    else
        echo "SoftEther VPN Server is not running. Starting it..."
        $VPN_SERVER_PATH start
    fi
}

# 检测 SoftEther VPN Client 是否在运行
check_vpn_client_status() {
    if pgrep -x vpnclient > /dev/null; then
        echo "SoftEther VPN Client is running."
    else
        echo "SoftEther VPN Client is not running. Starting it..."
        $VPN_CLIENT_PATH start
    fi
}

# 检测 VPN 连接是否已建立
check_vpn_connection() {
    if $VPN_CLIENT_CMD_PATH localhost /CLIENT /CMD AccountStatusGet $VPN_CONNECTION_NAME | grep -q "Session Established"; then
        echo "VPN is connected."

        # 检查 VPN 接口是否有 IP 地址
        VPN_IP=$(ip addr show dev vpn_vpn | grep 'inet ' | awk '{print $2}')
        if [ -z "$VPN_IP" ]; then
            echo "VPN interface does not have an IP address. Requesting one..."
            sudo dhclient vpn_vpn
        else
            echo "VPN interface has the IP address: $VPN_IP"
        fi
    else
        echo "VPN is not connected. Connecting..."
        $VPN_CLIENT_CMD_PATH localhost /CLIENT /CMD AccountConnect $VPN_CONNECTION_NAME
    fi
}

while true; do
    if [ "$vpn_server_installed" = true ]; then
        echo "Checking VPN Server status..."
        check_vpn_server_status
    fi

    if [ "$vpn_client_installed" = true ]; then
        echo "Checking VPN Client status..."
        check_vpn_client_status
        check_vpn_connection
    fi

    sleep 60
done

ubuntu系统为中文显示

#!/bin/bash

VPN_SERVER_PATH="/usr/local/vpnserver/vpnserver"
VPN_CLIENT_PATH="/usr/local/vpnclient/vpnclient"
VPN_CLIENT_CMD_PATH="/usr/local/vpnclient/vpncmd"
VPN_CONNECTION_NAME="vpn"

vpn_server_installed=false
vpn_client_installed=false

# 检查 SoftEther VPN Server 是否为可执行文件
if [ -x "$VPN_SERVER_PATH" ]; then
    vpn_server_installed=true
fi

# 检查 SoftEther VPN Client 是否为可执行文件
if [ -x "$VPN_CLIENT_PATH" ]; then
    vpn_client_installed=true
fi

# 检测 SoftEther VPN Server 是否在运行
check_vpn_server_status() {
    if pgrep -x vpnserver > /dev/null; then
        echo "SoftEther VPN Server is running."
    else
        echo "SoftEther VPN Server is not running. Starting it..."
        $VPN_SERVER_PATH start
    fi
}

# 检测 SoftEther VPN Client 是否在运行
check_vpn_client_status() {
    if pgrep -x vpnclient > /dev/null; then
        echo "SoftEther VPN Client is running."
    else
        echo "SoftEther VPN Client is not running. Starting it..."
        $VPN_CLIENT_PATH start
    fi
}

# 检测 VPN 连接是否已建立
check_vpn_connection() {
    if $VPN_CLIENT_CMD_PATH localhost /CLIENT /CMD AccountStatusGet $VPN_CONNECTION_NAME | grep -q "连接完成 (会话建立)"; then
        echo "VPN is connected."

        # 检查 VPN 接口是否有 IP 地址
        VPN_IP=$(ip addr show dev vpn_vpn | grep 'inet ' | awk '{print $2}')
        if [ -z "$VPN_IP" ]; then
            echo "VPN interface does not have an IP address. Requesting one..."
            sudo dhclient vpn_vpn
        else
            echo "VPN interface has the IP address: $VPN_IP"
        fi
    else
        echo "VPN is not connected. Connecting..."
        $VPN_CLIENT_CMD_PATH localhost /CLIENT /CMD AccountConnect $VPN_CONNECTION_NAME
    fi
}

while true; do
    if [ "$vpn_server_installed" = true ]; then
        echo "Checking VPN Server status..."
        check_vpn_server_status
    fi

    if [ "$vpn_client_installed" = true ]; then
        echo "Checking VPN Client status..."
        check_vpn_client_status
        check_vpn_connection
    fi

    sleep 60
done

脚本配置

# 复制脚本保存
sudo vi /usr/local/bin/softether-guard.sh
sudo chmod +x /usr/local/bin/softether-guard.sh
# 脚本配置自启动
sudo vi /etc/systemd/system/softether-guard.service
# 自动配置内容
[Unit]
Description=SoftEther VPN Guard
After=network.target

[Service]
ExecStart=/usr/local/bin/softether-guard.sh
Restart=always
User=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=softether-guard

[Install]
WantedBy=multi-user.target
sudo systemctl start softether-guard
# 使能重启自启动
sudo systemctl enable softether-guard
# 检查重启自启动
sudo systemctl is-enabled softether-guard
sudo systemctl disable softether-guard
sudo systemctl status softether-guard
sudo systemctl stop softether-guard

# 重新加载配置
systemctl daemon-reload

# 添加日志
#查看日志
sudo journalctl -u softether-guard
# 配置日志
sudo journalctl --vacuum-time=1d
# 清除日志
sudo rm -rf /var/log/journal/*

连接状态