Windows修改远程桌面端口和修改IP地址的工具
侧边栏壁纸
  • 累计撰写 20 篇文章
  • 累计收到 0 条评论

Windows修改远程桌面端口和修改IP地址的工具

247
247
2024-03-27 / 0 评论 / 6 阅读 / 正在检测是否收录...

Windows服务器的默认远程端口号是3389,为了服务器的安全,通常需要修改到其他的端口号,但是修改不好的话,就会出现连不上的问题,因此找了这个。用来修改远程端口号。

下载地址: 点这里
将以下的代码保存到本地,修改后缀名为bat。

@echo off
color 0A
setlocal enabledelayedexpansion


goto select

:select
cls
echo.
echo #########################################
echo ## 输入 1 配置防火墙和开启远程桌面
echo ## 输入 2 设置administrator密码
echo ## 输入 3 修改远程桌面端口号
echo ## 输入 4 配置单个IP
echo ## 输入 5 添加1CIP
echo ## 输入 6 添加4CIP
echo ## 输入 7 添加8CIP
echo ## 输入 q 退出bat
echo #########################################
echo.
set choice=0
set /p choice=请选择您的操作: 
echo.
if  "%choice%"=="1"  goto secset
if  "%choice%"=="2"  goto resetpass
if  "%choice%"=="3"  goto setremoveport
if  "%choice%"=="4"  goto configsingleIP
if  "%choice%"=="5"  goto configmoreip
if  "%choice%"=="6"  goto configmoreip4
if  "%choice%"=="7"  goto configmoreip8
if  "%choice%"=="q"  goto exit
exit

:exit
del %0

:secset
:: 禁用所有入站规则
netsh advfirewall firewall set rule name=all dir=in new enable=no >nul 2>nul
:: 添加阻止TCP和UDP连接的端口(135\137\138\139\445)
netsh advfirewall firewall add rule name="DENY tcp 135" protocol=TCP dir=in localport=135 action=block >nul && (echo DENY tcp 135)
netsh advfirewall firewall add rule name="DENY udp 135" protocol=UDP dir=in localport=135 action=block >nul && (echo DENY udp 135)
netsh advfirewall firewall add rule name="DENY tcp 137" protocol=TCP dir=in localport=137 action=block >nul && (echo DENY tcp 137)
netsh advfirewall firewall add rule name="DENY udp 137" protocol=UDP dir=in localport=137 action=block >nul && (echo DENY udp 137)
netsh advfirewall firewall add rule name="DENY tcp 138" protocol=TCP dir=in localport=138 action=block >nul && (echo DENY tcp 138)
netsh advfirewall firewall add rule name="DENY udp 138" protocol=UDP dir=in localport=138 action=block >nul && (echo DENY udp 138)
netsh advfirewall firewall add rule name="DENY tcp 139" protocol=TCP dir=in localport=139 action=block >nul && (echo DENY tcp 139)
netsh advfirewall firewall add rule name="DENY udp 139" protocol=UDP dir=in localport=139 action=block >nul && (echo DENY udp 139)
netsh advfirewall firewall add rule name="DENY tcp 445" protocol=TCP dir=in localport=445 action=block >nul && (echo DENY tcp 445)
netsh advfirewall firewall add rule name="DENY udp 445" protocol=UDP dir=in localport=445 action=block >nul && (echo DENY udp 445)
:: 允许PING
netsh advfirewall firewall set rule name="文件和打印机共享(回显请求 - ICMPv4-In)" dir=in new enable=yes >nul && (echo 允许 PING)
netsh advfirewall firewall set rule name="文件和打印机共享(回显请求 - ICMPv6-In)" dir=in new enable=yes >nul 2>nul
:: 启用远程桌面
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 0 /f >nul && (echo 开启远程桌面)
:: 防火允许远程连接
netsh advfirewall firewall set rule name="远程桌面(TCP-In)" dir=in new enable=yes >nul && (echo 防火墙允许远程桌面)
netsh advfirewall firewall set rule name="远程桌面 - RemoteFX (TCP-In)" dir=in new enable=yes >nul 2>nul
echo.
pause
goto select

:resetpass
echo Reset administrator password
set pwd=0
set /p pwd=Please enter new password: 
net user administrator %pwd% >nul && (echo 密码设置成功) || (echo 请重新设置密码 && goto resetpass)
echo.
pause
goto select


