2018-07-30 23:48:20 +02:00
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
from twisted.internet import reactor, defer
|
|
|
|
from txupnp.upnp import UPnP
|
|
|
|
|
|
|
|
log = logging.getLogger("txupnp")
|
|
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run_command(found, u, command):
|
|
|
|
if not found:
|
|
|
|
print("failed to find gateway")
|
|
|
|
reactor.callLater(0, reactor.stop)
|
|
|
|
return
|
2018-07-31 17:35:24 +02:00
|
|
|
if command == "debug_device":
|
2018-07-30 23:48:20 +02:00
|
|
|
print(u.get_debug_info())
|
2018-08-01 21:39:09 +02:00
|
|
|
elif command == "get_external_ip":
|
|
|
|
ip = yield u.get_external_ip()
|
|
|
|
print(ip)
|
|
|
|
elif command == "list_mappings":
|
2018-07-30 23:48:20 +02:00
|
|
|
redirects = yield u.get_redirects()
|
|
|
|
print("found {} redirects".format(len(redirects)))
|
|
|
|
for redirect in redirects:
|
|
|
|
print("\t", redirect)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="upnp command line utility")
|
2018-07-31 17:35:24 +02:00
|
|
|
parser.add_argument(dest="command", type=str, help="debug_gateway | list_mappings")
|
|
|
|
parser.add_argument("--debug_logging", dest="debug_logging", default=False, action="store_true")
|
2018-07-30 23:48:20 +02:00
|
|
|
args = parser.parse_args()
|
2018-07-31 17:35:24 +02:00
|
|
|
if args.debug_logging:
|
2018-07-30 23:48:20 +02:00
|
|
|
from twisted.python import log as tx_log
|
|
|
|
observer = tx_log.PythonLoggingObserver(loggerName="txupnp")
|
|
|
|
observer.start()
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
command = args.command
|
2018-08-01 21:39:09 +02:00
|
|
|
if command not in ['debug_device', 'list_mappings', 'get_external_ip']:
|
2018-07-30 23:48:20 +02:00
|
|
|
return sys.exit(0)
|
|
|
|
|
2018-07-31 22:53:08 +02:00
|
|
|
def show(err):
|
|
|
|
print("error: {}".format(err))
|
|
|
|
|
2018-07-30 23:48:20 +02:00
|
|
|
u = UPnP(reactor)
|
|
|
|
d = u.discover()
|
|
|
|
d.addCallback(run_command, u, command)
|
2018-07-31 22:53:08 +02:00
|
|
|
d.addErrback(show)
|
2018-07-30 23:48:20 +02:00
|
|
|
d.addBoth(lambda _: reactor.callLater(0, reactor.stop))
|
|
|
|
reactor.run()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|