def __init__(self, scan_task): """Initialize ConnectResultStore object.""" self.scan_task = scan_task source = scan_task.source # Sources can contain patterns that describe multiple hosts, # like '1.2.3.[4:6]'. Expand the patterns so hosts is a list # of single hosts we can try to connect to. hosts, exclude_hosts = [], [] hosts_list = source.get_hosts() exclude_hosts_list = source.get_exclude_hosts() for host in hosts_list: hosts.extend(expand_hostpattern(host)) for host in exclude_hosts_list: exclude_hosts.extend(expand_hostpattern(host)) # Remove excluded ip addresses from the hosts list. hosts = list(set(hosts) - set(exclude_hosts)) self._remaining_hosts = set(hosts) scan_task.update_stats('INITIAL NETWORK CONNECT STATS.', sys_count=len(hosts), sys_scanned=0, sys_failed=0, sys_unreachable=0)
def __init__(self, scan_task, conn_results): """Get the unique TaskConnectionResult object for this scan.""" self.scan_task = scan_task self.conn_results = conn_results source = scan_task.source with transaction.atomic(): conn_result = conn_results.results.filter( source__id=source.id).first() if conn_result is None: conn_result = TaskConnectionResult(scan_task=scan_task, source=source) conn_result.save() conn_results.results.add(conn_result) conn_results.save() self.conn_result = conn_result # If we're restarting the scan after a pause, systems that # were previously up might be down. So we throw out any # partial results and start over. conn_result.systems.all().delete() # Sources can contain patterns that describe multiple hosts, # like '1.2.3.[4:6]'. Expand the patterns so hosts is a list # of single hosts we can try to connect to. hosts = [] hosts_list = source.get_hosts() for host in hosts_list: hosts.extend(expand_hostpattern(host)) self._remaining_hosts = set(hosts) if scan_task.systems_count is None: scan_task.update_stats('INITIAL NETWORK CONNECT STATS.', sys_count=len(hosts), sys_scanned=0, sys_failed=0)
def test_ip_range(self): """A range of IP addresses.""" self.assertEqual(utils.expand_hostpattern('1.2.3.[4:6]'), ['1.2.3.4', '1.2.3.5', '1.2.3.6'])
def test_single_ip_address(self): """A single IP address.""" self.assertEqual(utils.expand_hostpattern('1.2.3.4'), ['1.2.3.4'])
def test_hostname_range(self): """A range of hostnames.""" self.assertEqual(utils.expand_hostpattern('[a:c].domain.com'), ['a.domain.com', 'b.domain.com', 'c.domain.com'])
def test_single_hostname_with_port(self): """Users can specify a port, too.""" self.assertEqual(utils.expand_hostpattern('domain.com:1234'), ['domain.com'])
def test_single_hostname(self): """A single hostname.""" self.assertEqual(utils.expand_hostpattern('domain.com'), ['domain.com'])