1. 查询端口映射情况

netsh interface portproxy show v4tov4

2. 查询某一个IP的所有端口映射情况

netsh interface portproxy show v4tov4 | find "[IP]"

例:

netsh interface portproxy show v4tov4 | find "192.168.1.1"

3. 增加一个端口映射

netsh interface portproxy add v4tov4 listenaddress=[外网IP] listenport=[外网端口] connectaddress=[内网IP] connectport=[内网端口]

例:

netsh interface portproxy add v4tov4 listenaddress=2.2.2.2 listenport=8080 connectaddress=192.168.1.50 connectport=80

4. 删除一个端口映射

netsh interface portproxy delete v4tov4 listenaddress=[外网IP] listenport=[外网端口]

例:

netsh interface portproxy delete v4tov4 listenaddress=2.2.2.2 listenport=8080

实验:将部署在内网的服务映射到外网

实验环境

  1. VMWare Workstation Pro
  2. 5台最小化安装的Windows虚拟机

如果Server4为Windows,替换一下相应的命令即可

Windows的IP信息如下

网卡IP地址子网掩码默认网关备注
Ethernet0192.168.50.105255.255.255.0-内网网卡
Ethernet1172.16.2.105255.255.255.0-外网网卡

配置并查看端口映射情况

netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8081 connectaddress=192.168.50.11 connectport=8080
netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8082 connectaddress=192.168.50.12 connectport=8080
netsh interface portproxy show v4tov4

检查效果

client节点上

curl http://172.16.2.105:8081/index.html
curl http://172.16.2.105:8082/index.html
curl http://172.16.2.11:8080/index.html