Created
June 25, 2019 09:45
-
-
Save rdapaz/f9619fbcff40085cd594441317280c97 to your computer and use it in GitHub Desktop.
Clever Network Maths
This file contains hidden or 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
import sys | |
import os | |
import sqlite3 | |
from ipaddress import IPv4Network | |
from socket import inet_ntoa, inet_aton | |
from struct import pack, unpack | |
def ip2long(ip_addr): | |
return unpack("!L", inet_aton(ip_addr))[0] | |
def long2ip(ip): | |
return inet_ntoa(pack("!L", ip)) | |
cidrFromMask = lambda y: sum(bin(int(x)).count('1') for x in y.split('.')) | |
maskFromCidr = lambda y: '.'.join([str((0xffffffff << (32 - y) >> i) & 0xff) for i in [24, 16, 8, 0]]) | |
print(maskFromCidr(30)) # -> 255.255.255.252 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment