Friday, 23 August 2024

IP Address Manipulation with Python



1. Check if an IP Address is Private

This program checks whether an IP address belongs to a private range.


import ipaddress


def check_private_ip(ip):

    try:

        ip_obj = ipaddress.ip_address(ip)

        return ip_obj.is_private

    except ValueError:

        return False


ip = "192.168.0.1"

print(f"Is {ip} private? {check_private_ip(ip)}")


#source code --> clcoding.com

Is 192.168.0.1 private? True


2. Calculate the Network Range from a CIDR Notation

This program calculates the network range from a given CIDR notation.


import ipaddress


def get_network_range(cidr):

    try:

        network = ipaddress.ip_network(cidr, strict=False)

        return (network.network_address, network.broadcast_address)

    except ValueError:

        return None


cidr = "192.168.1.0/24"

network_range = get_network_range(cidr)

if network_range:

    print(f"Network range: {network_range[0]} - {network_range[1]}")


#source code --> clcoding.com

Network range: 192.168.1.0 - 192.168.1.255


3. Check if an IP Address is in a Specific Network

This program checks if a given IP address is part of a specified network.


import ipaddress


def ip_in_network(ip, network):

    try:

        network_obj = ipaddress.ip_network(network, strict=False)

        ip_obj = ipaddress.ip_address(ip)

        return ip_obj in network_obj

    except ValueError:

        return False


ip = "192.168.1.10"

network = "192.168.1.0/24"

print(f"Is {ip} in network {network}? {ip_in_network(ip, network)}")


#source code --> clcoding.com

Is 192.168.1.10 in network 192.168.1.0/24? True


4. Generate All IP Addresses in a Network

This program generates all possible IP addresses within a given network.


import ipaddress


def generate_ips_in_network(network):

    try:

        network_obj = ipaddress.ip_network(network, strict=False)

        return [str(ip) for ip in network_obj.hosts()]

    except ValueError:

        return []


network = "192.168.1.0/30"

ips = generate_ips_in_network(network)

print(f"IP addresses in {network}: {ips}")


#source code --> clcoding.com

IP addresses in 192.168.1.0/30: ['192.168.1.1', '192.168.1.2']


5. Convert Integer to IPv4 Address

This program converts an integer back to its corresponding IPv4 address.


import ipaddress


def int_to_ipv4(integer):

    try:

        return str(ipaddress.IPv4Address(integer))

    except ValueError:

        return None


ipv4_int = 3232235777

print(f"Integer: {ipv4_int} -> IPv4: {int_to_ipv4(ipv4_int)}")


#source code --> clcoding.com

Integer: 3232235777 -> IPv4: 192.168.1.1

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses