Пример #1
0
def get_ipaddresses(raw_lines):
    ipaddress_list = []
    for line in raw_lines:
        if '-' in line:
            try:
                startip = line.split("-")[0]
                endip = line.split("-")[1]
                ipnetwork_list = summarize_address_range(
                    IPv4Address(startip), IPv4Address(endip))
                for ipnetwork in ipnetwork_list:
                    for ip in ipnetwork:
                        if ip.compressed not in ipaddress_list:
                            ipaddress_list.append(ip.compressed)
            except Exception as E:
                print(E)
        else:
            try:
                ipnetwork = IPv4Network(line)
                for ip in ipnetwork:
                    if ip.compressed not in ipaddress_list:
                        ipaddress_list.append(ip.compressed)
            except Exception as E:
                print(E)

    return ipaddress_list
Пример #2
0
    def param_address_range(self, name="address_range"):
        raw_input = self._custom_param.get(name)

        try:
            raw_lines = raw_input.split(",")
        except Exception as E:
            print(E)
            return []
        ipaddress_list = []
        for line in raw_lines:
            if '-' in line:
                try:
                    startip = line.split("-")[0]
                    endip = line.split("-")[1]
                    ipnetwork_list = summarize_address_range(
                        IPv4Address(startip), IPv4Address(endip))
                    for ipnetwork in ipnetwork_list:
                        for ip in ipnetwork:
                            if ip.compressed not in ipaddress_list:
                                ipaddress_list.append(ip.compressed)
                except Exception as E:
                    print(E)
            elif line == "":
                continue
            else:
                try:
                    ipnetwork = IPv4Network(line)
                    for ip in ipnetwork:
                        if ip.compressed not in ipaddress_list:
                            ipaddress_list.append(ip.compressed)
                except Exception as E:
                    logger.exception(E)

        return ipaddress_list
Пример #3
0
 def gen_nets(self, opts, args):
     for row in gen_csv(fileinput.input(args)):
         #handle weird space before quote problems
         lo, hi, cc = [x.strip(' "') for x in row[2:5]]
         lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
         nets = ipaddr.summarize_address_range(lo, hi)
         yield nets, (cc,)
Пример #4
0
 def gen_nets(self, opts, args):
     for row in gen_csv(fileinput.input(args)):
         #handle weird space before quote problems
         lo, hi, cc = [x.strip(' "') for x in row[2:5]]
         lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
         nets = ipaddr.summarize_address_range(lo, hi)
         yield nets, (cc,)
Пример #5
0
 def gen_nets(self, opts, args):
     for row in gen_csv(fileinput.input(args)):
         lo, hi = row[2:4]
         lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
         nets = ipaddr.summarize_address_range(lo, hi)
         #v6 postal_code is after lat/lon instead of before like v4
         country, region, city, lat, lon, postal_code, metro_code, area_code = row[4:]
         yield nets, (country, region, city, postal_code, lat, lon, metro_code, area_code)
Пример #6
0
 def gen_nets(self, opts, args):
     for row in gen_csv(fileinput.input(args)):
         lo, hi = row[2:4]
         lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
         nets = ipaddr.summarize_address_range(lo, hi)
         #v6 postal_code is after lat/lon instead of before like v4
         country, region, city, lat, lon, postal_code, metro_code, area_code = row[4:]
         yield nets, (country, region, city, postal_code, lat, lon, metro_code, area_code)
Пример #7
0
def get_one_result(raw_line, proto):
    try:
        proto_default_port = {
            'ftp': 21,
            'ssh': 22,
            'rdp': 3389,
            'smb': 445,
            'mysql': 3306,
            'mssql': 1433,
            'redis': 6379,
            'mongodb': 27017,
            'memcached': 11211,
            'postgresql': 5432,
            'vnc': 5901,
            "http": 80,
            "ssl/http": 443,
            "https": 443
        }
        if len(raw_line.split(":")) < 2:
            # 没有填写端口,使用默认端口
            port = proto_default_port.get(proto)
        else:
            port = int(raw_line.split(":")[1])
        line = raw_line.split(":")[0]
    except Exception as E:
        print(E)
        return []
    result = []
    ipaddress_list = []
    if '-' in line:
        try:
            startip = line.split("-")[0]
            endip = line.split("-")[1]
            ipnetwork_list = summarize_address_range(IPv4Address(startip),
                                                     IPv4Address(endip))
            for ipnetwork in ipnetwork_list:
                for ip in ipnetwork:
                    if ip.compressed not in ipaddress_list:
                        ipaddress_list.append(ip.compressed)
        except Exception as E:
            print(E)
    else:
        try:
            ipnetwork = IPv4Network(line)
            for ip in ipnetwork:
                if ip.compressed not in ipaddress_list:
                    ipaddress_list.append(ip.compressed)
        except Exception as E:
            print(E)
    # service = one_portscan_result.get("service").lower()
    # ipaddress = one_portscan_result.get("ipaddress")
    # port = one_portscan_result.get("port")
    # 读取mysql.txt,redis.txt中的ip地址
    for ip in ipaddress_list:
        result.append({"ipaddress": ip, "port": port, "service": proto})
    return result
