WireGuard——自动更新脚本

前言

每次端口被封之后,都要手动更新端口,尤其重要日子就太频繁,所以写一个shell脚本,来代理每次反复的操作,需要更新的时候,直接运行脚本。

内容

#!/bin/bash

# 设置配置文件路径
config_file="/etc/wireguard/wg0.conf"

# 检查文件是否存在
if [ ! -f "$config_file" ]; then
    echo "错误:配置文件 $config_file 不存在"
    exit 1
fi

# 查找当前的ListenPort
current_port=$(grep -oP 'ListenPort\s*=\s*\K\d+' "$config_file")

if [ -z "$current_port" ]; then
    echo "错误:未找到ListenPort配置"
    exit 1
fi

# 计算新的端口号
new_port=$((current_port + 1))

# 更新配置文件
sed -i "s/ListenPort\s*=\s*$current_port/ListenPort = $new_port/" "$config_file"

echo "ListenPort已更新:$current_port -> $new_port"

wg-quick down wg0 && wg-quick up wg0 
posted @ 2024-10-10 14:47:31 王洋 阅读(39) 评论(0)
发表评论
昵称
邮箱
网址