Skip to main content

py-cgnat

Python module for generating CGNAT rules using netmap

Brief

This is a Python library and CLI program for generating firewall rules to deploy Carrier-Grade NAT, besides translating a given IP and port to its private address and vice versa. The methodology consists in building netmap rules at 1:32 public-private ratio, mapping a range of 2.000 ports for each client. Works for any netmask, since that follow the 1:32 ratio:

Private prefix Public prefix N. of clients
... ... ...
/20 /25 4096
/21 /26 2048
/22 /27 1024
/23 /28 512
/24 /29 256
/25 /30 128
/26 /31 64
/27 /32 32

Supported Platforms

  • MikroTik RouterOS

Requirements

  • Python 3.7+

How to install it?

Installation can just being done with pip:

pip install pycgnat

How to use it?

1. Command Line Interface

For generating the rules, you can print it in console or save it to a file:

pycgnat 100.64.0.0/20 203.0.113.0/25 gen routeros filename.rsc
pycgnat 100.64.0.0/20 203.0.113.0/25 gen routeros

For translating a private IP to its public one, use the direct option:

pycgnat 100.64.0.0/20 203.0.113.0/25 trans --direct 100.64.2.15
pycgnat 100.64.0.0/20 203.0.113.0/25 trans -d 100.64.2.15

For translatig a public IP and port to its private IP correspondent, use the reverse option:

pycgnat 100.64.0.0/20 203.0.113.0/25 trans --reverse 203.0.113.20:13578
pycgnat 100.64.0.0/20 203.0.113.0/25 trans -r 203.0.113.20:13578

The CLI includes useful help command (supported by argparse framework), so just type:

pycgnat --help
pycgnat -h

2. Python library

You can use the functionalities directly in Python lang. Just import the wanted module to your program:

from pycgnat.translator.reverse import cgnat_reverse

dic = cgnat_reverse(privnet, pubnet, IPv4Address('203.0.113.20'), 13578)
print(dic['private_ip'])

The full pycgnat's documentation is written in the source-code.

Future works

  • Add support for other platfoms (I'm using MikroTik for while, so this is the reason for only supporting it at first version).

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Metadata

Release files for pycgnat 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pycgnat 1.0.0
File Size Uploaded Provenance Actions
pycgnat-1.0.0.tar.gz 7.0 kB Without provenance Details for pycgnat-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pycgnat 1.0.0
File Interpreter ABI Platform Actions
pycgnat-1.0.0-py3-none-any.whl Python 3 none any Details for pycgnat-1.0.0-py3-none-any.whl

Total release size: 16.5 kB

Release files / pycgnat-1.0.0.tar.gz

Download URL pycgnat-1.0.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f3c9c2d0c61d4863a8e90402181d1a7a0630c5e42ee7da5bb550e400c0fe8965
BLAKE2b-256 checksum
How to use checksums
097a2aafdb06da09ae34b2c587a7955b91b6c7a75c8d212a8311bda3e1dd0f66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.2.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Release files / pycgnat-1.0.0-py3-none-any.whl

Download URL pycgnat-1.0.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d8ca84362751d2deca2abdbd1ae345c2aac9340c70fbf126ceb2a341b4ccf7ec
BLAKE2b-256 checksum
How to use checksums
671624f209bce4f1aa8fae7209bd0e890f111348fd768557cfdb3f9d985f4bc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.2.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Security for pycgnat 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page