def init(self): self.title = _('Hosts') self.icon = 'sitemap' self.category = _('System') self.append(self.ui.inflate('hosts:main')) self.config = HostsConfig(path='/etc/hosts') self.binder = Binder(None, self.find('hosts-config')) self.find('aliases').new_item = lambda c: AliasData() self.find('hosts').new_item = lambda c: HostData()
class Handler(HttpPlugin): def __init__(self, context): self.context = context @url(r'/api/hosts') @endpoint(api=True) def handle_api_hosts(self, http_context): """ Load (through get) or save ( through post) the hosts file, and always make a backup before saving. Method GET. Method POST. :param http_context: HttpContext :type http_context: HttpContext :return: File content in load mode, success or error in save mode :rtype: dict in load mode, bool or throw error in save mode """ if http_context.method == 'GET': self.hosts_config = HostsConfig(path='/etc/hosts') self.hosts_config.load() return self.hosts_config.tree.to_dict() if http_context.method == 'POST': config = http_context.json_body()['config'] new_hosts = HostsConfig(content='') new_hosts.load() for host in config: new_host = HostData() for prop, value in host.items(): if prop == 'aliases': for alias in value: if alias['name'] != '': new_alias = AliasData() new_alias.name = alias['name'] new_host.aliases.append(new_alias) else: setattr(new_host, prop, value) new_hosts.tree.hosts.append(new_host) data = new_hosts.save()[None] # Always make a backup os.rename('/etc/hosts', '/etc/hosts.bak') try: with open('/etc/hosts', 'w') as f: f.write(data) return True except Exception as e: raise EndpointError(e)
def loads(filename=None): if not filename: filename = guess_hosts_path() mtime = os.path.getmtime(filename) config = HostsConfig(path=filename) config.load() expiration = mtime + _year_in_seconds dnspython_data = _convert_entries(config.tree.hosts, expiration) return dnspython_data
class Handler(HttpPlugin): def __init__(self, context): self.context = context @url(r'/api/hosts') @endpoint(api=True) def handle_api_hosts(self, http_context): if http_context.method == 'GET': self.hosts_config = HostsConfig(path='/etc/hosts') self.hosts_config.load() return self.hosts_config.tree.to_dict() if http_context.method == 'POST': config = http_context.json_body()['config'] new_hosts = HostsConfig(content='') new_hosts.load() for host in config: new_host = HostData() for property, value in host.items(): if property == 'aliases': for alias in value: if alias['name'] != '': new_alias = AliasData() new_alias.name = alias['name'] new_host.aliases.append(new_alias) else: setattr(new_host, property, value) new_hosts.tree.hosts.append(new_host) data = new_hosts.save()[None] # Always make a backup os.rename('/etc/hosts', '/etc/hosts.bak') try: with open('/etc/hosts', 'w') as f: f.write(data) return True except Exception as e: raise EndpointError(e)
class Hosts(SectionPlugin): def init(self): self.title = _('Hosts') self.icon = 'sitemap' self.category = _('System') self.append(self.ui.inflate('hosts:main')) self.config = HostsConfig(path='/etc/hosts') self.binder = Binder(None, self.find('hosts-config')) self.find('aliases').new_item = lambda c: AliasData() self.find('hosts').new_item = lambda c: HostData() def on_page_load(self): self.config.load() self.binder.setup(self.config.tree).populate() @on('save', 'click') def save(self): self.binder.update() self.config.save()
class Hosts (SectionPlugin): def init(self): self.title = _('Hosts') self.icon = 'sitemap' self.category = _('System') self.append(self.ui.inflate('hosts:main')) self.config = HostsConfig(path='/etc/hosts') self.binder = Binder(None, self.find('hosts-config')) self.find('aliases').new_item = lambda c: AliasData() self.find('hosts').new_item = lambda c: HostData() def on_page_load(self): self.config.load() self.binder.reset(self.config.tree).autodiscover().populate() @on('save', 'click') def save(self): self.binder.update() self.config.save()
if len(argv) < 3: exit(1) if not os.path.isfile(FILE_PATH_HOSTS) or not os.path.isfile(BIN_HOSTNAMECTL): exit(1) hostname_old = unicode(argv[1]) hostname_new = unicode(argv[2]) if hostname_old == hostname_new: exit(0) CMD_HOSTNAMECTL = CMD_HOSTNAMECTL + hostname_new try: config_hosts = HostsConfig(path=FILE_PATH_HOSTS) config_hosts.load() for host_entry in config_hosts.tree.hosts: for alias in host_entry.aliases: if alias.name != hostname_old: continue alias.name = hostname_new if host_entry.name != hostname_old: continue host_entry.name = hostname_new config_hosts.save()