今天突发奇想,想让自己的树莓派某个端口自动映射到公网,想起来路由器有个UPNP功能并且已经开启,那树莓派肯定也可以利用这个功能自动把对应端口映射到公网之上,想到这里于是乎我上网搜到一个linux下的小工具名为:MiniUPnP

于是下载下来传到树莓派上编译安装
这里也提供给大家下载链接

http://miniupnp.free.fr/files/

本站下载:

miniupnpc-2.0.20180222.tar

 

下面举个例子:

增加一个UPNP设置,把本机的80端口映射到网关的8008端口:

upnpc -a 192.168.88.162 80 8008 TCP

删除这个设置:

upnpc -d 8008 TCP

下面是运行后的结果:


root@raspberrypi:~# upnpc -a 192.168.88.162 80 8008 TCP
upnpc : miniupnpc library test client, version 2.0.
(c) 2005-2017 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
desc: http://192.168.88.1:2828/gateway.xml
st: urn:schemas-upnp-org:device:InternetGatewayDevice:1
Found a (not connected?) IGD : http://192.168.88.1:2828/upnp/control/jmsjwlikhz/wanipconn-1
Trying to continue anyway
Local LAN ip address : 192.168.88.162
ExternalIPAddress = 0.0.0.0
InternalIP:Port = 192.168.88.162:80
external 0.0.0.0:8008 TCP is redirected to internal 192.168.88.162:80 (duration=0)
root@raspberrypi:~# upnpc -d 8008 TCP
upnpc : miniupnpc library test client, version 2.0.
(c) 2005-2017 Thomas Bernard.
Go to http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
desc: http://192.168.88.1:2828/gateway.xml
st: urn:schemas-upnp-org:device:InternetGatewayDevice:1
Found a (not connected?) IGD : http://192.168.88.1:2828/upnp/control/jmsjwlikhz/wanipconn-1
Trying to continue anyway
Local LAN ip address : 192.168.88.162
UPNP_DeletePortMapping() returned : 0
root@raspberrypi:~#

利用这个命令大家完全可以自己编写一个启动脚本,当树莓派启动之后自动运行来提交端口映射。

发表回复