:setremoveport
echo 设置远程桌面端口号,范围:1024-65535,不能与其他端口冲突
set /p port=请输入要设置的端口号:
netsh advfirewall firewall add rule name="Allow %port%" protocol=TCP dir=in localport=%port% action=allow >nul && (echo 防火墙允许%port%端口)
echo 正在设置远程端口号...
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp" /v PortNumber /t reg_dword /d %port% /f  >nul
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber /t reg_dword /d %port% /f  >nul
ping 127.1 -n 2 >nul
echo 远程端口号设置完成
echo 正在重启远程桌面服务...
net stop UmRdpService >nul 2>nul
net stop TermService >nul
ping 127.1 -n 5 >nul
net start TermService >nul
ping 127.1 -n 2 >nul
echo 远程桌面服务重启完毕
echo.
pause
goto select

:configsingleIP
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "已连接"') do set "ethname=%%b"
echo.
echo 当前已连接状态的网卡名称为:!ethname!
echo 请确认您要配置IP的网卡名称为“!ethname!”后,再继续操作...
echo.
echo 单IP格式示例: 192.168.18.1/27
set /p ip=输入你要配置的IP: 
echo 配置的IP信息如下:
for /f "tokens=1-5 delims=./" %%a in ("!ip!") do (
    set x1=%%a
    set x2=%%b
    set x3=%%c
    set x4=%%d
    set x5=%%e
    set addr=!x1!.!x2!.!x3!.!x4!
    echo Server IP: !addr!
    if !x5! == 29 (set x6=248)
    if !x5! == 28 (set x6=240)
    if !x5! == 27 (set x6=224)
    if !x5! == 26 (set x6=192)
    if !x5! == 25 (set x6=128)
    if !x5! == 24 (set x6=0)
    set netmask=255.255.255.!x6!
    echo NetMask: !netmask!
    if !x5! == 29 if !x4! gtr 0 (set x7=1)
    if !x5! == 29 if !x4! gtr 8 (set x7=9)
    if !x5! == 29 if !x4! gtr 16 (set x7=17)
    if !x5! == 29 if !x4! gtr 24 (set x7=25)
    if !x5! == 29 if !x4! gtr 32 (set x7=33)
    if !x5! == 29 if !x4! gtr 40 (set x7=41)
    if !x5! == 29 if !x4! gtr 48 (set x7=49)
    if !x5! == 29 if !x4! gtr 56 (set x7=57)
    if !x5! == 29 if !x4! gtr 64 (set x7=65)
    if !x5! == 29 if !x4! gtr 72 (set x7=73)
    if !x5! == 29 if !x4! gtr 80 (set x7=81)
    if !x5! == 29 if !x4! gtr 88 (set x7=89)
    if !x5! == 29 if !x4! gtr 96 (set x7=97)
    if !x5! == 29 if !x4! gtr 104 (set x7=105)
    if !x5! == 29 if !x4! gtr 112 (set x7=113)
    if !x5! == 29 if !x4! gtr 120 (set x7=121)
    if !x5! == 29 if !x4! gtr 128 (set x7=129)
    if !x5! == 29 if !x4! gtr 136 (set x7=137)
    if !x5! == 29 if !x4! gtr 144 (set x7=145)
    if !x5! == 29 if !x4! gtr 152 (set x7=153)
    if !x5! == 29 if !x4! gtr 160 (set x7=161)
    if !x5! == 29 if !x4! gtr 168 (set x7=169)
    if !x5! == 29 if !x4! gtr 176 (set x7=177)
    if !x5! == 29 if !x4! gtr 184 (set x7=195)
    if !x5! == 29 if !x4! gtr 192 (set x7=193)
    if !x5! == 29 if !x4! gtr 200 (set x7=201)
    if !x5! == 29 if !x4! gtr 208 (set x7=209)
    if !x5! == 29 if !x4! gtr 216 (set x7=217)
    if !x5! == 29 if !x4! gtr 224 (set x7=235)
    if !x5! == 29 if !x4! gtr 232 (set x7=233)
    if !x5! == 29 if !x4! gtr 240 (set x7=241)
    if !x5! == 29 if !x4! gtr 248 (set x7=249)
    if !x5! == 28 if !x4! gtr 0 (set x7=14)
    if !x5! == 28 if !x4! gtr 16 (set x7=30)
    if !x5! == 28 if !x4! gtr 32 (set x7=46)
    if !x5! == 28 if !x4! gtr 48 (set x7=62)
    if !x5! == 28 if !x4! gtr 64 (set x7=78)
    if !x5! == 28 if !x4! gtr 80 (set x7=94)
    if !x5! == 28 if !x4! gtr 96 (set x7=110)
    if !x5! == 28 if !x4! gtr 112 (set x7=126)
    if !x5! == 28 if !x4! gtr 128 (set x7=142)
    if !x5! == 28 if !x4! gtr 144 (set x7=158)
    if !x5! == 28 if !x4! gtr 160 (set x7=174)
    if !x5! == 28 if !x4! gtr 176 (set x7=190)
    if !x5! == 28 if !x4! gtr 192 (set x7=206)
    if !x5! == 28 if !x4! gtr 208 (set x7=222)
    if !x5! == 28 if !x4! gtr 224 (set x7=238)
    if !x5! == 28 if !x4! gtr 240 (set x7=254)
    if !x5! == 27 if !x4! gtr 0 (set x7=1)
    if !x5! == 27 if !x4! gtr 32 (set x7=33)
    if !x5! == 27 if !x4! gtr 64 (set x7=65)
    if !x5! == 27 if !x4! gtr 96 (set x7=97)
    if !x5! == 27 if !x4! gtr 128 (set x7=129)
    if !x5! == 27 if !x4! gtr 160 (set x7=161)
    if !x5! == 27 if !x4! gtr 192 (set x7=193)
    if !x5! == 27 if !x4! gtr 224 (set x7=225)
    if !x5! == 26 if !x4! gtr 0 (set x7=1)
    if !x5! == 26 if !x4! gtr 64 (set x7=65)
    if !x5! == 26 if !x4! gtr 128 (set x7=129)
    if !x5! == 26 if !x4! gtr 192 (set x7=193)
    if !x5! == 25 if !x4! gtr 0 (set x7=126)
    if !x5! == 25 if !x4! gtr 128 (set x7=254)
    if !x5! == 24 (set x7=1)
    set gateway=!x1!.!x2!.!x3!.!x7!
    echo Gateway: !gateway!
    set dns1=8.8.8.8
    set dns2=8.8.4.4
    echo DNS1: !dns1!
    echo DNS2: !dns2!
)
echo.
echo 正在配置IP...
netsh interface ip set address name="!ethname!" source=static addr=!addr! mask=!netmask! gateway=!gateway! 1
netsh interface ip set dns name="!ethname!" source=static addr=!dns1! >/nul
netsh interface ip add dns "!ethname!" addr=!dns2! index=2 >/nul
ping 127.1 -n 5 >nul
echo IP配置完成
echo.
ping baidu.com
::echo.
::ping baidu.com >nul && (echo 可以PING通百度!!) || (echo 无法PING通百度!!)
echo.
pause
goto select

