文章摘要
这篇文章介绍了如何通过批处理脚本完成网络配置任务。具体来说,文章详细描述了以下步骤: 1. **停止运行中的WSL实例**:通过`wsl --shutdown ubuntu`命令,确保当前运行的WSL实例已停止。 2. **配置WSL的IP地址**:使用`wsl -u root ip addr`检查WSL的IP地址,如果发现192.168.3.100不存在,会自动绑定该IP地址到网络接口`eth0`。 3. **配置宿主机的IP地址**:通过`ipconfig`命令检查宿主机(Windows系统)的IP地址,如果发现192.168.3.200不存在,会绑定该IP地址到网络接口`vEthernet (WSL)`。 4. **设置SSH转发端口**:使用`netsh interface portproxy add`命令,将SSH协议在端口22上进行转发,允许外部访问本地SSH终端。 整个脚本的目标是确保WSL环境和宿主机的网络配置正确无误,并且支持SSH远程访问。
@echo off
setlocal enabledelayedexpansion
::先停掉可能在跑的wsl实例
wsl –shutdown ubuntu
if !errorlevel! equ 0 (
::检查WSL有没有我需要的IP
wsl -u root ip addr | findstr “192.168.3.100” > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
::IP不存在则绑定IP
wsl -u root ip addr add 192.168.3.100/24 broadcast 192.168.3.255 dev eth0 label eth0:1
echo set wsl ip success: 192.168.3.100
)
::检查宿主机有没有我需要的IP
ipconfig | findstr “192.168.3.200” > nul
if !errorlevel! equ 0 (
echo windows ip has set
) else (
::IP不存在则绑定IP
netsh interface ip add address “vEthernet (WSL)” 192.168.3.200 255.255.255.0
echo set windows ip success: 192.168.3.200
)
)
::为主机设置SSH转发端口
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=22 connectaddress=192.168.3.100 connectport=22
pause
setlocal enabledelayedexpansion
::先停掉可能在跑的wsl实例
wsl –shutdown ubuntu
if !errorlevel! equ 0 (
::检查WSL有没有我需要的IP
wsl -u root ip addr | findstr “192.168.3.100” > nul
if !errorlevel! equ 0 (
echo wsl ip has set
) else (
::IP不存在则绑定IP
wsl -u root ip addr add 192.168.3.100/24 broadcast 192.168.3.255 dev eth0 label eth0:1
echo set wsl ip success: 192.168.3.100
)
::检查宿主机有没有我需要的IP
ipconfig | findstr “192.168.3.200” > nul
if !errorlevel! equ 0 (
echo windows ip has set
) else (
::IP不存在则绑定IP
netsh interface ip add address “vEthernet (WSL)” 192.168.3.200 255.255.255.0
echo set windows ip success: 192.168.3.200
)
)
::为主机设置SSH转发端口
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=22 connectaddress=192.168.3.100 connectport=22
pause
© 版权声明
文章版权归作者所有,未经允许请勿转载。



