3

I want to convert netmask to wildcard mask with netaddr library

so the input is netmask = 255.255.255.0 and the output is wildcard = 0.0.0.255

or the input is netmask = 255.255.255.252 and the output is wildcard = 0.0.0.3

ChrisM
  • 505
  • 6
  • 18
Adhy
  • 177
  • 1
  • 1
  • 10
  • Combine a string split, int conversion and `xor 255`. – Klaus D. Apr 14 '19 at 08:52
  • with netaddr libray or no? – Adhy Apr 14 '19 at 09:45
  • Just subtract each octet from 255. There is a section in [this two-part answer](https://networkengineering.stackexchange.com/a/53994/8499) about the host mask (wildcard mask; there is a slight difference in that a host mask like a network mask must be contiguous bits, but a wildcard mask does not need to have contiguous bits) that explains how to calculate the host mask from the network mask. – Ron Maupin Nov 06 '22 at 14:32

2 Answers2

0

netaddr not support non-contiguous wildcard but cisco-acl can help to play with wildcard

from cisco_acl import AddressAg

address = AddressAg("0.0.0.0 255.255.255.0")
mask = address.subnet.split()[1]
wildmask = address.wildcard.split()[1]
print(mask)  # 255.255.255.0
print(wildmask)  # 0.0.0.255
pyjedy
  • 389
  • 2
  • 9
  • The question specifically mentions wanting to use the `netaddr` library. Without an explanation, I don't think this answer answers the question. – Ellis Nov 03 '22 at 09:44
  • Please avoid providing code-only answers without an explanation on why your code resolves the question. While the code may solve the problem, the answer is not useful in understanding why it solves it. – Marc Sances Nov 03 '22 at 10:38
0

Normally, all netmask octets are one of nine possible values "0", "255", "192", "224", "240", "248", "252", "254", or "255". Given this, a simple solution is:

netmask.replace("0", "255").replace("128", "127").replace("192", "63").replace("224", "31").replace("240", "15").replace("248", "7").replace("252", "3").replace("254", "0").replace("255", "0")
Andrew
  • 1
  • 4
  • 19