RustDesk客户端配置修改批处理文件实现一键修改

最近研究RustDesk Server的安装,成功在Windows和Linux下进行了安装,并且现实了内网穿透。为了方便其他人方便安装客户端程序,并进行自定义服务顺设置,写了一个批处理,实现配置文件通过预置IP进行一键修改。

<pre><code class='language-default'>chcp 65001
@echo off
setlocal enabledelayedexpansion

set "CONFIG_FILE=%USERPROFILE%\AppData\Roaming\RustDesk\config\RustDesk2.toml"
set "NEW_IP=192.168.1.99"
set "NEW_KEY=itkIH7MDYJTQ22Gko6GpHaEnF2nVEXyrxXxh7cmhYO8="

if not exist "%CONFIG_FILE%" (
echo 错误: 配置文件 %CONFIG_FILE% 不存在
exit /b 1
)

:: 初始化标记变量
set "found_rendezvous=0"
set "found_options=0"
set "found_key=0"
set "found_api=0"
set "found_relay=0"
set "found_custom=0"
set "inserted_rendezvous=0"

:: 处理现有内容并标记已存在的配置项
(
for /f "tokens=1* delims=:" %%a in ('findstr /n "^" "%CONFIG_FILE%"') do (
set "line=%%b"
if defined line (
rem 替换 rendezvous_server IP
if "!line:rendezvous_server =!" neq "!line!" (
set "new_line=rendezvous_server = '!NEW_IP!:21116'"
set "found_rendezvous=1"

rem 替换 key 值
) else if "!line:key =!" neq "!line!" (
set "new_line=key = '!NEW_KEY!'"
set "found_key=1"

rem 替换 api-server IP
) else if "!line:api-server =!" neq "!line!" (
set "new_line=api-server = 'http://!NEW_IP!:21114'"
set "found_api=1"

rem 替换 relay-server IP
) else if "!line:relay-server =!" neq "!line!" (
set "new_line=relay-server = '!NEW_IP!'"
set "found_relay=1"

rem 替换 custom-rendezvous-server IP
) else if "!line:custom-rendezvous-server =!" neq "!line!" (
set "new_line=custom-rendezvous-server = '!NEW_IP!'"
set "found_custom=1"

rem 检查 [options] 行
) else if "!line:~0,9!" equ "[options]" (
set "found_options=1"
rem 如果 rendezvous_server 不存在,则在此行之前插入
if !found_rendezvous! equ 0 if !inserted_rendezvous! equ 0 (
echo rendezvous_server = '!NEW_IP!:21116'
set "inserted_rendezvous=1"
)
set "new_line=!line!"
) else (
set "new_line=!line!"
)
echo !new_line!
) else (
echo.
)
)

:: 添加其他不存在的配置项
if !found_key! equ 0 (
echo key = '!NEW_KEY!'
)
if !found_api! equ 0 (
echo api-server = 'http://!NEW_IP!:21114'
)
if !found_relay! equ 0 (
echo relay-server = '!NEW_IP!'
)
if !found_custom! equ 0 (
echo custom-rendezvous-server = '!NEW_IP!'
)

:: 如果 [options] 不存在且 rendezvous_server 也不存在
if !found_options! equ 0 if !found_rendezvous! equ 0 (
echo rendezvous_server = '!NEW_IP!:21116'
)
) &gt; "%CONFIG_FILE%.tmp"

move /y "%CONFIG_FILE%.tmp" "%CONFIG_FILE%" &gt;nul

echo 配置更新完成
pause
endlocal</code></pre>

附下载地址:

https://www.softzhan.com/post/102.html

正文完
 0
正安一片瓦
版权声明:本站原创文章,由 正安一片瓦 于2025-06-28发表,共计2020字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码