Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2705166
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类:

2012-09-22 19:51:33

原文地址:利用upnp 映射到外网 作者:mxcai2005


1. Upnp 下载地址


说明:miniupnpc 为客户端, miniupnpd 为服务端
这里下载 miniupnpc-1.6.20120125.tar.gz

2. 编译miniupnp:
# tar xf miniupnpc-1.6.20120125.tar.gz
# make
在此目录下会生成 upnpc-static  upnpc-share

3. 配置路由器 让其开启 upnp 功能
登录路由器后 选择 转发规则-->upnp 设置→ 开启

4. 在客户端(loongson 1B 开发板)添加upnp 规则
#  ifconfig  //查看本机IP
eth0     Link encap:Ethernet  HWaddr 00:1e:64:63:ab:fe  
          inet addr:192.168.3.111  Bcast:192.168.3.255  Mask:255.255.255.0Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:163376 errors:0 dropped:0 overruns:0 frame:0
          TX packets:136701 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:170220701 (170.2 MB)  TX bytes:21403783 (21.4 MB)

#  upnpc-static  -a 192.168.3.111 80 8008 TCP   //此处的IP为本机IP
upnpc : miniupnpc library test client. (c) 2006-2011 Thomas Bernard
Go to or
for more information.
List of UPNP devices found on the network :
 desc:
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1

Found valid IGD :
Local LAN ip address : 192.168.3.111
ExternalIPAddress = 192.168.1.12
InternalIP:Port = 192.168.3.111:80
external 192.168.1.12:8008 TCP is redirected to internal 192.168.3.111:80 (duration=0)

说明:80 为内部端口, 此端口号可随意设置,只要与 web server的提供的端口号相同就可以了。8008 为外端口,也就是在外面通过 192.168.1.12:8008 就可以访问客户端的web 页面了
Upnpc-static 的详细用法:#  Upnpc-static
upnpc : miniupnpc library test client. (c) 2006-2011 Thomas Bernard
Go to or
for more information.
Usage :    ./upnpc-static [options] -a ip port external_port protocol [duration]
        Add port redirection
           ./upnpc-static [options] -d external_port protocol [port2 protocol2] [...]
        Delete port redirection
           ./upnpc-static [options] -s
        Get Connection status
           ./upnpc-static [options] -l
        List redirections
           ./upnpc-static [options] -L
        List redirections (using GetListOfPortMappings, IGD v2)
           ./upnpc-static [options] -r port1 protocol1 [port2 protocol2] [...]
        Add all redirections to the current host
           ./upnpc-static [options] -A remote_ip remote_port internal_ip internal_port protocol lease_time
        Add Pinhole (for IGD:2 only)
           ./upnpc-static [options] -U uniqueID new_lease_time
        Update Pinhole (for IGD:2 only)
           ./upnpc-static [options] -C uniqueID
        Check if Pinhole is Working (for IGD:2 only)
           ./upnpc-static [options] -K uniqueID
        Get Number of packets going through the rule (for IGD:2 only)
           ./upnpc-static [options] -D uniqueID
        Delete Pinhole (for IGD:2 only)
           ./upnpc-static [options] -S
        Get Firewall status (for IGD:2 only)
           ./upnpc-static [options] -w routerip
        set the ip address of the router with upnp server
           ./upnpc-static [options] -G remote_ip remote_port internal_ip internal_port protocol
        Get Outbound Pinhole Timeout (for IGD:2 only)
           ./upnpc-static [options] -P
        Get Presentation url

protocol is UDP or TCP
Options:
  -6 : use ip v6 instead of ip v4.
  -u url : bypass discovery process by providing the XML root description url.
  -m address/interface : provide ip address (ip v4) or interface name (ip v6) to use for sending SSDP multicast packets.
  -p path : use this path for MiniSSDPd socket.


备注:客户端要和路由器连在一起,因为upnp 只能穿透 1 层网络

这时再次登陆路由器 选择 转发规则-->upnp 设置→ 刷新 就可以看到添加了一条 upnp规则
当前UPnP设置列表
ID     应用描述     外部端口     协议类型     内部端口     IP地址     状态
1    libminiupnpc    8008    TCP    80    192.168.3.111    已启用
    

5. 在客户端开启web 服务器(loongson 1B开发板上用 boa 做web server,因为其大小只有 100 KB 多点 )
# cat /etc/boa/boa.conf | grep Port  //查看其端口号 看是否与upnp映射的端口号相同,不同则改为相同
 #Port: The port Boa runs on.  The default port for http servers is 80.
Port 80

# boa    //板上的web server ,

6. 在电脑上测试
这时找一个电脑在浏览器里输入: 192.168.1.12:8008 键入 enter ,客户端里的web页面就呈现在你的面前了


阅读(3109) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~