Untitled

 avatar
unknown
plain_text
7 days ago
1.2 kB
5
Indexable
import ipaddress

def get_ip_class(ip): first_octet = int(ip.split('.')[0]) if 1 <= first_octet <= 126: return "Class A" elif 128 <= first_octet <= 191: return "Class B" elif 192 <= first_octet <= 223: return "Class C" elif 224 <= first_octet <= 239: return "Class D (Multicast)" elif 240 <= first_octet <= 255: return "Class E (Experimental)" else: return "Invalid IP Address"

Q1 & Q2

ip_address = input("Enter an IP address: ") print(f"The IP address {ip_address} belongs to {get_ip_class(ip_address)}")

Q3: Convert dotted decimal IP to 32-bit binary address

def ip_to_binary(ip): try: return bin(int(ipaddress.IPv4Address(ip)))[2:].zfill(32) except ipaddress.AddressValueError: return "Invalid IP Address"

print(f"32-bit binary representation: {ip_to_binary(ip_address)}")

Q4: Routing protocol simulation (Basic example)

def check_connectivity(subnet1, subnet2): net1 = ipaddress.ip_network(subnet1, strict=False) net2 = ipaddress.ip_network(subnet2, strict=False) if net1.overlaps(net2): return "Networks are connected" else: return "Networks are not connected"

subnet1 = input("Enter first subnet (e.g., 192.168.1.0/24): ") subnet2 = input("Enter second subnet (e.g., 192.168.2.0/24): ") print(check_connectivity(subnet1, subnet2))

Editor is loading...
Leave a Comment