Python module for generating CGNAT rules using netmap
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pycgnat-1.0.0.tar.gz
.
File metadata
- Download URL: pycgnat-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3c9c2d0c61d4863a8e90402181d1a7a0630c5e42ee7da5bb550e400c0fe8965 |
|
MD5 | c829bce0b64b85151c57c79fec92b4d2 |
|
BLAKE2b-256 | 097a2aafdb06da09ae34b2c587a7955b91b6c7a75c8d212a8311bda3e1dd0f66 |
File details
Details for the file pycgnat-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: pycgnat-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8ca84362751d2deca2abdbd1ae345c2aac9340c70fbf126ceb2a341b4ccf7ec |
|
MD5 | d5543b58c1a721f2da22e5da081454e9 |
|
BLAKE2b-256 | 671624f209bce4f1aa8fae7209bd0e890f111348fd768557cfdb3f9d985f4bc1 |