Пример #8
0
def get_ip_range(s):
	global regobj;
	#print s;
	ipaddrs = regobj.findall(s);
	#print ipaddrs;
	if len(ipaddrs)<2 :
		return "Bad";
	addr_range = ipaddr.summarize_address_range(ipaddr.IPAddress(ipaddrs[0].strip()) , ipaddr.IPAddress(ipaddrs[1].strip()) );
	
	return_str = "%s" %addr_range[0];
	return return_str
Пример #9
0
    def gen_nets(self, opts, args):
        id_loc = None
        if opts.locations:
            id_loc = dict((row[0], row[1:]) for row in gen_csv(open(opts.locations)))

        for row in gen_csv(fileinput.input(args)):
            lo, hi = row[:2]
            loc = row[2:]
            if id_loc:
                loc = id_loc[loc[0]]
            lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
            nets = ipaddr.summarize_address_range(lo, hi)
            yield nets, tuple(loc)
Пример #10
0
 def match_ip(self, ip):
   match = False
   for x in self.ip_mappings.split(','):
     if x.find('-') > 0:
       ip1 = ipaddr.IPv4Address(x.split('-')[0])
       ip2 = ipaddr.IPv4Address(x.split('-')[1])
       for net in ipaddr.summarize_address_range(ip1,ip2):
         if ipaddr.IPv4Network(ip) in net:
           match = True
     else:
       if ipaddr.IPv4Address(ip) == ipaddr.IPv4Address(x):
         match = True
   return match
Пример #11
0
    def gen_nets(self, opts, args):
        id_loc = None
        if opts.locations:
            id_loc = dict((row[0], row[1:]) for row in gen_csv(open(opts.locations)))

        for row in gen_csv(fileinput.input(args)):
            lo, hi = row[:2]
            loc = row[2:]
            if id_loc:
                loc = id_loc[loc[0]]
            lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
            nets = ipaddr.summarize_address_range(lo, hi)
            yield nets, tuple(loc)
Пример #12
0
    def is_valid_ip_range(iprange, version=None):
        """ Check validity of iprange
            Return True if 'iprange' is a range of ip addresses in a format that
            Qualys's API will accept (i.e. "startip-endip" where
            startip < endip).
        """
        # Validate version:
        if version:
            if not isinstance(version, int):
                raise TypeError('Version is not of type "int"')
                return False
            if not (version == 4 or version == 6):
                raise ValueError('IP version is set to an invalid number: %s' %
                                 version)
                return False

        try:
            (start_ip, end_ip) = iprange.split('-')
            if ipaddr.IPAddress(start_ip) == ipaddr.IPAddress(end_ip):
                logger.debug('%s/%s-%s, Error: %s' %
                              (package,module,version,
                               'Start and End IP Address in an IP Range can' \
                               'not be the same IP Address.'))
                return False
            # A valid range requires:
            # 1) The start_ip must be a valid ipv4 address.
            # 2) The end_ip must be a valid ipv4 address.
            # 3) The start_ip must be less than the end_ip.
            # Although socket operations are nice (as shown in the ipaddr free
            #  code in qualysconnect.util), it's not feasible to determine that the
            #  start_ip is less than the end_ip without considerable effort.
            # We'll use the ipaddr.summarize_address_range function to test all
            #  three at one time.
            ipaddr.summarize_address_range(ipaddr.IPAddress(start_ip, version),
                                           ipaddr.IPAddress(end_ip, version))
        except ipaddr.AddressValueError, e:
            logger.debug('%s/%s-%s, Error: %s' % (package, module, version, e))
            return False
