以下是一个用于修改远程桌面端口(RDP)的批处理脚本(.bat)示例。这个脚本会更改Windows防火墙规则、修改远程桌面配置以及重新启动相关的服务。请注意,运行此脚本需要管理员权限,并且在执行之前,请确保你了解修改端口可能带来的安全风险。
@echo off
:: Batch Script to Change Remote Desktop Port
:: Define the new RDP port number
set NEW_RDP_PORT=33899 :: Replace 33899 with your desired port number
:: Check if the script is run as Administrator
net session >nul 2>&1
if %errorlevel% neq 0 (
echo This script must be run as Administrator.
echo Right-click the script and select Run as administrator.
pause
exit /b 1
)
:: Enable Remote Desktop(optional, ensure its enabled)
reg add HKLMSystemCurrentControlSetControlTerminal Server /v fDenyTSConnections /t REG_DWORD /d 0 /f
:: Configure the new port in Terminal Services Configuration
set TSCONFIG=%windir%System32tsconfig.msc
if not exist %TSCONFIG%(
echo tsconfig.msc not found. This script requires Remote Desktop Configuration tool.
pause
exit /b 1
)
:: Use tsconfig.msc to change the port(this step usually requires manualinteraction)
echo Opening Remote Desktop Configuration tool to change the port...
start %TSCONFIG% /s
:: Wait for user to manually change the port using tsconfig.msc
echo Please configure the new RDPport (%NEW_RDP_PORT%) in the Remote Desktop Configuration tool.
echo Once done, press any key to continue with firewall and service configuration...
pause >nul
:: Configure Windows Firewall to allow the new RDP port
netsh advfirewall firewall add rule name=Remote Desktop - New Port dir=in action=allow protocol=TCP localport=%NEW_RDP_PORT% enable=yes
netsh advfirewall firewall add rule name=Remote Desktop - NewPort (UDP - for someconfigurations) dir=in action=allow protocol=UDP localport=%NEW_RDP_PORT% enable=yes
:: Update Remote Desktop listener to use the new port
reg add HKLMSYSTEMCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp /v PortNumber /t REG_DWORD /d %NEW_RDP_PORT% /f
:: Restart Remote Desktop Services
echo Restarting Remote Desktop Services...
net stop TermService
net start TermService
echo Remote Desktop port has been changed to %NEW_RDP_PORT%.
echo Please ensure your firewall and router allow incoming connections to this port.
pause
exit /b 0
注意事项:
1.管理员权限:此脚本必须以管理员权限运行。右键点击脚本并选择“以管理员身份运行”。
2.手动配置:脚本会提示你使用tsconfig.msc手动配置新的RDP端口。这是因为自动化这个步骤通常不可靠,且需要用户确认。
3.防火墙配置:脚本会自动配置Windows防火墙以允许新的RDP端口。
4.服务重启:脚本会重启远程桌面服务以使更改生效。
5.安全性:更改RDP端口可以增加一定的安全性,但不应替代其他安全措施。确保你的防火墙和路由器正确配置,且只有受信任的用户可以访问该端口。
使用步骤:
1. 将上述代码复制到一个新的文本文件中。
2. 将文件保存为`.bat`扩展名,例如`change_rdp_port.bat`。
3. 右键点击该文件并选择“以管理员身份运行”。
4. 根据脚本提示完成操作。
希望这个脚本对你有所帮助!