Last active
December 7, 2025 16:12
-
-
Save HackingLZ/264631571adfdf05349f4ac0bb0fafc2 to your computer and use it in GitHub Desktop.
IPv4 to IPv4-mapped IPv6 Address Converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| IPv4 to IPv4-mapped IPv6 Address Converter | |
| Converts standard IPv4 addresses to the ::ffff: IPv6 mapped format. | |
| """ | |
| import ipaddress | |
| import sys | |
| def ipv4_to_mapped_ipv6(ipv4_str: str) -> str: | |
| """ | |
| Convert an IPv4 address to IPv4-mapped IPv6 format. | |
| Args: | |
| ipv4_str: IPv4 address as string (e.g., "193.24.210.101") | |
| Returns: | |
| IPv4-mapped IPv6 address (e.g., "::ffff:c118:d265") | |
| """ | |
| # Parse and validate the IPv4 address | |
| ipv4 = ipaddress.IPv4Address(ipv4_str) | |
| # Get the packed bytes (4 bytes) | |
| octets = ipv4.packed | |
| # Convert to two 16-bit hex groups | |
| high = (octets[0] << 8) | octets[1] | |
| low = (octets[2] << 8) | octets[3] | |
| return f"::ffff:{high:x}:{low:x}" | |
| def ipv4_to_mapped_ipv6_url(ipv4_str: str, path: str = "/") -> str: | |
| """ | |
| Convert an IPv4 address to a full URL with IPv4-mapped IPv6 format. | |
| Args: | |
| ipv4_str: IPv4 address as string | |
| path: URL path (default: "/") | |
| Returns: | |
| Full URL with bracketed IPv6 address | |
| """ | |
| mapped = ipv4_to_mapped_ipv6(ipv4_str) | |
| return f"http://[{mapped}]{path}" | |
| def mapped_ipv6_to_ipv4(ipv6_str: str) -> str: | |
| """ | |
| Convert an IPv4-mapped IPv6 address back to IPv4. | |
| Args: | |
| ipv6_str: IPv4-mapped IPv6 address (e.g., "::ffff:c118:d265") | |
| Returns: | |
| IPv4 address as string | |
| """ | |
| # Remove brackets if present | |
| ipv6_str = ipv6_str.strip("[]") | |
| # Parse the IPv6 address | |
| ipv6 = ipaddress.IPv6Address(ipv6_str) | |
| # Check if it's an IPv4-mapped address | |
| if not ipv6.ipv4_mapped: | |
| raise ValueError(f"{ipv6_str} is not an IPv4-mapped IPv6 address") | |
| return str(ipv6.ipv4_mapped) | |
| def main(): | |
| print("=" * 50) | |
| print("IPv4 to IPv4-Mapped IPv6 Converter") | |
| print("=" * 50) | |
| # Example conversions | |
| examples = [ | |
| "192.168.1.1", | |
| "8.8.8.8", | |
| "10.0.0.1", | |
| "172.16.0.1", | |
| "255.255.255.255", | |
| ] | |
| print("\nExample Conversions:") | |
| print("-" * 50) | |
| for ipv4 in examples: | |
| mapped = ipv4_to_mapped_ipv6(ipv4) | |
| url = ipv4_to_mapped_ipv6_url(ipv4) | |
| print(f"{ipv4:18} -> {mapped:22} -> {url}") | |
| print("-" * 50) | |
| # Reverse conversion demo | |
| print("\nReverse Conversion Demo:") | |
| print("-" * 50) | |
| test_v6 = "::ffff:c118:d265" | |
| print(f"{test_v6} -> {mapped_ipv6_to_ipv4(test_v6)}") | |
| # Interactive mode if arguments provided | |
| if len(sys.argv) > 1: | |
| print("\n" + "=" * 50) | |
| print("Your Conversions:") | |
| print("-" * 50) | |
| for arg in sys.argv[1:]: | |
| try: | |
| # Try IPv4 -> IPv6 | |
| mapped = ipv4_to_mapped_ipv6(arg) | |
| url = ipv4_to_mapped_ipv6_url(arg) | |
| print(f"{arg} -> {mapped}") | |
| print(f" URL: {url}") | |
| except ipaddress.AddressValueError: | |
| try: | |
| # Try IPv6 -> IPv4 | |
| ipv4 = mapped_ipv6_to_ipv4(arg) | |
| print(f"{arg} -> {ipv4}") | |
| except (ipaddress.AddressValueError, ValueError) as e: | |
| print(f"Error: {e}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment