找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 2156|回复: 11

[openwrt(x86)] wireguard自动重连

[复制链接]
由于家里宽带的 ip 经常变动所以我 endpoint 写的是域名,这就导致了路由器重启后,ip改变,windows客户端就连不上了,他们又不会自动重新链接,需要手动断开一次,很麻烦。
网上找了一个ps1脚本,可运行起来有错误提示,请高手帮看看。
写一个 ps1 脚本
```powershell
$profileName = ""
$servicesName = "WireGuardTunnel`$$profileName"

$log = wireguard.exe /dumplog | Select-String -Pattern $profileName | Select-Object -Last 10 | Select-String -Pattern retrying
if ($log.Length -gt 3) {
Write-Output $log
Stop-Service $servicesName
Start-Sleep -Seconds 5
Start-Service $servicesName
}
```
然后让它 5 分钟跑一次
 楼主| | 显示全部楼层
起先运行是报这个错误: wireguard.exe : 无法将“wireguard.exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路
径,请确保路径正确,然后再试一次。
添加路径  c:\wireguard.exe 后,又报另一个错误:Select-String : 无法将参数绑定到参数“Pattern”,因为该参数为空字符串。
所在位置 行:1 字符: 59
+ ... \wireguard.exe /dumplog | Select-String -Pattern $profileName
回复

使用道具 举报

 楼主| | 显示全部楼层
折腾了一天,终于把这个问题解决了,分享给大家。
$profileName = "1222"     //这里要填写你的conf文件的名称
$servicesName = "WireGuardTunnel`$$profileName"
c:\wg\wireguard.exe /dumplog c:\wg\1.log    //将日志读取到1.log中
$log = Get-Content -Path "c:\wg\1.log"         //将文件读取到$log中
Write-Output $log.length
$log = $log | Select-String -Pattern $profileName | Select-Object -Last 10 | Select-String -Pattern retrying
   //读取含有1222的行,读取最后10行,读取含有retrying的行
Write-Output $log.length
if ($log.Length -gt 3) {                如果行数大于3
Write-Output $log
Stop-Service $servicesName
Start-Sleep -Seconds 5
Start-Service $servicesName
}
回复

使用道具 举报

对啊 路由器和路由器也不能重连
回复

使用道具 举报

我的也是 ddns ip变了之后就不会重连了  这个怎么用啊
回复

使用道具 举报

 楼主| | 显示全部楼层
发现wireguard挂掉之后,log读取的是这些:
2023-11-25 02:25:07.416817: [TUN] [1222] Interface closed
2023-11-25 02:25:07.416817: [TUN] [1222] Shutting down
2023-11-25 02:25:12.594329: [TUN] [1222] Starting WireGuard/0.0.38 (Windows 10.0.22000; amd64)
2023-11-25 02:25:12.594329: [TUN] [1222] Watching network interfaces
2023-11-25 02:25:12.603336: [TUN] [1222] Resolving DNS names
2023-11-25 02:25:24.659218: [TUN] [1222] Unable to resolve one or more DNS hostname endpoints: No such host is known.
2023-11-25 02:25:24.660221: [TUN] [1222] Shutting down
2023-11-25 02:36:09.045667: [MGR] Update checker: Get https://download.wireguard.com/windows-client/latest.sig: dial tcp: lookup download.wireguard.com: no such host
2023-11-25 03:01:30.110564: [MGR] Update checker: Get https://download.wireguard.com/windows-client/latest.sig: dial tcp 108.160.169.186:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
2023-11-25 03:26:51.191527: [MGR] Update checker: Get https://download.wireguard.com/windows-client/latest.sig: dial tcp 199.59.148.147:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
2023-11-25 03:52:16.250130: [MGR] Update checker: Get https://download.wireguard.com/windows-client/latest.sig: dial tcp 162.125.32.12:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
2023-11-25 04:17:37.297481: [MGR] Update checker: Get https://download.wireguard.com/windows-client/latest.sig: dial tcp 157.240.11.40:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
2023-11-25 04:42:58.404771: [MGR] Update checker: Get https://download.wireguard.com/windows-client/latest.sig: dial tcp 128.242.240.157:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
回复

使用道具 举报

 楼主| | 显示全部楼层
因此修改脚本中的关键字 retrying 为 Update, gt 3 改为 gt 1,当行数大于1行时。 继续进行测试。
回复

使用道具 举报

 楼主| | 显示全部楼层
Q啊维 发表于 2023-5-8 09:48
我的也是 ddns ip变了之后就不会重连了  这个怎么用啊

使用方法 :

右键我的电脑-管理-任务计划程序-创建任务
常规:不管用户是否登录都要运行, 使用最高权限运行
触发器:重复任务间隔5分钟,持续时间无期限
操作:程序 c:\...\powershell.exe   参数 c:\wg\1.ps1
设置:如果过了计划开始时间,立即启动任务。

2. 在管理员权限下打开 PowerShell 控制台,运行以下命令来更改执行策略为 Unrestricted(允许在本地计算机上运行任何脚本):

```
Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy RemoteSigned
回复

使用道具 举报

 楼主| | 显示全部楼层
无法加载文件 C:\wg\1.ps1,因为在此系统上禁止运行脚本。
这个错误通常是由于 PowerShell 执行策略(Execution Policy)限制所致。PowerShell 执行策略是一种安全措施,用于防止在计算机上运行未知来源的脚本。如果默认的执行策略是 Restricted,则无法运行任何脚本。

要解决此问题,可以通过以下方法之一来更改 PowerShell 执行策略:

1. 打开 PowerShell 控制台,运行以下命令来更改执行策略为 RemoteSigned(允许在本地计算机上运行自己签名的脚本和远程计算机上下载的脚本):

```
Set-ExecutionPolicy RemoteSigned
```

2. 在管理员权限下打开 PowerShell 控制台,运行以下命令来更改执行策略为 Unrestricted(允许在本地计算机上运行任何脚本):

```
Set-ExecutionPolicy Unrestricted
```

请注意,更改执行策略可能会影响计算机的安全性。如果您不希望在计算机上运行未知来源的脚本,请将执行策略更改回 Restricted 等更安全的策略。
回复

使用道具 举报

 楼主| | 显示全部楼层
$log = $log  | Select-Object -Last 10 | Select-String -Pattern Update
Write-Output $log.length
if ($log.Length -gt 1) {
Write-Output $log
Stop-Service $servicesName
Start-Sleep -Seconds 5
Start-Service $servicesName
}
修改脚本,继续进行测试
回复

使用道具 举报

 楼主| | 显示全部楼层
Write-Output $log.length
$log1 = $log | Select-String -Pattern $profileName | Select-Object -Last 10 | Select-String -Pattern retrying    //出现retrying 的行数
$log2 = $log  | Select-Object -Last 10 | Select-String -Pattern Update                    //出现Update 的行数
Write-Output $log1.length
Write-Output $log2.length
Write-Output $log.length
if (($log1.Length -gt 2) -or ($log2.Length -gt 1) ) {                   //出现retrying的行数大于2 或者 出现Update的行数大于1 时
Write-Output $log
Stop-Service $servicesName
Start-Sleep -Seconds 5
Start-Service $servicesName
}
回复

使用道具 举报

tomcatt111 发表于 2023-11-25 21:30
使用方法 :

右键我的电脑-管理-任务计划程序-创建任务

我没用win客户端 我用的op客户端win电脑在op下面
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-5-17 02:34

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表