:configmoreip
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "已连接"') do set "ethname=%%b"
echo 已连接状态的网卡名称: !ethname!
echo 例如要配置: 192.168.1.2/24~192.168.1.254/24
echo 则起始IP的主机位为 2
echo 结束IP的主机位为 254
echo 网络位为 192.168.1
echo.
set /p start=请输入起始IP的主机位: 
set /p end=请输入结束IP的主机位: 
set /p net=请输入配置的IP的网络位: 
set netmask=255.255.255.0
)
echo 您要配置的IP信息如下: 
echo StartIP:!net!.!start!
echo EndIP: !net!.!end!
echo MASK:!netmask!
echo 正在配置指定段的IP...
for /l %%i in (!start!,1,!end!) do netsh interface ip add address "!ethname!" !net!.%%i !netmask! >nul && (echo !net!.%%i 已配置到“!ethname!”上)
echo.
pause
goto select

:configmoreip4
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "已连接"') do set "ethname=%%b"
echo 已连接状态的网卡名称: !ethname!
echo 例如要配置: 192.168.1.2/26~192.168.1.62/26
echo 则起始IP的主机位为 2
echo 结束IP的主机位为 62
echo 网络位为 192.168.1
echo.
set /p start1=请输入第一段起始IP的主机位: 
set /p end1=请输入第一段结束IP的主机位: 
set /p net1=请输入第一段配置的IP的网络位: 
set /p start2=请输入第二段起始IP的主机位: 
set /p end2=请输入第二段结束IP的主机位: 
set /p net2=请输入第二段配置的IP的网络位: 
set /p start3=请输入第三段起始IP的主机位: 
set /p end3=请输入第三段结束IP的主机位: 
set /p net3=请输入第三段配置的IP的网络位: 
set /p start4=请输入第四段起始IP的主机位: 
set /p end4=请输入第四段结束IP的主机位: 
set /p net4=请输入第四段配置的IP的网络位: 
set /p mask=请输入子网掩码最后一位: 
set netmask=255.255.255.192
echo.
echo 您要配置的IP信息如下: 
echo StartIP:!net1!.!start1!
echo EndIP: !net1!.!end1!
echo StartIP:!net2!.!start2!
echo EndIP: !net2!.!end2!
echo StartIP:!net3!.!start3!
echo EndIP: !net3!.!end3!
echo StartIP:!net4!.!start4!
echo EndIP: !net4!.!end4!
echo MASK:!netmask!
echo.
echo 正在配置指定段的IP...
for /l %%i in (!start1!,1,!end1!) do netsh interface ip add address "!ethname!" !net1!.%%i !netmask! >nul && (echo !net1!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start2!,1,!end2!) do netsh interface ip add address "!ethname!" !net2!.%%i !netmask! >nul && (echo !net2!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start3!,1,!end3!) do netsh interface ip add address "!ethname!" !net3!.%%i !netmask! >nul && (echo !net3!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start4!,1,!end4!) do netsh interface ip add address "!ethname!" !net4!.%%i !netmask! >nul && (echo !net4!.%%i 已配置到“!ethname!”上)
echo.
pause
goto select

