2023-12-30 16:35:35 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# vim: tabstop=2 shiftwidth=2 expandtab
|
2014-03-15 16:59:03 +00:00
|
|
|
# MiniUPnP project
|
|
|
|
# Author : Thomas Bernard
|
2023-12-30 16:35:35 +00:00
|
|
|
# Python 3
|
2014-03-15 16:59:03 +00:00
|
|
|
# This Sample code is public domain.
|
2023-12-30 16:35:35 +00:00
|
|
|
# website : https://miniupnp.tuxfamily.org/
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
# import the python miniupnpc module
|
|
|
|
import miniupnpc
|
|
|
|
import sys
|
|
|
|
|
2023-12-30 16:35:35 +00:00
|
|
|
try:
|
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-m', '--multicastif')
|
|
|
|
parser.add_argument('-p', '--minissdpdsocket')
|
|
|
|
parser.add_argument('-d', '--discoverdelay', type=int, default=200)
|
|
|
|
parser.add_argument('-z', '--localport', type=int, default=0)
|
|
|
|
# create the object
|
|
|
|
u = miniupnpc.UPnP(**vars(parser.parse_args()))
|
|
|
|
except:
|
|
|
|
print('argparse not available')
|
|
|
|
i = 1
|
|
|
|
multicastif = None
|
|
|
|
minissdpdsocket = None
|
|
|
|
discoverdelay = 200
|
|
|
|
localport = 0
|
|
|
|
while i < len(sys.argv):
|
|
|
|
print(sys.argv[i])
|
|
|
|
if sys.argv[i] == '-m' or sys.argv[i] == '--multicastif':
|
|
|
|
multicastif = sys.argv[i+1]
|
|
|
|
elif sys.argv[i] == '-p' or sys.argv[i] == '--minissdpdsocket':
|
|
|
|
minissdpdsocket = sys.argv[i+1]
|
|
|
|
elif sys.argv[i] == '-d' or sys.argv[i] == '--discoverdelay':
|
|
|
|
discoverdelay = int(sys.argv[i+1])
|
|
|
|
elif sys.argv[i] == '-z' or sys.argv[i] == '--localport':
|
|
|
|
localport = int(sys.argv[i+1])
|
|
|
|
else:
|
|
|
|
raise Exception('invalid argument %s' % sys.argv[i])
|
|
|
|
i += 2
|
|
|
|
# create the object
|
|
|
|
u = miniupnpc.UPnP(multicastif, minissdpdsocket, discoverdelay, localport)
|
|
|
|
|
|
|
|
print('inital(default) values :')
|
|
|
|
print(' discoverdelay', u.discoverdelay)
|
|
|
|
print(' lanaddr', u.lanaddr)
|
|
|
|
print(' multicastif', u.multicastif)
|
|
|
|
print(' minissdpdsocket', u.minissdpdsocket)
|
2014-03-15 16:59:03 +00:00
|
|
|
#u.minissdpdsocket = '../minissdpd/minissdpd.sock'
|
2023-12-30 16:35:35 +00:00
|
|
|
# discovery process, it usually takes several seconds (2 seconds or more)
|
|
|
|
print('Discovering... delay=%ums' % u.discoverdelay)
|
|
|
|
print('%d device(s) detected' % u.discover())
|
2014-03-15 16:59:03 +00:00
|
|
|
# select an igd
|
|
|
|
try:
|
|
|
|
u.selectigd()
|
2023-12-30 16:35:35 +00:00
|
|
|
except Exception as e:
|
|
|
|
print('Exception :', e)
|
2014-03-15 16:59:03 +00:00
|
|
|
sys.exit(1)
|
2023-12-30 16:35:35 +00:00
|
|
|
# it is also possible to pass the root description URL to u.selectigd() :
|
|
|
|
# u.selectigd('http://192.168.1.254:5678/desc/root')
|
2014-03-15 16:59:03 +00:00
|
|
|
# display information about the IGD and the internet connection
|
2023-12-30 16:35:35 +00:00
|
|
|
print('local ip address :', u.lanaddr)
|
|
|
|
print('external ip address :', u.externalipaddress())
|
|
|
|
print( u.statusinfo(), u.connectiontype())
|
|
|
|
print('total bytes : sent', u.totalbytesent(), 'received', u.totalbytereceived())
|
|
|
|
print('total packets : sent', u.totalpacketsent(), 'received', u.totalpacketreceived())
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
#print u.addportmapping(64000, 'TCP',
|
|
|
|
# '192.168.1.166', 63000, 'port mapping test', '')
|
|
|
|
#print u.deleteportmapping(64000, 'TCP')
|
|
|
|
|
|
|
|
port = 0
|
|
|
|
proto = 'UDP'
|
|
|
|
# list the redirections :
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
p = u.getgenericportmapping(i)
|
|
|
|
if p==None:
|
|
|
|
break
|
2023-12-30 16:35:35 +00:00
|
|
|
print(i, p)
|
2014-03-15 16:59:03 +00:00
|
|
|
(port, proto, (ihost,iport), desc, c, d, e) = p
|
|
|
|
#print port, desc
|
|
|
|
i = i + 1
|
|
|
|
|
2023-12-30 16:35:35 +00:00
|
|
|
print(u.getspecificportmapping(port, proto))
|
|
|
|
try:
|
|
|
|
print(u.getportmappingnumberofentries())
|
|
|
|
except Exception as e:
|
|
|
|
print('GetPortMappingNumberOfEntries() is not supported :', e)
|
2014-03-15 16:59:03 +00:00
|
|
|
|