sane-ip-check: add an --expect option to abort if IP address is not as expected

This commit is contained in:
Colin 2024-04-18 19:46:16 +00:00
parent 3bd56fb565
commit 9442a87311
1 changed files with 6 additions and 2 deletions

View File

@ -28,13 +28,16 @@ def get_wan_fallback():
else:
return ip
def main(format: str, try_upnp: bool) -> None:
def main(format: str, try_upnp: bool, expect: str|None) -> None:
upnp_details = get_any_wan() if try_upnp else None
if upnp_details:
root_dev, _lan_ip, wan_ip = upnp_details
else:
root_dev, wan_ip = "", get_wan_fallback()
if expect:
assert wan_ip == expect, f"actual IP {wan_ip} != expected IP {expect}"
if format == "plaintext":
print(wan_ip)
elif format == "json":
@ -51,6 +54,7 @@ if __name__ == '__main__':
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")
parser.add_argument("--expect", default=None, help="exit with error if IP address isn't as specified")
format = "plaintext"
try_upnp = True
@ -65,4 +69,4 @@ if __name__ == '__main__':
if args.no_upnp:
try_upnp = False
main(format=format, try_upnp=try_upnp)
main(format=format, try_upnp=try_upnp, expect=args.expect)