sane-ip-check: add an --expect option to abort if IP address is not as expected
This commit is contained in:
@@ -28,13 +28,16 @@ def get_wan_fallback():
|
|||||||
else:
|
else:
|
||||||
return ip
|
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
|
upnp_details = get_any_wan() if try_upnp else None
|
||||||
if upnp_details:
|
if upnp_details:
|
||||||
root_dev, _lan_ip, wan_ip = upnp_details
|
root_dev, _lan_ip, wan_ip = upnp_details
|
||||||
else:
|
else:
|
||||||
root_dev, wan_ip = "", get_wan_fallback()
|
root_dev, wan_ip = "", get_wan_fallback()
|
||||||
|
|
||||||
|
if expect:
|
||||||
|
assert wan_ip == expect, f"actual IP {wan_ip} != expected IP {expect}"
|
||||||
|
|
||||||
if format == "plaintext":
|
if format == "plaintext":
|
||||||
print(wan_ip)
|
print(wan_ip)
|
||||||
elif format == "json":
|
elif format == "json":
|
||||||
@@ -51,6 +54,7 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument("-vv", action="store_true", help="be more 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("--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("--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"
|
format = "plaintext"
|
||||||
try_upnp = True
|
try_upnp = True
|
||||||
@@ -65,4 +69,4 @@ if __name__ == '__main__':
|
|||||||
if args.no_upnp:
|
if args.no_upnp:
|
||||||
try_upnp = False
|
try_upnp = False
|
||||||
|
|
||||||
main(format=format, try_upnp=try_upnp)
|
main(format=format, try_upnp=try_upnp, expect=args.expect)
|
||||||
|
Reference in New Issue
Block a user