sane-ip-check: port argument parsing to argparse
This commit is contained in:
@@ -1,9 +1,13 @@
|
|||||||
#!/usr/bin/env nix-shell
|
#!/usr/bin/env nix-shell
|
||||||
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ps.requests ps.sane-lib.ssdp ])" -p miniupnpc
|
#!nix-shell -i python3 -p "python3.withPackages (ps: [ ps.requests ps.sane-lib.ssdp ])" -p miniupnpc
|
||||||
|
"""
|
||||||
|
sane-ip-check: query the IP address of this machine as seen by an external Internet host.
|
||||||
|
"""
|
||||||
|
|
||||||
# best to run this with an external timeout. e.g.
|
# best to run this with an external timeout. e.g.
|
||||||
# - `timeout 60 sane-ip-check`
|
# - `timeout 60 sane-ip-check`
|
||||||
|
|
||||||
|
import argparse
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
@@ -27,20 +31,25 @@ def get_wan_fallback():
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument("-v", action="store_true", help="be verbose")
|
||||||
|
parser.add_argument("-vv", action="store_true", help="be more verbose")
|
||||||
|
parser.add_argument("--json", action="store_true", help="output results in json format")
|
||||||
|
parser.add_argument("--no-upnp", action="store_true", help="instead of asking the gateway for its IP address, discover my IP by querying a third-party public service")
|
||||||
|
|
||||||
format = "plaintext"
|
format = "plaintext"
|
||||||
try_upnp = True
|
try_upnp = True
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.v:
|
||||||
|
logging.getLogger().setLevel(logging.INFO)
|
||||||
|
if args.vv:
|
||||||
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
|
if args.json:
|
||||||
|
format = "json"
|
||||||
|
if args.no_upnp:
|
||||||
|
try_upnp = False
|
||||||
|
|
||||||
for arg in sys.argv[1:]:
|
|
||||||
if arg == "-v":
|
|
||||||
logging.getLogger().setLevel(logging.INFO)
|
|
||||||
elif arg == "-vv":
|
|
||||||
logging.getLogger().setLevel(logging.DEBUG)
|
|
||||||
elif arg == "--json":
|
|
||||||
format = "json"
|
|
||||||
elif arg == "--no-upnp":
|
|
||||||
try_upnp = False
|
|
||||||
else:
|
|
||||||
raise RuntimeError(f"invalid CLI argument {arg!r}")
|
|
||||||
|
|
||||||
upnp_details = get_any_wan() if try_upnp else None
|
upnp_details = get_any_wan() if try_upnp else None
|
||||||
if upnp_details:
|
if upnp_details:
|
||||||
|
Reference in New Issue
Block a user