def testPrefixlen(self): """Prefixlen""" self.assertEqual(IPy.IP("127.0.0.1").prefixlen(), 32) self.assertEqual(IPy.IP("::1").prefixlen(), 128) self.assertEqual(IPy.IP("10.0.0.0/24").prefixlen(), 24) self.assertEqual(IPy.IP("10.0.0.0-10.0.0.255").prefixlen(), 24) self.assertEqual(IPy.IP("10.0.0.0/255.255.255.0").prefixlen(), 24) self.assertEqual(IPy.IP("2001::/64").prefixlen(), 64)
def StrToIp(self, ips_type): if ips_type == IPS_TYPE.IPS: ips = args.ip_segment.split('-') # 192.168.0.1-192.168.0.254 if '.' in ips[1]: if self.CheckIp(ips[0]) == False or self.CheckIp( ips[1]) == False: raise ArgsException( 'The entered IP address range is not valid') ip1, ip2 = ips[0].split('.'), ips[1].split('.') for i in range(4): if ip1[i] == ip2[i]: continue if ip1[i] < ip2[i]: break if ip1[i] > ip2[i]: raise ArgsException( 'The entered IP address range is not valid') ip_sum = self.Addr2Dec(ips[1]) - self.Addr2Dec(ips[0]) if ip_sum > int(conf.MAX_IP_COUNT): raise ArgsException( 'The number of IP exceeds the maximum limit, the maximum number is {0}' .format(conf.MAX_IP_COUNT)) self.domain_list.append(ips[0]) start_ip, end_ip = ips[0], ips[1] while (start_ip != end_ip): start_ip = self.GetNextIp(start_ip) if start_ip != '': self.domain_list.append(start_ip) else: # 192.168.0.1-254 if self.CheckIp(ips[0]) == False: raise ArgsException( 'The entered IP address range is not valid') ip1 = ips[0].split('.') if int(ips[1]) < 0 or int(ips[1]) > 255 or int(ip1[3]) > int( ips[1]): raise ArgsException( 'The entered IP address range is not valid') str_ip = str(ip1[0]) + '.' + str(ip1[1]) + '.' + str( ip1[2]) + '.' + "{0}" for number in range(int(ip1[3]), int(ips[1]) + 1): self.domain_list.append(str_ip.format(str(number))) elif ips_type == IPS_TYPE.SUBNETMASK: try: domain_tmp_list = IPy.IP(args.ip_segment) except Exception, e: raise ArgsException( 'The entered IP address range is not valid') for ip in domain_tmp_list: self.domain_list.append(str(ip))
def testInt(self): """Prefixlen""" self.assertEqual(IPy.IP("127.0.0.1").int(), 2130706433) self.assertEqual(IPy.IP("0.0.0.0").int(), 0) self.assertEqual(IPy.IP("255.255.255.255").int(), 0xffffffff) self.assertEqual(IPy.IP("0000:0000:0000:0000:0000:0000:0000:0000").int(), 0) self.assertEqual(IPy.IP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").int(), 0xffffffffffffffffffffffffffffffff) self.assertEqual(IPy.IP("2001:1234:5678:9abc:de00:0000:0000:0000").int(), 42540857391974671903776007410583339008)
def ipToC(): ips = set() readPath = 'ip.txt' writePath = 'ip_c.txt' outFile = open(writePath, 'w') with open(readPath, 'r') as f: for line in f: ip = line.strip('\n') ip_c = IPy.IP(ip).make_net('255.255.255.0') if ip_c not in ips: outFile.write(str(ip_c) + '\n') ips.add(ip_c) outFile.close() print('转换结束')
def get_events_for_ip(self, ip): ip_int = IPy.IP(ip).int() t = Table('event_simple_by_ip', self.metadata,autoload=True) self.current_table = t self._fix_where() e = t.c clauses = list(self.where_clause) clauses.append(or_(e.ip_src==ip_int, e.ip_dst==ip_int)) return select( [e.sig, func.count(e.sig).label("count"), func.min(e.timestamp).label("first"), func.max(e.timestamp).label("last")], whereclause = and_(*clauses), group_by = [e.sig], ).execute()
def _subnet_guess(self, ip): x = IPy.IP(ip).strBin() if x[0] == '0': return ('A', '255.0.0.0') elif x[1] == '0': return ('B', '255.255.0.0') elif x[2] == '0': return ('C', '255.255.255.0') elif x[3] == '0': # multicasting return ('D', None) else: # reserved return ('E', None)
def netIp(preIp=''): Ip = [] ip = preIp[0:preIp.find('/')] netmask = preIp[preIp.find('/') + 1:] if netmask == '32': Ip.append(ip) else: IP = IPy.IP(ip) for i in IP.make_net(netmask)[1:-1]: Ip.append(i) return Ip
def overview(self): text = "This is %s version %s by Daniel Mende - [email protected]\nRunning on %s" % ( self.__class__.__name__, VERSION, PLATFORM) text = urwid.Text(text, 'center') text = urwid.AttrMap(text, 'header') text = urwid.LineBox(text) text = urwid.Filler(text, top=3, bottom=3) text = urwid.BoxAdapter(text, 7) text2 = "Press 'tab' for menu" if self.configured: text2 += "\n\nUsing interface %s\n\n" % self.interface text2 += "IPv4:\n%s/%d\n\nIPv6:\n%s/%d" % ( self.ip, len(IPy.IP(self.mask).strBin().replace("0", "")), self.ip6, len(IPy.IP(self.mask6).strBin().replace("0", ""))) if self.ip6 != self.ip6_ll: text2 += "\n%s/%d" % ( self.ip6_ll, len(IPy.IP(self.mask6_ll).strBin().replace("0", ""))) elif not self.filename is None: text2 += "\n\nReading file %s" % self.filename text2 = urwid.Text(text2, 'center') return urwid.AttrMap( urwid.ListBox(urwid.SimpleListWalker([text, text2])), 'body')
def is_public_ip(ip: str): """ Returns whether it is a public IP address :param str ip: String :return: bool """ try: import IPy if 'PUBLIC' == IPy.IP(ip).iptype(): return True return False except: return False
def is_reserved_ip(ip: str): """ Returns whether it is a reserved IP address :param str ip: String :return: bool """ try: import IPy if 'RESERVED' == IPy.IP(ip).iptype(): return True return False except: return False
def testOverlaps(self): """Overlapping Address Ranges.""" testValues = [('192.168.0.0/23', '192.168.1.0/24', 1), ('192.168.0.0/23', '192.168.0.0/20', 1), ('192.168.0.0/23', '192.168.2.0', 0), ('192.168.0.0/23', '192.167.255.255', 0), ('192.168.0.0/23', '192.168.0.0', 1), ('192.168.0.0/23', '192.168.1.255', 1), ('192.168.1.0/24', '192.168.0.0/23', -1), ('127.0.0.1', '127.0.0.1', 1), ('127.0.0.1', '127.0.0.2', 0)] for (a, b, answer) in testValues: result = IPy.IP(a).overlaps(b) self.assertEqual(answer, result, (a, b, result, answer))
def testStrCompressed(self): """Compressed string Output.""" testValues = [ '127.0.0.1', 'dead::beef', 'dead:beef::', 'dead:beef::/48', 'ff00:1::', 'ff00:0:f000::', '0:0:1000::', '::e000:0/112', '::e001:0/112', 'dead:beef::/48', 'ff00:1::/64', 'ff00:0:f000::/64', '0:0:1000::/64', '::e000:0/112', '::e001:0/112', '::1:0:0:0:2', '0:1:2:3:4:5:6:7', '1:2:3:4:0:5:6:7', '1:2:3:4:5:6:7:0', '1:0:0:2::', '1:0:0:2::3', '1::2:0:0:3' ] for question in testValues: result = IPy.IP(question).strCompressed() self.assertEqual(question, result, (question, result))
def _revoke_rule_args_to_dict(self, context, to_port=None, from_port=None, ip_protocol=None, cidr_ip=None, user_id=None, source_security_group_name=None, source_security_group_owner_id=None): values = {} if source_security_group_name: source_project_id = self._get_source_project_id( context, source_security_group_owner_id) source_security_group = \ db.security_group_get_by_name(context.elevated(), source_project_id, source_security_group_name) values['group_id'] = source_security_group['id'] elif cidr_ip: # If this fails, it throws an exception. This is what we want. cidr_ip = urllib.unquote(cidr_ip).decode() IPy.IP(cidr_ip) values['cidr'] = cidr_ip else: values['cidr'] = '0.0.0.0/0' if ip_protocol and from_port and to_port: from_port = int(from_port) to_port = int(to_port) ip_protocol = str(ip_protocol) if ip_protocol.upper() not in ['TCP', 'UDP', 'ICMP']: raise exception.InvalidIpProtocol(protocol=ip_protocol) if ((min(from_port, to_port) < -1) or (max(from_port, to_port) > 65535)): raise exception.InvalidPortRange(from_port=from_port, to_port=to_port) values['protocol'] = ip_protocol values['from_port'] = from_port values['to_port'] = to_port else: # If cidr based filtering, protocol and ports are mandatory if 'cidr' in values: return None return values
def _is_valid_ip_ipy(ip): """Checks for ip validity using the IPy library A cleaned up version of the IP address string is returned if it is verified, otherwise a false value is returned. """ if isinstance(ip, six.string_types) and not ip.isdigit(): try: valid_ip = IPy.IP(ip) if valid_ip.len() == 1: return str(valid_ip) except ValueError: pass return False
def whois_ip(self, ip): ip = IPy.IP(ip) if ip.version() == 4: self.ipv4 = True else: self.ipv4 = False key = self.__find_best_range(ip) to_return = '' if not key: to_return += 'IP ' + str(ip) + ' not found.\n' else: to_return += self.redis_whois_server.get(key) to_return += self.get_all_informations(key) return to_return
def get_interface_address(config): '''es下普通interface,通过掩码获得已经分配地址段''' address = [] address_data = [] ip = get_ip(config) config_7750 = Config_7750(config) ies = config_7750.get_ies() for item in ies: interface = item.get_interface() p_address = r'address (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(\d\d)' for item2 in interface: sap_name = '' description = '' #排除 pppoe if item2.name == 'pppoe': continue #判断是否有sap p_sap = PAT['sap'] res_sap = re.search(p_sap, item2.config) if res_sap: sap_name = res_sap.group(2) #获取描述 res_description = re.search(PAT['description'], item2.config) if res_description: description = res_description.group(1) res_address = re.findall(p_address, item2.config) address.append(res_address) for item3 in res_address: ips = IPy.IP(item3[0] + '/' + item3[1], make_net=1) for item4 in ips: ip_type = '业务侧' if item4 == ips[0]: ip_type = '网号' elif item4 == ips[-1]: ip_type = '广播' elif item4.strNormal(0) == item3[0]: ip_type = '接口' address_data.append( [ ip, 'interfaceip(三层用户接口)', ip_type, '是', item4, '', item3[1], item2.name, sap_name, '', item.name, '', '', description ] ) return address_data
def get_iplist_from_unofficial(self): print '-> get ip list from UNOFFICIAL source...\n\tbe patient...' import args a = args.args() iplist = [] i1, i2 = a.test_args() for i in xrange(0, len(i1)): p = '0x%s/%s' % (i1[i], i2[i]) ip = IPy.IP(p) for x in ip: iplist.append(str(x)) iplist = {}.fromkeys(iplist).keys() print '\tget %s IPs' % len(iplist) return iplist
def testStrFullsize(self): """Normal / 0-padded string Output.""" testValues = [(338770000845734292534325025077361652240, 'fedc:ba98:7654:3210:fedc:ba98:7654:3210'), (21932261930451111902915077091070067066, '1080:0000:0000:0000:0008:0800:200c:417a'), (338958331222012082418099330867817087043, 'ff01:0000:0000:0000:0000:0000:0000:0043'), (0, '0.0.0.0'), (2130706433, '127.0.0.1'), (4294967295, '255.255.255.255'), (1, '0.0.0.1'), (3588059479, '213.221.113.87')] for (question, answer) in testValues: result = IPy.IP(question).strFullsize(question) self.assertEqual(answer, result, (question, result, answer))
def is_private_ip(ip: str): """ Returns whether it is a private IP address :param str ip: String :return: bool """ try: import IPy if 'PRIVATE' == IPy.IP(ip).iptype(): return True return False except: return False
def db_get_record(key, zone="none"): ''' 获取指定 zone 的单条 record ''' if is_ip(key): #如果输入的是IP地址 ip_addr = IPy.IP(key) ip_rever = ip_addr.reverseNames()[0] # 完整反向解析字符串 ip_spilit = ip_rever.find(".") # 查找分割点 key = ip_rever[0:ip_spilit] # %D zone = ip_rever[ip_spilit + 1:-1] # zone name if zone == "none" or zone == "": return records.objects.filter(Key=key) #返回所有符合的 record return records.objects.filter(ZoneName=zone, Key=key)
def get_pub_ip_from_inet(): ip_from_inet = '' for num in range(1, 3): if num == 1: ip_from_inet = load(urlopen('https://httpbin.org/ip'))['origin'] elif num == 2: ip_from_inet = load(urlopen('https://api.ipify.org/?format=json'))['ip'] else: ip_from_inet = load(urlopen('https://jsonip.com'))['ip'] if IPy.IP(ip_from_inet).version() == 4: break return ip_from_inet
def testStrNormal(self): """Normal string Output.""" testValues = [(338770000845734292534325025077361652240L, 'fedc:ba98:7654:3210:fedc:ba98:7654:3210'), (21932261930451111902915077091070067066L, '1080:0:0:0:8:800:200c:417a'), (338958331222012082418099330867817087043L, 'ff01:0:0:0:0:0:0:43'), (0L, '0.0.0.0'), (2130706433L, '127.0.0.1'), (4294967295L, '255.255.255.255'), (1L, '0.0.0.1'), (3588059479L, '213.221.113.87')] for (question, answer) in testValues: result = IPy.IP(question).strNormal(question) self.failUnlessEqual(answer, result, (question, result, answer))
def CIDRExpand(cidr_block, begin=None, end=None): """Expands a cidr block to a list of ip addreses from begin (integer) to end (integer). Inputs: cidr_block: string of cidr_block begin: integer of ip address to start end: integer of ip address to end Raises: InvalidInputError: Not a valid CIDR block. Outputs: list: list of ip addresses in strings """ try: cidr_block = IPy.IP(cidr_block) except ValueError: raise errors.InvalidInputError('%s is not a valid cidr block' % cidr_block) ip_address_list = [] end_count = -1 length = cidr_block.len() count = 0 start_ip = cidr_block.int() if (begin): start_ip = cidr_block.int() + begin if (begin and end): end_count = end - begin elif (end): end_count = end while (count != end_count and count != length and IPy.IP(start_ip + count) in cidr_block): ip_address_list.append(unicode(IPy.IP(start_ip + count).strFullsize())) count += 1 return ip_address_list
def getNextIP(self, name): rows = self.db.execute("""select subnet, netmask from subnets where name = '%s'""" % (name)) if rows != 1: return None subnet, netmask = self.db.fetchone() ipinfo = IPy.IP('%s/%s' % (subnet, netmask)) bcast = ipinfo.broadcast() net = ipinfo.net() firstip = '%s' % IPy.IP(net.int() + 1) rows = self.db.execute("""select ip from networks""") knownips = [] if rows > 0: knownips = self.db.fetchall() index = 1 ip = None while 1: lastip = '%s' % IPy.IP(bcast.int() - index) if lastip == firstip: break if (lastip,) not in knownips: ip = lastip break index += 1 return ip
def regularcheck(checkfirewall): policymiclist = checkfirewall.policymiclist.copy() regularpolicylist = [] for i in RegularList.regularlist: srcnet = iplocate(i['srcaddr']) dstnet = iplocate(i['dstaddr']) try: routelist = networkx.shortest_path(Topo.nxtopology, source=srcnet, target=dstnet) except: logger.warn(srcnet.name + " to " + dstnet.name + "路径不完整") continue srceth = '' dsteth = '' for k in range(len(routelist)): if routelist[k] == checkfirewall: for port in checkfirewall.portlink: if routelist[k - 1].name in port: srceth = port.split('-')[0] if routelist[k + 1].name in port: dsteth = port.split('-')[0] if not srceth or not dsteth: continue else: for j in policymiclist: if j.srceth == srceth and j.dsteth == dsteth: if IPy.IP(i['srcaddr']) == IPy.IP(j.srcaddr) and IPy.IP( i['dstaddr']).overlaps(j.dstaddr) == 1: if i['protocol'] == j.service['protocol'] and i[ 'port'] == j.service['port']: policymiclist.remove(j) return policymiclist
def main(): china_list_set = IPy.IPSet() for line in sys.stdin: line_params = line.split("|") if len(line_params) < 5 or line_params[2] != "ipv4" or line_params[1] != "CN": continue ip_addr = line_params[3] ip_length = float(line_params[4]) ip_mask = 32 - int(math.ceil(math.log(ip_length, 2))) china_list_set.add(IPy.IP("%s/%d" % (ip_addr, ip_mask))) # 添加内网地址 internal_list = IPy.IPSet(map(IPy.IP, [ "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "112.124.47.0/24", "114.114.114.0/24", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/29", "192.0.2.0/24", "192.88.99.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4", ])) china_list_set += internal_list all = china_list_set # 取反 # all = IPy.IPSet([IPy.IP("0.0.0.0/0")]) # 剔除所有孤立的C段 # for ip in china_list_set: # all.discard(ip) # filter = itertools.ifilter(lambda x: len(x) <= 65536, all) # for ip in filter: # all.discard(ip) # all.add(IPy.IP(ip.strNormal(0)).make_net('255.255.0.0')) # 输出结果 for ip in all: print '<item>' + str(ip) + '</item>'
def process_port_scan(rabbituri,src,dest,rabbituri2=None): lasttime=datetime.now() while(True): try: conn = pika.BlockingConnection(pika.URLParameters(rabbituri)) channel = conn.channel() while(True): (getok,properties,body)=channel.basic_get(src,no_ack=True) priority = default_priority if not body: logger.debug('no tasks...[{0}]'.format(src)) if (datetime.now()-lasttime).total_seconds()>600: lasttime=datetime.now() conn.close() break time.sleep(1) continue lasttime=datetime.now() obj=None try: obj=json.loads(body.decode()) except: logger.error('invalid msg,{0}'.format(body.decode)) if obj: if obj.get('ip') and obj.get('cmd')and obj.get('tag'): priority = properties.priority scan_ip=obj['ip'].encode("ascii") scan_argv = obj['cmd'].encode("ascii") scan_tag = obj['tag'].encode("ascii") ip = IPy.IP(scan_ip) post_data = {} for x in ip: if str(x).split('.')[-1]!=str(0): if str(x).split('.')[-1]!=str(255): logger.debug('task ip:%s,cmd:%s'%(str(x),scan_argv)) t = threading.Thread(target=scan,args=(str(x),scan_argv,scan_tag,priority)) t.start() while True: if threading.activeCount()-1>60: ## time.sleep(1) else: break #if not scan(str(x),scan_argv,scan_tag,priority): # scan(str(x),scan_argv,scan_tag,priority) except Exception as e: logger.error(e.message) time.sleep(10)
def clean(self): # 检查网段 subnet = self.cleaned_data.get('subnet', '') gateway = self.cleaned_data.get('gateway', '') import IPy try: if gateway in IPy.IP(subnet): error = '' else: error = '网关不属于所填的网段中' except: error = '网段填写错误' # import ipdb; ipdb.set_trace() if error: raise forms.ValidationError(error)
def delete_ip(group_id, segments): group_old = PGroup.objects.get(id=group_id) asset_old = group_old.asset_set.all() asset_all = get_all_asset_info() ip_required = [] for segment in segments: for ip in IPy.IP(segment): ip_required.append(str(ip)) for asset in asset_old: if asset.ip not in [asset_info[0] for asset_info in asset_all]: asset.delete() continue elif asset.ip not in ip_required: asset.delete()
def csv_split_to_sheet(assign_subnets_list, log_lines, exl_name): for sub_net_01 in assign_subnets_list: for sub_net_02 in assign_subnets_list: if sub_net_01 != sub_net_02: new_list = [] count = 0 title = [ 'interface', 'action', 'source', 'destination', 'service', 'tcp_udp', 'first_time', 'last_time' ] for line in log_lines: if count == 0: #title = line.split(",") count += 1 else: line_list = line.split(",") src_ip = line.split(",")[2] # Src IP dst_ip = line.split(",")[3] # Dst IP if src_ip in IPy.IP(sub_net_01) and dst_ip in IPy.IP( sub_net_02): new_list.append(line_list) count += 1 if new_list: sheet_name = str( sub_net_01.split("/")[0] + "-" + sub_net_02.split("/")[0]) my_sheet = exl_name.add_sheet(sheet_name) for value in range(len(title)): my_sheet.write(0, value, title[value]) rn = 1 for row in new_list: cn = 0 for _ in range(len(row)): my_sheet.write(rn, cn, row[_]) cn += 1 rn += 1