windows服务器修改远程登录的端口+防火墙配置

 

话不多说,一个bat文件,快速修改注册表端口。你只需要做的是另外防火墙添加例外端口后重启即可。经测试2008-2012-2016-2019正常使用!

 

@echo off
rem 查找端口号
for /f "skip=4 tokens=3 delims= " %%i in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber') do (set value16=%%i)
set value=%value16:~2%
rem echo %value%
setlocal EnableDelayedExpansion
set "str=0123456789ABCDEF"
for /l %%a in (0 1 15) do (
 set -%%a=!str:~%%a,1!
 set -!str:~%%a,1!=%%a
)
for /l %%a in (0 1 100) do (
if not "!value:~%%a,1!"=="" (
call set sun=%%-!value:~%%a,1!%% !sun!
))
rem echo !sun!
set /a nnn=1,num=0
for %%a in (!sun!) do (
rem set num0=!num!
set /a num=%%a*nnn+num
rem echo num = %%a*!nnn! + !num0! = !num!
set /a nnn*=16
)
rem echo 当前远程桌面端口号为十六进制: %value16% 即 %value%
echo 修改后重启系统生效,记得把新端口加入防火墙.
rem setlocal DisableDelayedExpansion
rem 以下是系统默认
goto xiugai
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List" /v 3389:TCP /t REG_SZ /d 3389:TCP:*:Enabled:@xpsp2res.dll,-22009 /f
:xiugai
set PortNumber=
set /p PortNumber=输入即将修改的十进制端口号(1024-65535):
rem if "%PortNumber%"=="" exit
if not defined PortNumber (set /a PortNumber=3389 & echo 未输入端口号,即将修改为默认值!PortNumber! & pause)
echo %PortNumber%|findstr "^[0-9]*$" >nul || (echo 必须纯数字! & goto xiugai)
if %PortNumber% lss 1024 (echo 端口号不能小于1024 & goto xiugai)
if %PortNumber% gtr 65535 (echo 端口号不能大于65535 & goto xiugai)
echo %PortNumber%
rem 修改端口后开启
rem 总开关
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
rem 修改端口号,默认会直接修改十进制值,注意:范围在1024到65535,而且不能冲突,事实上,只修改第二处WinStations那一个也是可以的。
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp" /v PortNumber /t REG_DWORD /d %PortNumber% /f
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber /t REG_DWORD /d %PortNumber% /f
@reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List /v !num!:TCP /f
@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List" /v %PortNumber%:TCP /t REG_SZ /d %PortNumber%:TCP:*:Enabled:@xpsp2res.dll,-22009 /f

 

复制我上述代码后新建文本txt粘贴进去,将文件命名为xx.bat文件即可,然后双击文件。输入自定义端口,比如6679,然后回车,把6679放行防火墙然后重启即可。

 

posted @ 2019-06-20 09:44  疯刘小三  阅读(2068)  评论(0编辑  收藏  举报