def calculateIP(self): ipadd = self.ipEntry.get() ipin = self.validateIP(ipadd) option = self.optionVar.get().lower() if ipin: # IP Valid.. ok, let's rock if option == "convert": conto = self.conToVar.get() confrom = self.conFromVar.get() output = "" if conto == confrom: output = ipadd elif conto == "bin": if (ipin == "ipv4"): output = PyNIPv4Address(ipadd).pn_binary else: output = PyNIPv6Address(ipadd).binary elif conto == "hex": if (ipin == "ipv4"): output = PyNIPv4Address(ipadd).pn_hexadecimal else: output = PyNIPv6Address(ipadd).hexadecimal else: if (ipin == "ipv4"): output = PyNIPv4Address(ipadd).pn_decimal else: output = PyNIPv6Address(ipadd).decimal self.setOutput("Original (%s): %s\nConverted (%s): %s" % (confrom, ipadd, conto, output)) else: # show the IP address stats accordingly address = ipadd output = "" is_ipv4 = validator.ipv4address(address) is_ipv6 = validator.ipv6address(address) is_ipv4_net = validator.ipv4network(address) is_ipv6_net = validator.ipv6network(address) if (is_ipv4): addr = PyNIPv4Address(address) output = helpers.show_ipv4_address_stats(addr) elif (is_ipv6): addr = PyNIPv6Address(address) output = helpers.show_ipv6_address_stats(addr) elif (is_ipv4_net): addr = PyNIPv4Network(address) output = helpers.show_ipv4_network_stats(addr) elif (is_ipv6_net): addr = PyNIPv6Network(address) output = helpers.show_ipv6_network_stats(addr) self.setOutput(output) else: messagebox.showerror("Invalid IP", "Please use a valid IP address")
def validateIP(self, ip): address = ip if (not validator.ipv4address(address) and not validator.ipv6address(address) and not validator.ipv4network(address) and not validator.ipv6network(address)): return False try: PyNIPv6Address(address) return "ipv6" except: try: PyNIPv4Address(address) return "ipv4" except: return False
def test_ipv4network(network, expected_result): """Tests for ipv4network function""" result = validator.ipv4network(network) assert result == expected_result
def calculateSubnets(self): subnetLimit = self.limitEntry.get() network = self.netadd.get() hosts = self.hosts.get() subnets = self.snets.get() priorityHosts = self.priorVar.get() scom = self.subcomVar.get() output = '' # Validation if (not validator.ipv4network(network) and not validator.ipv6network(network)): messagebox.showerror( "Invalid Network", "IPv4/IPv6 network address supplied is invalid.") return False isIpv4 = validator.ipv4network(network) isIpv6 = validator.ipv6network(network) if scom == 'flsm': try: subnetLimit = int(subnetLimit) except: subnetLimit = None if not subnetLimit: subnetLimit = None if (not validator.integer(subnets)): messagebox.showerror("Invalid Subnet Number", "Number of subnets must be an integer.") return False elif (not validator.integer(hosts)): messagebox.showerror("Invalid Host Number", "Number of hosts must be an integer.") return False subnetList = [] try: if isIpv4: subnetList = PyNIPv4Network(network).subnets_flsm( int(hosts), int(subnets), priorityHosts) numOfSubnets = PyNIPv4Network(network).num_of_subnets( int(hosts), int(subnets), priorityHosts) output = helpers.show_ipv4_subnet_table( network, hosts, subnets, subnetList, numOfSubnets, subnetLimit) else: subnetList = PyNIPv6Network(network).subnets_flsm( int(hosts), int(subnets), priorityHosts) numOfSubnets = PyNIPv6Network(network).num_of_subnets( int(hosts), int(subnets), priorityHosts) output = helpers.show_ipv6_subnet_table( network, hosts, subnets, subnetList, numOfSubnets, subnetLimit) self.setOutput(output) return True except TypeError: messagebox.showerror( "Invalid Network", "IPv4/IPv6 network address supplied is invalid.") return False except: messagebox.showerror("Error", "Unknown Error Occured") return False elif scom == "vlsm": ss = self.subSize.get() ss = ss.split(',') hosts = list(map(lambda i: int(i), ss)) try: if isIpv4: subnetList = PyNIPv4Network(network).subnets_vlsm(hosts) output = helpers.show_ipv4_subnet_table( network, hosts, len(hosts), subnetList, len(hosts)) else: subnetList = PyNIPv6Network(network).subnets_vlsm(hosts) output = helpers.show_ipv6_subnet_table( network, hosts, len(hosts), subnetList, len(hosts)) self.setOutput(output) return True except ValueError: messagebox.showerror( "Error", "Specified number of hosts or subnets cannot be accommodated" )
# retrieve arguments passed network = arguments['<network-address>'] hosts = arguments['<hosts>'] subnets = arguments['<subnets>'] subnet_limit = arguments['--limit'] if (subnet_limit is None): pass else: try: subnet_limit = int(subnet_limit) except: helpers.show_error("--limit argument must be an integer.") # do some validation first if (not validator.ipv4network(network) and not validator.ipv6network(network)): helpers.show_error( "IPv4/IPv6 network address you supplied is invalid.") sys.exit() elif (not validator.integer(hosts)): helpers.show_error( "number of <hosts> specified must be an integer.") sys.exit() elif (not validator.integer(subnets)): helpers.show_error( "number of <subnets> specified must be an integer.") sys.exit() is_ipv4 = validator.ipv4network(network) is_ipv6 = validator.ipv6network(network)