diff --git a/bin/pwnagotchi b/bin/pwnagotchi index dd1b9b3..1d48137 100755 --- a/bin/pwnagotchi +++ b/bin/pwnagotchi @@ -37,6 +37,8 @@ if __name__ == '__main__': logging.debug("effective configuration:\n\n%s\n\n" % yaml.dump(config, default_flow_style=False)) + pwnagotchi.set_name(config['main']['name']) + plugins.load(config) display = Display(config=config, state={'name': '%s>' % pwnagotchi.name()}) diff --git a/pwnagotchi/__init__.py b/pwnagotchi/__init__.py index caa4bc9..cc1262b 100644 --- a/pwnagotchi/__init__.py +++ b/pwnagotchi/__init__.py @@ -9,6 +9,37 @@ version = '1.1.0b' _name = None +def set_name(new_name): + if new_name is None: + return + + new_name = new_name.strip() + if new_name == '': + return + + current = name() + if new_name != current: + global _name + + logging.info("setting unit hostname '%s' -> '%s'" % (current, new_name)) + with open('/etc/hostname', 'wt') as fp: + fp.write(new_name) + + with open('/etc/hosts', 'rt') as fp: + prev = fp.read() + logging.debug("old hosts:\n%s\n" % prev) + + with open('/etc/hosts', 'wt') as fp: + patched = prev.replace(current, new_name, -1) + logging.debug("new hosts:\n%s\n" % patched) + fp.write(patched) + + _name = new_name + logging.info("restarting avahi ...") + os.system("service avahi-daemon restart") + logging.info("hostname set") + + def name(): global _name if _name is None: diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml index 3d45abd..32db5ad 100644 --- a/pwnagotchi/defaults.yml +++ b/pwnagotchi/defaults.yml @@ -6,6 +6,8 @@ # # main algorithm configuration main: + # if set this will set the hostname of the unit + name: 'pwnagotchi' # currently implemented: en (default), de, el, fr, it, mk, nl, ru, se, pt-BR, es, pt lang: en # custom plugins path, if null only default plugins with be loaded