def exec_command(request, menu, command): context = dict() context['menu'] = menu context['command'] = command context['tree'] = list() context['return'] = Commands.execute(command) for (dirpath, dirnames, filenames) in os.walk( os.path.join(settings.BASE_DIR, 'templates', 'menu')): context['tree'] += [os.path.join(dirpath, file) for file in filenames] target = 'menu/' + menu + '/' + command + '.html' if os.path.exists(os.path.join(settings.BASE_DIR, 'templates', target)): return render(request, target, context) else: return render(request, 'generic/404.html', context)
class PupyCmd(cmd.Cmd): def __init__(self, pupsrv): cmd.Cmd.__init__(self) self.pupsrv = pupsrv self.dnscnc = pupsrv.dnscnc self.config = pupsrv.config self.input = sys.stdin self.output = sys.stdout self.commands = Commands() self.display_lock = Lock() self.init_readline() self._intro = [ Color(BANNER, 'green'), Indent(Color(UPSTREAM, 'cyan')), Indent(Color(DISCLAIMER, 'lightred')) ] self.raw_prompt = colorize('>> ', 'blue') self.prompt = colorize('>> ', 'blue', prompt=True) self.default_filter = None try: if not self.config.getboolean("cmdline", "display_banner"): self._intro = [] except Exception: pass self.aliases = {} for m, _ in self.pupsrv.get_aliased_modules(): self.aliases[m] = m try: for command, alias in self.config.items("aliases"): logger.debug("adding alias: %s => %s" % (command, alias)) self.aliases[command] = alias except: logger.warning("error while parsing aliases from pupy.conf ! %s", traceback.format_exc()) self.pupsrv.register_handler(self) @property def intro(self): return '\n'.join(hint_to_text(x) for x in self._intro) def add_motd(self, motd={}): for ok in motd.get('ok', []): self._intro.append(ServiceInfo(ok + '\n')) for fail in motd.get('fail', []): self._intro.append( Error(fail + '\n') if not issubclass(type(fail), Text) else fail) def default(self, line): return self.execute(line) def inject(self, line, clients_filter, message=None): self.display_srvinfo(message or 'Inject: {}'.format(line)) self.execute(line, clients_filter) self.display_srvinfo('Action complete') def execute(self, line, clients_filter=None): if line.startswith('!'): os.system(line[1:]) return try: self.commands.execute(self.pupsrv, self, self.pupsrv.config, line, clients_filter) self.completion_matches = None except PupyModuleUsageError, e: prog, message, usage = e.args self.display(Line(Error(message, prog))) self.display(usage) except PupyModuleExit: pass
def init(): w = executer.confirm() if not w: return c = creds.init_creds("primary", w) s = creds.Session(c) q = input("press y to continue") if q != "y": return secondary_username = c["secondary_username"] secondary_password = c["secondary_password"] if True: session = executer.start_session(s) builder = Commands() builder.log("adding user") builder.command("sudo -k adduser " + secondary_username) builder.reply("password", secondary_password) builder.reply("password", secondary_password) builder.print() builder.execute(session, s) if False: session = executer.start_session(s) builder = Commands() builder.log("adding user password") builder.command("sudo -k passwd " + secondary_username) builder.reply("password", secondary_password) builder.reply("password", secondary_password) builder.reply("password", secondary_password) builder.print() builder.execute(session, s) if True: session = executer.start_session(s) builder = Commands() builder.log("adding user to sudo group") builder.command("usermod -aG sudo " + secondary_username) # builder.reply("password",secondary_password) builder.print() builder.execute(session, s) if False: session = executer.start_session(s) builder = Commands() builder.log("removing existing home dir") builder.command("cd /home/;sudo -k rm -rf " + secondary_username) builder.reply("password", secondary_password) builder.print() builder.log("adding new home dir") builder.command("cd /home/;sudo -k mkdir " + secondary_username) builder.reply("password", secondary_password) builder.print() builder.log("adding permissions to home dir") builder.command("cd /home/;sudo -k chown " + secondary_username + " " + secondary_username) builder.reply("password", secondary_password) builder.print() builder.execute(session, s)