Skip to main content

A tool to convert IP addresses to /24 CIDR notation

Project description

ip2cidr

A Python tool to convert IP addresses to /24 CIDR notation.

Installation

pip install ip2cidr

Usage

The tool can be used from the command line:

# Convert a single IP
ip2cidr -i "10.200.2.132"

# Convert multiple IPs (comma-separated)
ip2cidr -i "10.200.2.132,192.168.1.100,172.16.5.200"

# Use a different separator for output (default is comma)
ip2cidr -i "10.200.2.132,192.168.1.100" -s " "

Arguments

  • -i, --ips: Required. Comma-separated list of IP addresses to convert
  • -s, --separator: Optional. Output separator (default: comma)

Example Output

$ ip2cidr -i "10.200.2.132,192.168.1.100,172.16.5.200"
10.200.2.0/24,172.16.5.0/24,192.168.1.0/24

$ ip2cidr -i "10.200.2.132,192.168.1.100" -s " "
10.200.2.0/24 192.168.1.0/24

Python Usage

You can also use the tool in your Python code:

from ip_converter import ip_to_cidr

# Convert IPs to CIDR
ips = "10.200.2.132,192.168.1.100"
cidrs = ip_to_cidr(ips)
print(cidrs)  # ['10.200.2.0/24', '192.168.1.0/24']

Development

Running Tests

To run the tests, first install the development dependencies:

pip install -e ".[test]"

Then run the tests with pytest:

pytest

This will run all tests and generate a coverage report. The tests cover various scenarios including:

  • Single IP conversion
  • Multiple IP conversion
  • Duplicate network handling
  • Invalid IP handling
  • Empty input
  • Whitespace handling
  • Partial IP addresses
  • Special IP addresses (localhost, etc.)

Features

  • Converts IP addresses to their corresponding /24 CIDR networks
  • Handles multiple IP addresses
  • Removes duplicates and sorts the output
  • Customizable output separator
  • Error handling for invalid IP addresses
  • Comprehensive test suite with 100% coverage

Requirements

  • Python 3.8 or higher
  • argparse>=1.4.0

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ip2cidr-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ip2cidr-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file ip2cidr-0.1.0.tar.gz.

File metadata

  • Download URL: ip2cidr-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for ip2cidr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8da54bed9aa12ce7f255895ce1805d0e31122efbcc5320cdee435e1aba227932
MD5 b2da8635de878002da655bee1b689b80
BLAKE2b-256 fae8dd98b0f438b5813a9a20e8eeb8c53c56535337d6efa2c6a7acb69a236787

See more details on using hashes here.

File details

Details for the file ip2cidr-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ip2cidr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for ip2cidr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7bd1ba2fb5fbd69cb2d3edded04078dfad1b4ce0144fb997ca23e9f9d2fbf050
MD5 6917c46a07803686ef42c224243703a7
BLAKE2b-256 da899c8a4f230d9396b323b977a0aa2605637f957074f149461c042a29446721

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page