-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add address_to_mask and address_to_wildcard
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import ipaddress | ||
from ipaddress import IPv4Address | ||
|
||
def address_to_mask(addr): | ||
""" | ||
Transform A.B.C.D/nn into A.B.C.D M.M.M.M subnet mask format. | ||
""" | ||
ipaddr4 = ipaddress.ip_interface(addr) | ||
return f"{ipaddr4.ip} {ipaddr4.netmask}" | ||
|
||
|
||
def address_to_wildcard(addr): | ||
""" | ||
Transform A.B.C.D/nn into A.B.C.D W.W.W.W subnet wildcard format. | ||
credit for the mask shenanigans to George Shuklin | ||
https://medium.com/opsops/wildcard-masks-operations-in-python-16acf1c35683 | ||
""" | ||
ipnet4 = ipaddress.ip_network(addr) | ||
wildcard = str(IPv4Address(int(IPv4Address(ipnet4.netmask))^(2**32-1))) | ||
return f"{ipnet4.network_address} {wildcard}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
from netom.j2_filters import address_to_mask, address_to_wildcard | ||
import pytest | ||
|
||
|
||
def test_address_to_mask(): | ||
assert address_to_mask("192.168.0.1/4") == "192.168.0.1 240.0.0.0" | ||
assert address_to_mask("192.168.0.1/8") == "192.168.0.1 255.0.0.0" | ||
assert address_to_mask("192.168.0.1/9") == "192.168.0.1 255.128.0.0" | ||
assert address_to_mask("192.168.0.1/16") == "192.168.0.1 255.255.0.0" | ||
assert address_to_mask("192.168.0.1/24") == "192.168.0.1 255.255.255.0" | ||
assert address_to_mask("192.168.0.1/25") == "192.168.0.1 255.255.255.128" | ||
assert address_to_mask("192.168.0.1/30") == "192.168.0.1 255.255.255.252" | ||
assert address_to_mask("192.168.0.1/31") == "192.168.0.1 255.255.255.254" | ||
assert address_to_mask("192.168.0.1/32") == "192.168.0.1 255.255.255.255" | ||
|
||
def test_address_to_wildcard(): | ||
assert address_to_wildcard("192.0.0.0/4") == "192.0.0.0 15.255.255.255" | ||
assert address_to_wildcard("192.0.0.0/8") == "192.0.0.0 0.255.255.255" | ||
assert address_to_wildcard("192.0.0.0/9") == "192.0.0.0 0.127.255.255" | ||
assert address_to_wildcard("192.168.0.0/16") == "192.168.0.0 0.0.255.255" | ||
assert address_to_wildcard("192.168.0.0/24") == "192.168.0.0 0.0.0.255" | ||
assert address_to_wildcard("192.168.0.0/25") == "192.168.0.0 0.0.0.127" | ||
assert address_to_wildcard("192.168.0.0/30") == "192.168.0.0 0.0.0.3" | ||
assert address_to_wildcard("192.168.0.0/31") == "192.168.0.0 0.0.0.1" | ||
assert address_to_wildcard("192.168.0.0/32") == "192.168.0.0 0.0.0.0" |