From 2f36d69f78bc7e2866c5db7e0903586b901efd2d Mon Sep 17 00:00:00 2001 From: byt3bl33d3r Date: Tue, 22 Sep 2015 22:09:07 +0200 Subject: [PATCH] When sessions connect/disconnect a status message will now be displayed clients command was renamed to sessions added an option to kill a session through the sessions command --- pupy/pupylib/PupyCmd.py | 33 ++++++++++++++++++++++++++------- pupy/pupylib/PupyServer.py | 13 +++++++++++-- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/pupy/pupylib/PupyCmd.py b/pupy/pupylib/PupyCmd.py index 1f4a73a0..57701f3a 100644 --- a/pupy/pupylib/PupyCmd.py +++ b/pupy/pupylib/PupyCmd.py @@ -357,11 +357,30 @@ class PupyCmd(cmd.Cmd): """ List available modules with a brief description """ for m,d in self.pupsrv.list_modules(): self.stdout.write("{:<20} {}\n".format(m, color(d,'grey'))) - - def do_clients(self, arg): - """ List connected clients """ - client_list=self.pupsrv.get_clients_list() - self.display(PupyCmd.table_format([x.desc for x in client_list], wl=["id", "user", "hostname", "platform", "release", "os_arch", "address"])) + + def do_sessions(self, arg): + """ list/interact with established sessions """ + arg_parser = PupyArgumentParser(prog='sessions', description='List/interact with established sessions') + arg_parser.add_argument('-k', dest='kill', metavar='', type=int, help='Kill the selected session') + #arg_parser.add_argument('-i', dest='interact', metavar='', help='Interact with the supplied session ID') + arg_parser.add_argument('-l', dest='list', action='store_true', help='List all active sessions') + + try: + modargs=arg_parser.parse_args(shlex.split(arg)) + except PupyModuleExit: + return + + if modargs.list or not arg: + client_list=self.pupsrv.get_clients_list() + self.display(PupyCmd.table_format([x.desc for x in client_list], wl=["id", "user", "hostname", "platform", "release", "os_arch", "address"])) + + elif modargs.kill: + selected_client = self.pupsrv.get_clients(modargs.kill) + if selected_client: + try: + selected_client[0].conn.exit() + except Exception: + pass def do_jobs(self, arg): """ manage jobs """ @@ -408,7 +427,7 @@ class PupyCmd(cmd.Cmd): self.display_error(traceback.format_exc()) def do_python(self,arg): - """ start interacting with the server local python interpreter (for debugging purposes). Auto-completion available. """ + """ start the local python interpreter (for debugging purposes) """ orig_exit=builtins.exit orig_quit=builtins.quit def disabled_exit(*args, **kwargs): @@ -504,7 +523,7 @@ class PupyCmd(cmd.Cmd): error=pj.interactive_wait() if error and not modjobs: pj.stop() - + except KeyboardInterrupt: self.display_warning("interrupting job ... (please wait)") pj.interrupt() diff --git a/pupy/pupylib/PupyServer.py b/pupy/pupylib/PupyServer.py index 66c8431f..cc076f62 100644 --- a/pupy/pupylib/PupyServer.py +++ b/pupy/pupylib/PupyServer.py @@ -24,6 +24,8 @@ import modules import logging from .PupyErrors import PupyModuleExit, PupyModuleError from .PupyJob import PupyJob +from .PupyCmd import color_real + try: import ConfigParser as configparser except ImportError: @@ -127,12 +129,20 @@ class PupyServer(threading.Thread): "pid" : l[7], "address" : conn._conn._config['connid'].split(':')[0], }, self)) - self.current_id+=1 + + addr = conn.modules['pupy'].get_connect_back_host() + server_ip, server_port = addr.rsplit(':', 1) + client_ip, client_port = conn._conn._config['connid'].split(':') + + print color_real('[*]', 'blue'), "Session {} opened ({}:{} -> {}:{})".format(self.current_id, server_ip, server_port, client_ip, client_port) + + self.current_id += 1 def remove_client(self, client): with self.clients_lock: for i,c in enumerate(self.clients): if c.conn is client: + print color_real('[*]', 'blue'), 'Session {} closed'.format(self.clients[i].desc['id']) del self.clients[i] break @@ -219,7 +229,6 @@ class PupyServer(threading.Thread): self.jobs[self.jobs_id]=job self.jobs_id+=1 - def get_job(self, job_id): try: job_id=int(job_id)