Add JSON output support to --me flag

The --me flag now respects the --json option. When used together,
chkip will return local and public IP addresses as structured JSON.

This allows easier integration into scripts or tools that consume JSON,
while maintaining human-readable output as the default.

Additionally, CGNAT detection is included in the JSON object as a boolean flag.
This commit is contained in:
Pascal Bouquet 2025-04-19 16:53:43 +02:00
parent 4868ff54e2
commit 1d01ada2aa

View File

@ -218,15 +218,22 @@ def main():
if args.me:
local_v4, public_v4, public_v6 = get_my_ip()
print(f"Your local IPv4: {local_v4}")
print(f"Your public IPv4: {public_v4}")
print(f"Your public IPv6: {public_v6}")
result = {
"local_ipv4": local_v4,
"public_ipv4": public_v4,
"public_ipv6": public_v6
}
if is_cgnat(public_v4):
print("⚠️ Hinweis: Du befindest dich möglicherweise hinter CGNAT (Carrier-Grade NAT)")
return
result["cgnat"] = True
if not args.domain:
parser.print_help()
if args.json:
print(json.dumps(result, indent=2))
else:
print(f"Your local IPv4: {result['local_ipv4']}")
print(f"Your public IPv4: {result['public_ipv4']}")
print(f"Your public IPv6: {result['public_ipv6']}")
if result.get("cgnat"):
print("⚠️ Hinweis: Du befindest dich möglicherweise hinter CGNAT (Carrier-Grade NAT)")
return
domain = args.domain