summaryrefslogtreecommitdiffstats
path: root/python/samba/netcmd/shell.py
blob: 31619eb135462f57ecf847f4345afb9a67fa409d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Unix SMB/CIFS implementation.
#
# Interactive Python shell for SAMBA
#
# Copyright (C) Catalyst.Net Ltd. 2023
#
# Written by Rob van der Linde <rob@catalyst.net.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import code
import readline
import rlcompleter

import ldb

import samba.getopt as options
from samba import version
from samba.netcmd import Command
from samba.netcmd.domain.models import MODELS


class cmd_shell(Command):
    """Open a SAMBA Python shell."""

    synopsis = "%prog -H [options]"

    takes_optiongroups = {
        "sambaopts": options.SambaOptions,
        "credopts": options.CredentialsOptions,
        "hostopts": options.HostOptions,
    }

    def run(self, sambaopts=None, credopts=None, hostopts=None):
        samdb = self.ldb_connect(hostopts, sambaopts, credopts)

        context = globals()
        context.update({
            "samdb": samdb,
            "ldb": ldb,
        })
        context.update(MODELS)

        banner = rf"""
   _____         __  __ ____
  / ____|  /\   |  \/  |  _ \   /\
 | (___   /  \  | \  / | |_) | /  \
  \___ \ / /\ \ | |\/| |  _ < / /\ \
  ____) / ____ \| |  | | |_) / ____ \
 |_____/_/    \_\_|  |_|____/_/    \_\
                                       v{version}

Variables:

samdb = {samdb}
"""
        for name, model in MODELS.items():
            banner += f"{name} = {model}\n"

        readline.parse_and_bind("tab: complete")
        readline.set_completer(rlcompleter.Completer(context).complete)
        code.InteractiveConsole(locals=context).interact(banner=banner)