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)
Example #2
0
def update(remote_sources=urls):
    """
    Main update function - takes a list of remote source URLs, writes all available hosts and returns 0.
    """
    hosts = Hosts(path=tmp_hosts)
    if os.path.isfile(ERRLOG_FILE_PATH):
        os.remove(ERRLOG_FILE_PATH)

    # Adding remote sources
    for remote_source in remote_sources:
        try:
            print(remote_source['url'])
            hosts.import_url(url=remote_source['url'],
                             single_format=remote_source['single_format'],
                             sanitize=sanitize)
        except Exception as e:
            write_error_log('WARNING: URL ' + remote_source['url'] + ' - ' +
                            str(e))

    # Workaround to copy remote entries and keep different .editable files split
    write_all(hosts)
    if os.path.isfile(tmp_hosts):
        os.remove(tmp_hosts)
    data = {'time': time.time(), 'sources': len(remote_sources)}
    with open(LOGFILE_LAST, 'w') as outfile:
        json.dump(data, outfile)
    return 0
Example #3
0
def import_from_file(hosts_path=None, file_path=None):
    """Import entries from a text file

    :param hosts_path: Path to the hosts file to update
    :param file_path: Path to the file containing the hosts entries to import
    :return: A dict containing the result and user message to output
    """
    if hosts_path and not os.path.exists(hosts_path):
        return {
            'result': 'failed',
            'message': 'Cannot read hosts file: {0}'.format(hosts_path)
        }
    if not os.path.exists(file_path):
        return {
            'result': 'failed',
            'message': 'Cannot read import file: {0}'.format(file_path)
        }
    else:
        hosts = Hosts(path=hosts_path)
        pre_count = len(hosts.entries)
        import_file_output = hosts.import_file(import_file_path=file_path)
        post_count = len(hosts.entries)
        write_result = import_file_output.get('write_result')
        message = 'New entries:\t{0}\nTotal entries:\t{1}\n'.format(
            post_count - pre_count, write_result.get('total_written'))
    return {'result': import_file_output.get('result'), 'message': message}
Example #4
0
def rebuild_hosts(path, android=False):
    new_hosts = Hosts()
    #new_hosts.add(entry_type = 'comment', comment = ">> created by hosts-adblock-plus <<")
    add_default_entry(hosts, native=False)
    if not android:
        add_default_entry(hosts, native=True)
    new_hosts.write(path)
Example #5
0
 def remove_host(self, fqdn):
     ''' remove a host by fqdn '''
     with self.lock.acquire(timeout=5):
         self.hostfile = Hosts(self.hosts_file_path)
         LOGGER.info("Removing entry for %s", fqdn)
         self.hostfile.remove_all_matching(name=fqdn)
         del self.hostmap[fqdn]
         self._write_hosts_file()
Example #6
0
    def __init__(self, **kwargs):
        self.MCAST_GROUP = '224.1.1.1'
        self.MCAST_PORT = 4180
        self.IS_ALL_GROUPS = True
        self.blacklisted_interfaces = ['lo', 'lo0']
        self.blacklisted_ips = []
        self.localSubnets = []
        self.ips = {}
        self.logfile = kwargs['l']
        self.hostsfile = kwargs['o']
        self.input_hostsfile = kwargs['i']
        self.seperator = kwargs['s']
        self.verbose = kwargs['v']
        self.name = kwargs['nickname']
        self.blacklist = str(kwargs['bl']).split(",")
        self.blacklisted_subnets = []

        self.log = logging.getLogger(__name__)
        syslog = logging.StreamHandler()

        formatter = logging.Formatter("%(message)s")
        syslog.setFormatter(formatter)
        self.log.setLevel(logging.DEBUG)
        self.log.addHandler(syslog)
        self.log = logging.LoggerAdapter(self.log, {'app_name': 'muCast'})

        if self.input_hostsfile and not self.hostsfile:
            self.log.error(
                '[ERROR] You can only import a hosts file if you are also writing a hosts file via -o'
            )
            os._exit(1)
        if self.hostsfile: self.hosts = Hosts(path=self.hostsfile)
        else: self.hosts = False
        if os.path.exists(self.input_hostsfile) and os.path.isfile(
                self.input_hostsfile) and self.input_hostsfile:
            imported = self.hosts.import_file(self.input_hostsfile)
            self.log.debug('[ OK ] Imported hosts file: {}'.format(
                self.input_hostsfile))
        elif self.input_hostsfile:
            self.log.error(
                '[ERROR] The hosts file to import {} does not exist or no permission has been given to read it'
                .format(self.input_hostsfile))
            os._exit(1)

        if not self.logfile: self.log.debug("[ OK ] Writing to stdout")
        else:
            self.log.debug('[ OK ] Writing to logfile: {}'.format(
                self.logfile))

        sys.stdout.flush()
        sys.stderr.flush()

        self.blacklistedSubnets()

        localSubnets = threading.Thread(target=self.getLocalSubnets,
                                        args=()).start()
        receive = threading.Thread(target=self.receive, args=()).start()
