def get_remote_hosts(self): ''' parse remote hosts file into python-hosts ''' self.hosts = Hosts(path='/dev/null') self.logger.debug('Cleaning remote hosts..') for line in self.file_handler.hosts: if self.block_start in line: break line_type = HostsEntry.get_entry_type(line) if line_type in ['ipv4', 'ipv6']: self.hosts.add([HostsEntry.str_to_hostentry(line)]) elif line_type == 'comment': self.hosts.add( [HostsEntry(entry_type='comment', comment=line)]) elif line_type == 'blank': # python_hosts.Hosts.add doesn't seem to work for blank lines. # We'll have to use the internal class methods directly. self.hosts.entries.append(HostsEntry(entry_type="blank")) else: self.logger.warning('Unknown line type in hosts file: %s', line) self.hosts.add( [HostsEntry(entry_type='comment', comment=self.block_start)]) if self.params.log_level == logging.DEBUG: self.logger.debug('Cleaned remote hosts:') for entry in self.hosts.entries: print(' ', entry)
def test_add_adblock_entry_with_force_single_name(tmpdir): """ Test that an addition of an adblock entry replaces one with a matching name if force is True """ ipv4_line = '0.0.0.0 example2.com example3.com' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com') hosts_entries.add(entries=[new_entry], force=True) assert hosts_entries.exists(names=['example.com'])
def test_add_adblock_entry_without_force_multiple_names(tmpdir): """ Test that addition of an adblock entry does not succeed if force is not set and there is a matching name """ ipv4_line = '0.0.0.0 example2.com example3.com' hosts_file = tmpdir.mkdir("etc").join("hosts") hosts_file.write(ipv4_line) hosts_entries = Hosts(path=hosts_file.strpath) new_entry = HostsEntry.str_to_hostentry('0.0.0.0 example.com example3.com') hosts_entries.add(entries=[new_entry], force=False) assert hosts_entries.exists(names=['example2.com'])
def test_write_will_create_path_if_missing(): """ Test that the hosts file declared when constructing a Hosts instance will be created if it doesn't exist """ now = datetime.datetime.now() timestamp = now.strftime('%Y%m%d%H%M%S') hosts_path = '/tmp/testwrite.{0}'.format(timestamp) hosts = Hosts(path=hosts_path) entry = HostsEntry.str_to_hostentry('1.2.3.4 example.com example.org') hosts.add(entries=[entry]) hosts.write() hosts2 = Hosts(path=hosts_path) os.remove(hosts_path) assert hosts2.exists(address='1.2.3.4')
def add(entry_line=None, hosts_path=None, force_add=False): """Add the specified entry :param entry_line: The entry to add :param hosts_path: The path of the hosts file :param force_add: Replace matching any matching entries with new entry :return: A dict containing the result and user message to output """ hosts_entry = HostsEntry.str_to_hostentry(entry_line) if not hosts_entry: output_message({ 'result': 'failed', 'message': '"{0}": is not a valid entry.'.format(entry_line) }) duplicate_entry = False entry_to_add = False hosts = Hosts(hosts_path) add_result = hosts.add(entries=[hosts_entry], force=force_add) if add_result.get('replaced_count'): hosts.write() return { 'result': 'success', 'message': 'Entry added. Matching entries replaced.' } if add_result.get('ipv4_count') or add_result.get('ipv6_count'): entry_to_add = True if add_result.get('duplicate_count'): duplicate_entry = True if entry_to_add and not duplicate_entry: hosts.write() return {'result': 'success', 'message': 'New entry added.'} if not force_add and duplicate_entry: return { 'result': 'failed', 'message': 'New entry matches one or more existing.' '\nUse -f to replace similar entries.' }
def test_str_to_hostentry_returns_fails_with_false(): result = HostsEntry.str_to_hostentry('invalid example.com example') assert not result
def test_str_to_hostentry_ipv6(): str_entry = HostsEntry.str_to_hostentry('2001:0db8:85a3:0042:1000:8a2e:0370:7334 example.com example') assert str_entry.entry_type == 'ipv6' assert str_entry.address == '2001:0db8:85a3:0042:1000:8a2e:0370:7334' assert str_entry.names == ['example.com', 'example']
def test_str_to_hostentry_ipv4(): str_entry = HostsEntry.str_to_hostentry('10.10.10.10 example.com example.org example') assert str_entry.entry_type == 'ipv4' assert str_entry.address == '10.10.10.10' assert str_entry.names == ['example.com', 'example.org', 'example']