:configmoreip8
for /f "tokens=3*" %%a in ('netsh interface show interface ^| findstr "已连接"') do set "ethname=%%b"
echo 已连接状态的网卡名称: !ethname!
echo 例如要配置: 192.168.1.2/27~192.168.1.30/26
echo 则起始IP的主机位为 2
echo 结束IP的主机位为 30
echo 网络位为 192.168.1
echo.
set /p start1=请输入第一段起始IP的主机位: 
set /p end1=请输入第一段结束IP的主机位: 
set /p net1=请输入第一段配置的IP的网络位: 
set /p start2=请输入第二段起始IP的主机位: 
set /p end2=请输入第二段结束IP的主机位: 
set /p net2=请输入第二段配置的IP的网络位: 
set /p start3=请输入第三段起始IP的主机位: 
set /p end3=请输入第三段结束IP的主机位: 
set /p net3=请输入第三段配置的IP的网络位: 
set /p start4=请输入第四段起始IP的主机位: 
set /p end4=请输入第四段结束IP的主机位: 
set /p net4=请输入第四段配置的IP的网络位: 
set /p start5=请输入第五段起始IP的主机位: 
set /p end5=请输入第五段结束IP的主机位: 
set /p net5=请输入第五段配置的IP的网络位: 
set /p start6=请输入第六段起始IP的主机位: 
set /p end6=请输入第六段结束IP的主机位: 
set /p net6=请输入第六段配置的IP的网络位: 
set /p start7=请输入第七段起始IP的主机位: 
set /p end7=请输入第七段结束IP的主机位: 
set /p net7=请输入第七段配置的IP的网络位: 
set /p start8=请输入第八段起始IP的主机位: 
set /p end8=请输入第八段结束IP的主机位: 
set /p net8=请输入第八段配置的IP的网络位: 
set /p mask=请输入子网掩码最后一位: 
set netmask=255.255.255.224
echo.
echo 您要配置的IP信息如下: 
echo StartIP1:!net1!.!start1!
echo EndIP1: !net1!.!end1!
echo StartIP2:!net2!.!start2!
echo EndIP2: !net2!.!end2!
echo StartIP3:!net3!.!start3!
echo EndIP3: !net3!.!end3!
echo StartIP4:!net4!.!start4!
echo EndIP4: !net4!.!end4!
echo StartIP5:!net5!.!start5!
echo EndIP5: !net5!.!end5!
echo StartIP6:!net6!.!start6!
echo EndIP6: !net6!.!end6!
echo StartIP7:!net7!.!start7!
echo EndIP7: !net7!.!end7!
echo StartIP8:!net8!.!start8!
echo EndIP8: !net8!.!end8!
echo MASK:!netmask!
echo.
echo 正在配置指定段的IP...
for /l %%i in (!start1!,1,!end1!) do netsh interface ip add address "!ethname!" !net1!.%%i !netmask! >nul && (echo !net1!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start2!,1,!end2!) do netsh interface ip add address "!ethname!" !net2!.%%i !netmask! >nul && (echo !net2!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start3!,1,!end3!) do netsh interface ip add address "!ethname!" !net3!.%%i !netmask! >nul && (echo !net3!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start4!,1,!end4!) do netsh interface ip add address "!ethname!" !net4!.%%i !netmask! >nul && (echo !net4!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start5!,1,!end5!) do netsh interface ip add address "!ethname!" !net5!.%%i !netmask! >nul && (echo !net5!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start6!,1,!end6!) do netsh interface ip add address "!ethname!" !net6!.%%i !netmask! >nul && (echo !net6!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start7!,1,!end7!) do netsh interface ip add address "!ethname!" !net7!.%%i !netmask! >nul && (echo !net7!.%%i 已配置到“!ethname!”上)
for /l %%i in (!start8!,1,!end8!) do netsh interface ip add address "!ethname!" !net8!.%%i !netmask! >nul && (echo !net8!.%%i 已配置到“!ethname!”上)
echo.
pause
goto select
0

评论 (0)

取消