[python]Netmask to inverse mask

Cisco IOS uses inverse mask in access control list and router network command, only Cisco uses inverse mask instead of actual netmask.

Suppose if the mask is 255.255.255.240, then the inverse mask is 0.0.0.15, another example is 255.255.240.0 then the inverse mask is 0.0.15.255, if you are handling cisco acl with python then there will be a chance inverse mask is used.

Here’s the code to do it.

  • Split the netmask by dot.
  • Subtract each octet by 255.
  • Join the elements in the list by dot in order to get inverse mask
netmask = "255.255.255.240"
conversion = [str(255 - int(octet)) for octet in netmask.split(".")]
print(".".join(conversion))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s