예제 #1
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']))
    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)
예제 #3
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()
예제 #4
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}
예제 #5
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'}
예제 #6
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")
예제 #7
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)
예제 #8
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.')
예제 #9
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
예제 #10
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()
예제 #11
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()
예제 #12
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()
예제 #13
0
파일: src.py 프로젝트: niruhsa/muCast
    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()
예제 #14
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()
예제 #15
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()
예제 #16
0
 def add_host():
     """ Add hosts entry for the server IP """
     hosts = Hosts()
     hosts.add([
         HostsEntry(
             entry_type='ipv4',
             address=GCEMetadata.IP,
             names=GCEMetadata.HOSTS,
         ),
     ])
     hosts.write()
예제 #17
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)
예제 #18
0
파일: deploy.py 프로젝트: a523/storagestack
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
예제 #19
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()
예제 #20
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"
                    )
예제 #21
0
 def edit_host(enable):
     hosts = Hosts()
     if enable:
         entry = HostsEntry(entry_type='ipv4',
                            address='127.0.0.1',
                            names=[
                                'us-or-rly101.zwift.com',
                                'secure.zwift.com', 'cdn.zwift.com',
                                'launcher.zwift.com'
                            ])
         hosts.add([entry])
     else:
         hosts.remove_all_matching(name='cdn.zwift.com')
     logger.info(hosts.write())
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}")
예제 #23
0
def get_localhost_hosts():
    """
    Returns a list of hostnames from the hosts /etc/host that point to 127.0.0.1, as well as RIPTIDE_HOST_HOSTNAME

    The constant IGNORE_LOCAL_HOSTNAMES contains names are exceptions, that are not returned.
    """
    names = [RIPTIDE_HOST_HOSTNAME]

    hosts = Hosts()
    host: HostsEntry = None
    for host in hosts.entries:
        if host.address == '127.0.0.1':
            names += host.names

    return [name for name in names if name not in IGNORE_LOCAL_HOSTNAMES]
예제 #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}
예제 #25
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)
예제 #26
0
    def create(self, vals):
        # Inherit create_portal.plan create method for generating template
        # name from server name and plan name.
        # Give warning if template is already exist.
        saas_server_obj = self.env['saas_portal.server']
        portal_db_obj = self.env['saas_portal.database']
        config = self.env['ir.config_parameter']
        saas_config = self.env['saas_portal.config.settings'].search(
            [], limit=1, order="id desc")
        alies_name = saas_config.base_saas_domain
        if alies_name == False:
            base_saas_rec = config.search(
                [('key', '=', 'saas_portal.base_saas_domain')], limit=1)
            alies_name = base_saas_rec.value
        server_id = vals.get('server_id' or False)
        server_rec = saas_server_obj.browse(server_id)
        server_name = server_rec.name
        default_plan_name = vals.get('db_name')
        new_server_name = self.concate_string(server_name, alies_name)
        new_plan_name = self.concate_string(default_plan_name, alies_name)
        template_name = "template." + new_server_name + '.' + new_plan_name + \
                        '.' + alies_name

        exsiting_templates = portal_db_obj.search(
            [('name', '=', template_name), ('state', '=', 'template'),
             ('server_id', '=', server_id)],
            limit=1)

        if exsiting_templates:
            raise Warning(
                _('Template already exists. Please give Different '
                  'Name of your Plan.'))
        else:
            # Host entry is done for the ip address and database name
            hosts = Hosts(path='/etc/hosts')
            new_entry = HostsEntry(entry_type='ipv4',
                                   address='127.0.0.1',
                                   names=[template_name])
            hosts.add([new_entry])
            hosts.write()
            portal_db_rec = portal_db_obj.create({
                'name': template_name,
                'server_id': server_id
            })
        vals.update({
            'template_id': portal_db_rec.id,
        })
        return super(mint_plan_enhancement, self).create(vals)
예제 #27
0
파일: __init__.py 프로젝트: cuenca-mx/speid
def configure_environment():
    # Descarga la private key de S3
    if 'AWS_ACCESS_KEY_ID' in os.environ:
        s3 = boto3.client('s3')
        s3.download_file(STP_BUCKET_S3, STP_PRIVATE_KEY, STP_PRIVATE_LOCATION)

    # Edita archivo hosts si es necesario
    if os.environ['EDIT_HOSTS'] == 'true':
        host_ip = os.environ['HOST_IP']
        host_ad = os.environ['HOST_AD']
        hosts = Hosts()
        new_entry = HostsEntry(entry_type='ipv4',
                               address=host_ip,
                               names=[host_ad])
        hosts.add([new_entry])
        hosts.write()
예제 #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",
         ),
     }
예제 #29
0
    def check(self) -> CheckResult:
        hosts = Hosts()
        entries = [
            e for e in hosts.entries if e.entry_type in ("ipv4", "ipv6")
        ]
        entries_dict = {}
        for entry in entries:
            for name in entry.names:
                entries_dict[name] = entry.address
        logging.debug(f"Hosts file entries: {entries_dict}")
        for (name, address) in self.required_aliases.items():
            if entries_dict.get(name) != address:
                return self.failed(
                    f"{hosts.hosts_path} alias {name} -> {address} not present"
                )

        return self.passed(f"{hosts.hosts_path} aliases present")
예제 #30
0
def add_host(
    airflow_options: AirflowOptions,
    entry_type: str = "ipv4",
    address: Optional[str] = None,
    force: bool = False,
):
    if address is None:
        address = get_minikube_ip()
    my_hosts = Hosts()
    my_hosts.add(
        [
            HostsEntry(entry_type,
                       address=address,
                       names=[airflow_options.domain_name])
        ],
        force=force,
    )
    my_hosts.write()