'list/interact with established sessions'

def do_sessions(self, arg):

    arg_parser = PupyArgumentParser(prog='sessions', description=self.do_sessions.__doc__)
    arg_parser.add_argument('-i', '--interact', metavar='<filter>', help='change    the    default    --filter    value    for    other    commands')
    arg_parser.add_argument('-g', '--global-reset', action='store_true', help='reset    --interact    to    the    default    global    behavior')
    arg_parser.add_argument('-l', dest='list', action='store_true', help='List    all    active    sessions')
    arg_parser.add_argument('-k', dest='kill', metavar='<id>', type=int, help='Kill    the    selected    session')
    arg_parser.add_argument('-K', dest='killall', action='store_true', help='Kill    all    sessions')
    arg_parser.add_argument('-d', dest='drop', metavar='<id>', type=int, help='Drop    the    connection    (abruptly    close    the    socket)')
    arg_parser.add_argument('-D', dest='dropall', action='store_true', help='Drop    all    connections')
    try:
        modargs = arg_parser.parse_args(shlex.split(arg))
    except PupyModuleExit:
        return
    if modargs.global_reset:
        self.default_filter = None
        self.display_success('default    filter    reset    to    global    !')
    elif modargs.interact:
        self.default_filter = modargs.interact
        self.display_success(('default    filter    set    to    %s' % self.default_filter))
    elif modargs.kill:
        selected_client = self.pupsrv.get_clients(modargs.kill)
        if selected_client:
            try:
                selected_client[0].conn.exit()
            except Exception:
                pass
    elif modargs.drop:
        selected_client = self.pupsrv.get_clients(modargs.drop)
        if selected_client:
            try:
                selected_client[0].conn._conn.close()
            except Exception:
                pass
    elif modargs.dropall:
        clients = list(self.pupsrv.get_clients_list())
        for client in clients:
            try:
                client.conn._conn.close()
            except Exception:
                pass
    elif (modargs.list or (not arg)):
        client_list = self.pupsrv.get_clients_list()
        if self.default_filter:
            filtered_clients = self.pupsrv.get_clients(self.default_filter)
        else:
            filtered_clients = client_list
        columns = ['id', 'user', 'hostname', 'platform', 'release', 'os_arch', 'proc_arch', 'intgty_lvl', 'address', 'tags']
        content = []
        for client in client_list:
            color = ('white' if (client in filtered_clients) else 'darkgrey')
            data = {k: colorize(v, color) for (k, v) in client.desc.iteritems() if (k in columns)}
            data.update({'tags': colorize(self.config.tags(client.node()), color)})
            content.append(data)
        self.display(PupyCmd.table_format(content, wl=columns))
    elif modargs.killall:
        client_list = self.pupsrv.get_clients_list()
        objectClient = [x.desc for x in client_list]
        for client in objectClient:
            try:
                self.pupsrv.get_clients(client['id'])[0].conn.exit()
            except Exception:
                pass