Пример #13
0
def expandrange(ip1, ip2):
    #        print ipaddr.summarize_address_range(ipaddr.IPv4Address(ip1),ipaddr.IPv4Address(ip2))
    #print listx
    listrange = []
    range1 = [
        fipaddr.with_prefixlen for fipaddr in ipaddr.summarize_address_range(
            ipaddr.IPv4Address(ip1), ipaddr.IPv4Address(ip2))
    ]
    for q in range1:
        #print q
        ipraw = str(q)
        #ipraw = ipraw.replace('/', ' ')
        #print ipraw
        listrange.append(ipraw)
    return listrange
    listrange
Пример #14
0
    def __init__(self):
        self.log = logging.getLogger("avocado.test")
        parser = cartesian_config.Parser()
        cfg = os.path.join(settings.get_value('datadir.paths', 'base_dir'),
                           'config/tests.cfg')
        parser.parse_file(cfg)
        dicts = parser.get_dicts()
        self.ips_list = []
        self.post_check = 'false'
        execute_flag = True
        for params in (_ for _ in dicts):
            if execute_flag:
                self.params = params
                self.post_check = params.get('perform_health_check_after_job')
                host_list = params.get("host_ips")
                if host_list:
                    host_list = host_list.split(",")
                else:
                    return
                for item in host_list:
                    if item.find("/") != -1:
                        for ip_info in ipaddr.IPv4Network(item):
                            self.ips_list.append(str(ip_info))
                    elif item.find("-") != -1:
                        begin_ip, end_ip = item.split("-")
                        ip_ranges = ipaddr.summarize_address_range(
                            ipaddr.IPv4Address(begin_ip),
                            ipaddr.IPv4Address(end_ip))
                        for ip_range in ip_ranges:
                            for ip_info in ipaddr.IPv4Network(str(ip_range)):
                                self.ips_list.append(str(ip_info))
                    else:
                        self.ips_list.append(item)

                self.ips_list = sorted(set(self.ips_list),
                                       key=self.ips_list.index)
                self.log.info("All health check ip list:")
                self.log.info(self.ips_list)
                execute_flag = False
            if 'health_check' in params.get('ct_type'):
                self.post_check = params.get('perform_health_check_after_job')
                break
        self.post_check = self.post_check.lower() == "true"
Пример #15
0
    def as_lookup(self, args):

        if not isinstance(args, list):
            args = [args]

        docs = []
        for arg in args:

            as_string = None
            successful = True
            doc = None

            if re.match("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", arg):

                as_string = self.g1_asnum_reader.org_by_addr(arg)

                start, end = self.g1_asnum_reader.range_by_ip(arg)
                start = ipaddr.IPv4Address(start)
                end = ipaddr.IPv4Address(end)
                net_range = ipaddr.summarize_address_range(start, end)
                doc = geo_docs.GeoIpASNDoc()
                setattr(doc, "ip_allocation", str(net_range[0]))

            else:
                as_string = self.g1_asnum_reader.org_by_name(arg)
                doc = geo_docs.GeoASNDoc()

            if as_string:

                as_parts = as_string.split(" ")
                setattr(doc, "as_num", as_parts[0])
                setattr(doc, "as_name", " ".join(as_parts[1:]))
                setattr(doc, "successful", True)

            else:
                setattr(doc, "as_num", 0)
                setattr(doc, "as_name", "")
                setattr(doc, "successful", False)

            docs.append(doc)

        return docs
Пример #16
0
def update():
    content = urllib2.urlopen(KISA_ADDR)
    soup = BeautifulSoup(content)
    for row in soup.select("tbody > tr"):
        cells = row.find_all('td')
        name = cells[0].string.strip()
        name_en = cells[1].string.strip()
        ipaddr_st = IPAddress(cells[2].string)
        ipaddr_ed = IPAddress(cells[3].string)
        assigned_at = cells[5].string.strip()

        networks = summarize_address_range(ipaddr_st, ipaddr_ed)

        for net in networks:
            krip = KRIPAddress(int(net.network),
                               int(net.broadcast),
                               name,
                               name_en,
                               datetime.strptime(assigned_at, '%Y%m%d'))
            db.session.add(krip)
        # int(r.network) < int(s.network) < int(r.broadcast)
        print '%s - %s(%s) : %s' % (ipaddr_st, ipaddr_ed, net, assigned_at)

    db.session.commit()
Пример #17
0
 def gen_nets(self, opts, args):
     for _, _, lo, hi, asn in gen_csv(fileinput.input(args)):
         lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
         nets = ipaddr.summarize_address_range(lo, hi)
         yield nets, (asn,)
Пример #18
0
import ipaddr

with open('nets.txt') as f:
    data = f.readlines()
nets = []
for d in data:
    nets.append(d.strip().split('-'))