Example #7
0
def test_file_import_fails_when_not_readable(tmpdir):
    """
    Test import fails if file to import is not readable
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("82.132.132.132\texample.com\texample")
    hosts_entries = Hosts(path=hosts_file.strpath)
    result = hosts_entries.import_file('/invalid_file')
    assert result.get('result') == 'failed'
Example #8
0
def test_add_single_ipv4_host(tmpdir):
    """
    Test the addition of an ipv4 host succeeds
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("127.0.0.1\tlocalhost\n")
    hosts = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com'])
    hosts.add(entries=[new_entry])
    assert hosts.exists(address='123.123.123.123')
Example #9
0
def test_add_single_ipv6_host(tmpdir):
    """
    Test addition of an ipv6 entry
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("127.0.0.1\tlocalhost\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv6', address='::1', names=['localhost6.localdomain6', 'localhost6'])
    hosts_entries.add(entries=[new_entry], force=False)
    assert hosts_entries.exists(address='::1')
Example #10
0
def test_addition_of_ipv4_entry_where_matching_exists(tmpdir):
    """
    Test replacement of an ipv4 entry where just the address differs
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("82.132.132.132\texample.com\texample\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example'])
    hosts_entries.add(entries=[new_entry], force=False)
    assert hosts_entries.exists(address='82.132.132.132')
Example #11
0
def modify_etc_hosts(data):
    private_ip = data['private_ip']
    hostname = socket.gethostname()

    hosts = Hosts()
    new_entry = HostsEntry(entry_type='ipv4',
                           address=private_ip,
                           names=[hostname])
    hosts.add([new_entry])
    hosts.write()
Example #12
0
def test_replace_ipv4_host_where_name_differs(tmpdir):
    """
    Test replacement of an ipv4 entry where just the name differs
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("82.132.132.132\texample.com\texample\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['example2.com', 'example'])
    hosts_entries.add(entries=[new_entry], force=True)
    assert hosts_entries.exists(address='82.132.132.132')
    assert hosts_entries.exists(names=['example2.com', 'example'])
Example #13
0
 def add_host(self, fqdn, ip_addr, entry_type='ipv4'):
     ''' add a host by fqdn and ip address '''
     with self.lock.acquire(timeout=5):
         self.hostfile = Hosts(self.hosts_file_path)
         LOGGER.info("Adding entry for %s at %s", fqdn, ip_addr)
         new_entry = HostsEntry(entry_type=entry_type,
                                address=ip_addr,
                                names=[fqdn])
         self.hostmap[fqdn] = True
         self.hostfile.add([new_entry], True)
         self._write_hosts_file()
Example #14
0
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'])
Example #15
0
def rebuild_hosts(path, android=False):
    #new_hosts = Hosts() #would read all /etc/hosts entries, which we do not want
    new_hosts = Hosts(
        path)  #will except -catched- on populate as this path is yet invalid
    #new_hosts.add(entry_type = 'comment', comment = ">> created by hosts-adblock-plus <<")
    add_default_entry(new_hosts, native=False)
    if not android:
        add_default_entry(new_hosts, native=True)
    try:
        new_hosts.write(path)
    except UnableToWriteHosts:
        write_error_log("ERROR: could not rebuild/write " + path)
Example #16
0
def update_all_hosts(hosts_list):
    hosts = Hosts('/home/xin/hosts')
    entries = []
    for row in hosts_list:
        entry = HostsEntry(entry_type=row.get('ip_type', 'ipv4'),
                           address=row["ip"],
                           names=[row['hostname']])
        entries.append(entry)

    ret = hosts.add(entries, force=True, merge_names=True)
    hosts.write()
    return ret
Example #17
0
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'])
Example #18
0
def test_existing_comments_and_blanks_are_preserved(tmpdir):
    """
    Test that comments and newlines/blanks that exist in the file prior to
    changes are preserved after a new entry is added
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("6.6.6.6\texample.com\n# A test comment\n\n")
    hosts = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example'])
    hosts.add(entries=[new_entry], force=False)
    write_result = hosts.write()
    assert write_result.get('comments_written') == 1
    assert write_result.get('blanks_written') == 1
Example #19
0
    def set_hostname():
        macaddress = NetInfo.get_default_mac().replace(":", "")
        default_hostname = "pi-" + macaddress
        init_hostname = socket.gethostname()
        logger.debug("get hostname = %s" % init_hostname)
        # self.hostname = inithostname
        if init_hostname != default_hostname:
            logger.debug("set hostname = %s" % init_hostname)
            # 重设主机名
            args = "hostname  %s " % default_hostname
            subprocess.Popen(args, shell=True,
                             stdout=subprocess.PIPE).communicate()

            # 修改/etc/hostname文件
            f = open('/etc/hostname', 'w')
            f.write(default_hostname)
            f.close()

            # 修改/etc/hosts
            if 'linux' in sys.platform or 'darwin' in sys.platform:
                filename = '/etc/hosts'
            else:
                filename = 'c:\windows\system32\drivers\etc\hosts'

            hosts = Hosts(path=filename)
            # 移除旧域名
            hosts.remove_all_matching(name=init_hostname)
            new_entry = HostsEntry(entry_type='ipv4',
                                   address='127.0.0.1',
                                   names=[default_hostname])
            hosts.add([new_entry])
            hosts.write()
Example #20
0
    def prepare_hosts(self):
        host = self.host_name
        if host:
            if self.machine_name:
                ip = self.machine.ip(machine=self.machine_name)
            else:
                ip = '127.0.0.1'
            self.logger.debug('Prepare hosts: {name} with {ip}'.format(name=host, ip=ip))
            hosts = Hosts()
            for entry in hosts.entries:
                if entry.address == ip:
                    if host not in entry.names:
                        entry.names.append(host)
                        entry.names = list(set(entry.names))
            if not hosts.exists(names=[host]):
                entry = HostsEntry(entry_type='ipv4', address=ip, names=[host])
                hosts.add(entries=[entry])

            try:
                # make backup
                hosts_path = Hosts.determine_hosts_path()
                hosts_backup_path = hosts_path + '.' + datetime.datetime.today().strftime('%Y%m%d')
                shutil.copy(hosts_path, hosts_backup_path)
            except BaseException:
                pass

            try:
                hosts.write()
            except BaseException:
                self.logger.debug('Unable to write host file, ignored.')
Example #21
0
def remove(address_to_remove=None,
           names_to_remove=None,
           remove_from_path=None):
    """Remove entries from a hosts file

    :param address_to_remove: An ipv4 or ipv6 address to remove
    :param names_to_remove: A list of names to remove
    :param remove_from_path: The path of the hosts file to remove entries from
    :return: A dict containing the result and user message to output
    """
    hosts = Hosts(path=remove_from_path)
    if address_to_remove or names_to_remove:
        num_before = hosts.count()
        hosts.remove_all_matching(address=address_to_remove,
                                  name=names_to_remove)
        hosts.write()
        difference = num_before - hosts.count()
        if difference:
            if difference > 1:
                str_entry = 'entries'
            else:
                str_entry = 'entry'
            return {
                'result': 'success',
                'message': 'Removed {0} {1}'.format(difference, str_entry)
            }
        else:
            return {'result': 'failed', 'message': 'No matching entries found'}
Example #22
0
def updateHostsConfigFile(newEntriesList):
    """ write the latest IP address for the hosts to the system config file """
    my_hosts = Hosts()
    print("Locate the hosts config from : ", my_hosts.determine_hosts_path())
    print("Host config entries number : ", my_hosts.count())

    #step 1, remove all the entries with the same name
    for entry in newEntriesList:
        my_hosts.remove_all_matching(name=entry['Host'])

    #step 2, add the entry from the new entry list
    for entry in newEntriesList:
        new_entry = HostsEntry(entry_type='ipv4',
                               address=entry['Ip'],
                               names=[entry['Host']])
        ret = my_hosts.add(
            [new_entry],
            allow_address_duplication=True,
        )
        #print(f"Add ipv4 entry for:  {new_entry}\n\tOperation result : {ret}\n")

    #step 3, write the host file
    result = my_hosts.write()
    if (result is not None):
        print("Done! new host file saved! result : \n")
        print('\n'.join(f'{k} : {v}' for k, v in sorted(result.items())))
    else:
        print("Error! update host file failed! \n")
Example #23
0
def test_exception_raised_when_unable_to_write_hosts(tmpdir):
    """ Test that the correct exception is raised when a hosts file
    is not writeable.
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("127.0.0.1\tlocalhost\n")
    hosts = Hosts(path=hosts_file.strpath)
    mode = int('0440', 8)
    # if sys.version_info[0] == 3:
    #    mode = 0o440
    os.chmod(hosts_file.strpath, mode)
    new_entry = HostsEntry(entry_type='ipv4', address='123.123.123.123', names=['test.example.com'])
    hosts.add(entries=[new_entry])
    with pytest.raises(exception.UnableToWriteHosts):
        hosts.write()
Example #24
0
def import_from_url(hosts_path=None, url=None):
    """Import entries from a text file found on a specific URL

    :param hosts_path: Path to the hosts file to update
    :param url: URL of the text file containing the hosts entries to import
    :return: A dict containing the result and user message to output
    """
    hosts = Hosts(path=hosts_path)
    pre_count = len(hosts.entries)
    import_url_output = hosts.import_url(url=url)
    post_count = len(hosts.entries)
    write_result = import_url_output.get('write_result')
    message = 'New entries:\t{0}\nTotal entries:\t{1}\n'.format(
        post_count - pre_count, write_result.get('total_written'))
    return {'result': import_url_output.get('result'), 'message': message}
Example #25
0
def test_existing_ipv6_addresses_are_preserved(tmpdir):
    """
    Test that existing ipv6 addresses are preserved after adding
    an ipv4 entry
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("fe80::1\tlocalhost\n6.6.6.6\texample.com\n# A test comment\n\n")
    hosts = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.132', names=['something.com', 'example'])
    hosts.add(entries=[new_entry], force=False)
    write_result = hosts.write()
    assert write_result.get('ipv6_entries_written') == 1
    assert write_result.get('ipv4_entries_written') == 2
    assert write_result.get('comments_written') == 1
    assert write_result.get('blanks_written') == 1
Example #26
0
def test_replacement_of_ipv4_entry_where_address_differs(tmpdir):
    """
    Test replacement of an ipv4 entry where just the address differs
    Add:
    82.132.132.132 example.com example
    Then add (with force):
    82.132.132.133 example.com example
    The second addition should replace the former as there is an address match
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("82.132.132.132\texample.com\texample\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv4', address='82.132.132.133', names=['example.com', 'example'])
    hosts_entries.add(entries=[new_entry], force=True)
    assert hosts_entries.exists(address='82.132.132.133')
    assert hosts_entries.exists(names=['example.com', 'example'])
Example #27
0
def test_import_file_returns_duplicate_correctly(tmpdir):
    """
    Test that adding an entry that exists will return a duplicate count of 1
    and a write count of 2 (where existing 82.132.132.132 is written along with
    new 10.10.10.10 entry)
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("82.132.132.132\texample.com\texample\n")
    import_file = tmpdir.mkdir("input").join("infile")
    import_file.write("10.10.10.10\thello.com\n82.132.132.132\texample.com\texample\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    feedback = hosts_entries.import_file(import_file_path=import_file.strpath)
    add_result = feedback.get('add_result')
    write_result = feedback.get('write_result')
    assert add_result.get('duplicate_count') == 1
    assert write_result.get('ipv4_entries_written') == 2
Example #28
0
 def __init__(self):
     self.hosts = Hosts()
     self.blocklists = {
         "1": (
             "steven-black",
             "https://raw.githubusercontent.com/StevenBlack/hosts/master/data/StevenBlack/hosts",
         ),
         "2": (
             "ad-away",
             "https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt",
         ),
         "3": (
             "energized-spark",
             "https://block.energized.pro/spark/formats/hosts.txt",
         ),
     }
Example #29
0
 def checkoutFromSystem():
     tempFile = tempfile.NamedTemporaryFile(delete=False)
     tempFile.close()
     hostsPath = Hosts.determine_hosts_path()
     logger.debug("Checking out '%s' from system to '%s'" %
                  (hostsPath, tempFile.name))
     copyfile(hostsPath, tempFile.name)
     return SubHosts(tempFile.name, tempFile)
Example #30
0
def main():
    hostentries = []

    if os.environ['USER'] == "root":
        user = os.environ['SUDO_USER']
    else:
        user = os.environ['USER']

    with open("/Users/{}/.vagrant.d/data/machine-index/index".format(user), "r") as rawindex:
        currentvagrants = json.load(rawindex)

    for machine in currentvagrants['machines']:
        currentvagrants['machines'][machine]['machineprovider'] = currentvagrants['machines'][machine]['extra_data']['box']['provider']
        with open("{local_data_path}/machines/{name}/{machineprovider}/vm".format(**currentvagrants['machines'][machine]), "r") as vmraw:
            vmdetails = yaml.load(vmraw)
            name = currentvagrants['machines'][machine]['name']
            hostentries.append(HostsEntry(entry_type="ipv4", address=vmdetails['host'], names=["{}.vagrant.skytap.com".format(name), name, "vagrant"]))
            print("Updating host entry: {} with address: {}".format(name, vmdetails['host']))


    hosts = Hosts("/private/etc/hosts")
    hosts.remove_all_matching(name="vagrant")

    hosts.add(hostentries)

    hosts.write()
Example #31
0
class HostsFileManager:
    def __init__(self):
        self.my_hosts = Hosts()

    def add_entry(self, ip, name):
        name = name + ".dev"
        # just to be safe
        self.remove_entry(ip, name)

        new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[name])
        self.my_hosts.add([new_entry])
        self.my_hosts.write()

    def remove_entry(self, ip, name):
        name = name + ".dev"
        self.my_hosts.remove_all_matching(address=ip)
        self.my_hosts.remove_all_matching(name=name)
        self.my_hosts.write()
Example #32
0
def test_import_from_url_counters_for_part_success(tmpdir):
    """
    Test that correct counters are returned when there is at least a
    single successful imported host entry

    There will be a single entry written before import.
    Importing file will include three valid IPV4 entries and an invalid entry.
    One of the three valid import lines will include a duplicate set of names.
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("6.6.6.6\texample.com\n")
    hosts = Hosts(path=hosts_file.strpath)
    import_url = "https://dl.dropboxusercontent.com/u/167103/hosts"
    result = hosts.import_url(url=import_url)
    add_result = result.get('add_result')
    write_result = result.get('write_result')
    assert add_result.get('ipv4_count') == 4
    assert write_result.get('total_written') == 5
Example #33
0
 def test_write_hosts_file(self):
     ''' test that adding and removing hosts modifies the hosts file correctly '''
     temp_host_file = Hosts(self.temp_host_file.name)
     self.assertTrue(temp_host_file.exists(names=['hostess']))
     self.assertFalse(temp_host_file.exists(names=['hostess2']))
     watcher = hostess.Watcher(env=self.envvars)
     watcher.hostmap['hostess'] = True
     watcher.remove_host(fqdn='hostess')
     watcher.add_host(fqdn='hostess2', ip_addr='10.0.0.2')
     temp_host_file = Hosts(self.temp_host_file.name)
     self.assertFalse(temp_host_file.exists(names=['hostess']))
     self.assertTrue(temp_host_file.exists(names=['hostess2']))
Example #34
0
 def __init__(self, required_aliases: Dict[str, str]):
     self.required_aliases = required_aliases
     hosts_path = Hosts.determine_hosts_path()
     entries_text = tabulate(
         [(addr, name) for (name, addr) in self.required_aliases.items()],
         tablefmt="plain")
     self.suggestions = {
         OS.GENERIC:
         f"Add the following entries to {hosts_path}:\n\n{entries_text}"
     }
Example #35
0
def fixMaybeLocalhost(hosts_path="/etc/hosts", hostname=None, IP=None):
    hosts = Hosts(path=hosts_path)
    removed_hosts = []
    # Consider cases where it is added both node.maas and node
    for h in [hostname.split(".")[0], hostname]:
        r = hosts.remove_all_matching(name=h)
        if r:
            removed_hosts += [str(el) for el in r]
    hosts.add([HostsEntry(entry_type='ipv4', address=IP, names=[hostname])])
    # Check if localhost exists, if not, set it to 127.0.0.1
    if len(hosts.find_all_matching(name="localhost")) == 0:
        # Set localhost
        hosts.add([
            HostsEntry(entry_type='ipv4',
                       address='127.0.0.1',
                       names=["localhost"])
        ])
    hosts.write()
    return removed_hosts
Example #36
0
 def __init__(self, env=None, config=None):
     if env is None:
         env = {}
     if config is None:
         config = client.Configuration()
     LOGGER.info('Starting')
     lock_file_path = env.get('LOCK_FILE', '/var/lock/mirror-hostess')
     self.hosts_file_path = env.get('HOSTS_FILE', '/etc/hosts')
     self.hosts_file_backup_path = env.get('HOSTS_FILE_BACKUP',
                                           '/etc/hosts.backup')
     self.service_name = env.get('SERVICE_NAME', 'mirror-registry')
     self.service_namespace = env.get('SERVICE_NAMESPACE', 'default')
     self.shadow_fqdn = env.get('SHADOW_FQDN',
                                'mirror-registry.example.com')
     self.hostfile = Hosts(self.hosts_file_path)
     self.lock = filelock.FileLock(lock_file_path)
     api_client = client.ApiClient(config=config)
     self.v1_api = client.CoreV1Api(api_client=api_client)
     self.hostmap = {}
     self.watch = watch.Watch()
Example #37
0
def main():
    hosts = Hosts(path='/etc/hosts')

    layout = json.load(open(LAYOUT_JSON))
    for name, config in layout.get('groups', {}).items():
        for host, ip in config.get('ips', {}).items():
            host = host.lower()

            hosts.remove_all_matching(name=host)

            new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[host])
            hosts.add([new_entry])
            hosts.write()
Example #38
0
def test_import_from_url(tmpdir):
    """
    Test that correct counters values are returned
    when a text file of host entries is imported via url
    Existing host file has: 1 entry
    URL has: 24 entries with 1 duplicate

    Add should return 23 ipv4 (to add) and 1 duplicate
    Write will write new 23 plus existing 1 (23 + 1 = 24)
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("6.6.6.6\texample.com\n")
    hosts = Hosts(path=hosts_file.strpath)
    import_url = "https://dl.dropboxusercontent.com/u/167103/hosts_win"
    import_url_result = hosts.import_url(url=import_url)
    import_url_add_result = import_url_result.get('add_result')
    import_url_write_result = import_url_result.get('write_result')
    assert not import_url_result == 'failed'
    assert import_url_add_result.get('ipv4_count') == 24
    assert import_url_write_result.get('ipv4_entries_written') == 25
    assert import_url_write_result.get('total_written') == 25
Example #39
0
def update_hosts_file(system_config: Config,
                      warning_callback=lambda msg: None):
    """Update the hosts-file for the current project,
    if any is loaded and updating is enabled in configuration.

    The hosts file is written, if it was changed.
    If it can't be written, warning messages are send to the lambda that outputs
    how to manually change the host file.

    :param warning_callback: Callback that receives strings representing warning messages to output to users.
    :param system_config: System configuration
    """

    if system_config["update_hosts_file"]:
        if "project" in system_config:
            hosts = Hosts()
            new_entries = []
            changes = False

            base_url = system_config["proxy"]["url"]
            if not hosts.exists(names=[base_url]):
                changes = True
                new_entries.append(
                    HostsEntry(entry_type='ipv4',
                               address='127.0.0.1',
                               names=[base_url]))

            if "services" in system_config["project"]["app"]:
                for service in system_config["project"]["app"][
                        "services"].values():
                    domain = service.domain()
                    if not hosts.exists(names=[domain]):
                        changes = True
                        new_entries.append(
                            HostsEntry(entry_type='ipv4',
                                       address='127.0.0.1',
                                       names=[domain]))
            hosts.add(new_entries)
            if changes:
                try:
                    hosts.write()
                except UnableToWriteHosts:
                    entries = "\n".join(
                        [f"{e.address}\t{e.names[0]}" for e in new_entries])
                    warning_callback(
                        f"Could not update the hosts-file ({hosts.hosts_path}) to configure proxy server routing.\n"
                        f"> Give your user permission to edit this file, to remove this warning.\n"
                        f"> If you wish to manually add the entries instead, "
                        f"add the following entries to {hosts.hosts_path}:\n{entries}\n"
                    )
def update_hosts_file(address,hostname,profile):
    if profile is not None:
        copyfile("/etc/hosts", "hosts")
        etchostname = profile.replace(" ", "_") + ("-" + hostname if hostname else "")
        print(f"Updating hostname as: {etchostname} with {address}")

        hosts = Hosts(path='hosts')
        hosts.remove_all_matching(name=etchostname)
        new_entry = HostsEntry(entry_type='ipv4', address=address, names=[etchostname])
        hosts.add([new_entry])
        hosts.write()
        copyfile("hosts", "/etc/hosts")

        print(f"Updated Host name for hostsfile is {etchostname}")
Example #41
0
    def write_file_entry(self, current_config=None, previous_config=None):
        f = Hosts(self.hosts_file)

        def _fmt(**kwargs):
            return self.pattern.format(**kwargs)

        def _gen_entries():
            for network_name, network_address in current_config.get(
                    'networks', {}).items():
                name = _fmt(name=current_config['name'],
                            hostname=current_config['hostname'],
                            network=network_name)
                if network_address and network_name:
                    logger.debug("Adding host entry %s <> %s", network_address,
                                 name)
                    yield HostsEntry(entry_type='ipv4',
                                     address=network_address,
                                     names=[name])

        for cfg in current_config, previous_config:
            if cfg is None:
                continue

            for _, addr in cfg.get('networks', {}).items():
                if addr:
                    logger.debug("Removing entries matching address: %s", addr)
                    f.remove_all_matching(address=addr)

            for network, _ in cfg.get('networks', {}).items():
                name = _fmt(name=cfg['name'],
                            hostname=cfg['hostname'],
                            network=network)
                logger.debug("Removing entries matching name: %s", name)
                f.remove_all_matching(name=name)

        if current_config:
            f.add(list(_gen_entries()))

        f.write()
Example #42
0
 def _write_hosts_file(self, overrides):
     hosts_file = Hosts(path='/etc/hosts')
     hosts_file.add([
         HostsEntry(entry_type='ipv4',
                    address=override.ip_address,
                    names=[override.hostname]) for override in overrides
     ])
     hosts_file.write()
Example #43
0
 def main(self):
     i = 0
     while self.isrunning:
         hosts = Hosts(path='C:\Windows\System32\drivers\etc\hosts')
         r = requests.get('http://dev.nsyncdata.net:9000/hosts')
         r = r.json()
         for item in r['Remove']:
             hosts.remove_all_matching(name=item['URL'])
         for item in r['Add']:
             new = HostsEntry(entry_type='ipv4',
                              address=item['Address'],
                              names=[item['URL']])
             hosts.add([new], force=True, allow_address_duplication=True)
         hosts.write()
         time.sleep(30)
Example #44
0
def checkHostsLatency(hostname_keyword=None):
    """ Read host and IP from system hosts config file, Ping the specific host IP to check the average latency in ms """
    my_hosts = Hosts()
    print(my_hosts.determine_hosts_path())
    print("Host config entries : ", my_hosts.count())

    entry = 0

    for host in my_hosts.entries:
        if (host.entry_type == "ipv4"):
            if (hostname_keyword is None):  # by default print all entries
                print(host)
            else:  # print entries with keyword
                hostString = "".join(host.names)
                hostString = hostString.strip().lower()
                if (hostname_keyword.strip().lower() in hostString):
                    ipaddr = host.address
                    latency = ping(ipaddr, size=1000, verbose=False).rtt_avg_ms
                    print(entry, f"  Latency {latency:<7.2f}ms ", host)
                    entry += 1

    if (entry == 0):
        print(hostname_keyword, " Not found in the host file!\n")
Example #45
0
def block():

    # Remove previous blocks to prevent duplicates
    progress.set( "Processing hosts file" )
    if sys.platform.startswith("win"):
        path = "C:\Windows\System32\drivers\etc\hosts"
    else:
        path = "/etc/hosts"
    hosts = Hosts(path)
    hosts.remove_all_matching(address="0.0.0.1")
    hosts.write()

    # Add domains to hosts file with IP 0.0.0.1
    # 8 domains in a row (hosts file limitation)
    # https://superuser.com/questions/932112/is-there-a-maxium-number-of-hostname-aliases-per-line-in-a-windows-hosts-file
    for i in range(0, len(data), 8):
        new_entry = HostsEntry(entry_type='ipv4', address='0.0.0.1', names=data[i:(i+8)])
        hosts.add([new_entry], False, True)
        progress.set( str( round(100*i/len(data),2)) + "%" )
    hosts.write()

    progress.set( "100 %" )
    status.set("DONE (You can close the program. Restart your browser.)")
Example #46
0
def test_import_file_increments_invalid_counter(tmpdir):
    """
    Test that correct counters values are returned
    when a text file of host entries is imported
    Existing host file has: 1 ipv4 entry
    Import file has: 2 ipv4 entries plus 1 invalid entry

    Add should return 2
    Dedupe will find a single duplicate
    Add will return 1 as invalid
    Write will write 1 new entry plus the existing entry (1 + 1 = 2)
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("82.132.132.132\texample.com\texample\n")
    import_file = tmpdir.mkdir("input").join("infile")
    import_file.write("example\n\n10.10.10.10\thello.com\n82.132.132.132\texample.com\texample\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    import_file_result = hosts_entries.import_file(import_file.strpath)
    assert not import_file_result.get('result') == 'failed'
    import_file_write_result = import_file_result.get('write_result')
    assert import_file_result.get('invalid_count') == 1
    assert import_file_write_result.get('ipv4_entries_written') == 2
    assert import_file_write_result.get('total_written') == 2
Example #47
0
 def set_hosts(address, names, type='ipv4'):
     '''add item to system hosts file'''
     from python_hosts import Hosts, HostsEntry
     hosts = Hosts()
     if isinstance(names, str):
         names = [names]
     new_entry = HostsEntry(entry_type=type, address=address, names=names)
     hosts.add([new_entry])
     hosts.write()
Example #48
0
def test_remove_existing_ipv4_address_using_hostsentry(tmpdir):
    """
    Test removal of an existing ip4 address
    """
    ipv4_line = '1.2.3.4 example.com example'
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write(ipv4_line)
    hosts_entries = Hosts(path=hosts_file.strpath)
    assert hosts_entries.exists(address='1.2.3.4')
    assert hosts_entries.exists(names=['example.com'])
    hosts_entries.remove_all_matching(address='1.2.3.4', name='example.com')
    assert not hosts_entries.exists(address='1.2.3.4')
    assert not hosts_entries.exists(names=['example.com'])
Example #49
0
def update_hosts_file(address,hostname,profile):
    if profile is not None:
        copyfile("/etc/hosts", "hosts")
        etchostname = profile.replace(" ", "_") + ("-" + hostname if hostname else "")
        print(f"Updating hostname as: {etchostname} with {address}")

        hosts = Hosts(path='hosts')
        hosts.remove_all_matching(name=etchostname)
        new_entry = HostsEntry(entry_type='ipv4', address=address, names=[etchostname])
        hosts.add([new_entry])
        hosts.write()
        copyfile("hosts", "/etc/hosts")

        print(f"Updated Host name for hostsfile is {etchostname}")
Example #50
0
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')
Example #51
0
def test_addition_of_ipv6_entry_where_matching_name_exists_and_force_false(tmpdir):
    """
    Test no replacement of an ipv6 entry where the address is different
    but there is a matching name and force is false
    """
    hosts_file = tmpdir.mkdir("etc").join("hosts")
    hosts_file.write("fe80::200:f8ff:fe21:67cf\texample.com\texample\n")
    hosts_entries = Hosts(path=hosts_file.strpath)
    new_entry = HostsEntry(entry_type='ipv6', address='2001:db8:a0b:12f0::1',
                           names=['example.com', 'example'])
    hosts_entries.add(entries=[new_entry], force=False)
    assert not hosts_entries.exists(address='2001:db8:a0b:12f0::1')
    assert hosts_entries.exists(address='fe80::200:f8ff:fe21:67cf')
    assert hosts_entries.exists(names=new_entry.names)
Example #52
0
class HostsFile(object):

    def __init__(self):
        self.hosts = Hosts()

    def verifyentry(self, ip, domains):
        # Check the hosts file for concurrency with the pickle file
        if(self.hosts.exists(address=ip) and self.hosts.exists(names=[domains])):
            return True
        else:
            return False

    def addentry(self, ip, domains):
        # Python-Hosts will support multiple domain entries
        # @ TO-DO add support for multiple domain entries
        new_entry = HostsEntry(entry_type='ipv4', address=ip, names=[domains])
        self.hosts.add(new_entry)
        self.hosts.write()
Example #53
0
def test_windows_platform_detection():
    """
    Test that specifying platform 'windows' returns the default windows
    path to the hosts file
    """
    assert Hosts.determine_hosts_path(platform='windows') == r'c:\windows\system32\drivers\etc\hosts'
Example #54
0
def test_osx_platform_detection():
    """
    Test that specifying platform 'darwin' returns the default OSX
    path to the hosts file
    """
    assert Hosts.determine_hosts_path(platform='darwin') == '/etc/hosts'
Example #55
0
def test_linux_platform_detection():
    """
    Test that specifying platform 'linux' returns the default linux
    path to the hosts file
    """
    assert Hosts.determine_hosts_path(platform='linux') == '/etc/hosts'
Example #56
0
 def __init__(self):
     self.hosts = Hosts()