code: def ip_check(ip, netmask_expected=False):
      
      try:
          if netmask_expected:
              if "/" not in ip:
                  raise netaddr.core.AddrFormatError()
              netaddr.IPNetwork(ip)
          else:
              netaddr.IPAddress(ip)
      except netaddr.core.AddrFormatError:
          if netmask_expected:
              raise ArgsError("Not a valid CIDR (%s)" % ip)
          else:
              raise ArgsError("Not a valid IP address (%s)" % ip)
      except Exception as e:
          raise ArgsError("Invalid format: %s" % str(e))
docs: Sanity check that the specified string is indeed an IP address or mask.