for net in nets:
    start = ipaddr.IPv4Address(net[0])
    end = ipaddr.IPv4Address(net[1])
    print(ipaddr.summarize_address_range(start, end)[0])

Пример #19
0
 def gen_nets(self, opts, args):
     for lo, hi, org in gen_csv(fileinput.input(args)):
         lo, hi = ipaddr.IPAddress(lo), ipaddr.IPAddress(hi)
         nets = ipaddr.summarize_address_range(lo, hi)
         #print 'lo %s - li %s - nets %s - org %s' % (lo, hi, nets, org)
         yield nets, (org, )
Пример #20
0
 def gen_nets(self, opts, args):
     for lo, hi, org in gen_csv(fileinput.input(args)):
         lo, hi = ipaddr.IPAddress(lo), ipaddr.IPAddress(hi)
         nets = ipaddr.summarize_address_range(lo, hi)
         #print 'lo %s - li %s - nets %s - org %s' % (lo, hi, nets, org)
         yield nets, (org,)
Пример #21
0
 def gen_nets(self, opts, args):
     for _, _, lo, hi, cc, _ in gen_csv(fileinput.input(args)):
         lo, hi = ipaddr.IPAddress(int(lo)), ipaddr.IPAddress(int(hi))
         nets = ipaddr.summarize_address_range(lo, hi)
         yield nets, (cc,)
Пример #22
0
 def is_ip_in_range(ip_addr, ip_range_start, ip_range_end):
     ip_addr_ranges = summarize_address_range(IPAddress(ip_range_start),
                                              IPAddress(ip_range_end))
     return any(IPAddress(ip_addr) in iprange for iprange in ip_addr_ranges)
Пример #23
0
    def _runTest(self):
        params = self.params

        # If a dependency test prior to this test has failed, let's fail
        # it right away as TestNA.
        if params.get("dependency_failed") == 'yes':
            raise exceptions.TestNotFoundError("Test dependency failed")

        # Report cloud test version
        # logging.info(version.get_pretty_version_info())
        # Report the parameters we've received and write them as keyvals
        self.log.info("Test parameters:")
        keys = params.keys()
        keys.sort()
        for key in keys:
            self.log.info("    %s = %s", key, params[key])

        ips_list = []
        host_list = params.get("host_ips").split(",")
        for item in host_list:
            if item.find("/") != -1:
                for ip_info in ipaddr.IPv4Network(item):
                    ips_list.append(str(ip_info))
            elif item.find("-") != -1:
                begin_ip, end_ip = item.split("-")
                ip_ranges = ipaddr.summarize_address_range(
                    ipaddr.IPv4Address(begin_ip), ipaddr.IPv4Address(end_ip))
                for ip_range in ip_ranges:
                    for ip_info in ipaddr.IPv4Network(str(ip_range)):
                        ips_list.append(str(ip_info))
            else:
                ips_list.append(item)

        ips_list = sorted(set(ips_list), key=ips_list.index)
        self.log.debug("all health check ip list:")
        self.log.debug(ips_list)

        test_passed = True
        health_check_result = {}

        try:
            try:
                try:
                    for host_ip in ips_list:
                        try:
                            health_check = \
                                HealthCheck(host_ip, params,
                                            is_raise_health_check_excp=True)

                            health_check.get_health_status()
                            health_check_result[host_ip] = True
                        except exceptions.TestError:
                            health_check_result[host_ip] = False
                        except exceptions.TestFail:
                            health_check_result[host_ip] = False
                    for key in health_check_result.keys():
                        result = health_check_result[key]
                        if not result:
                            self.log.error("host %s health check failed" % key)
                            test_passed = False
                        else:
                            self.log.info("host %s health check passed" % key)

                    self.verify_background_errors()

                    if not test_passed:
                        raise exceptions.TestFail("health check failed")

                except Exception:
                    # try:
                    #     env_process.postprocess_on_error(self, params, env)
                    # finally:
                    #     self.__safe_env_save(env)
                    self.log.debug("Exception happened during running test")
                    raise

            finally:
                pass
                #     if self.__safe_env_save(env):
                #         env.destroy()   # Force-clean as it can't be stored

        except Exception, e:
            if params.get("abort_on_error") != "yes":
                raise
            # Abort on error
            self.log.info("Aborting job (%s)", e)
Пример #24
0
 def networks(self):
   """Returns a list of networks for the address range"""
   _networks = ipaddr.summarize_address_range(ipaddr.IPAddress(self.start),
                                              ipaddr.IPAddress(self.end))
   return _networks