From 83e36d97c956c2dba1caef45e0205cb5b1b164f3 Mon Sep 17 00:00:00 2001 From: Rob van der Linde Date: Thu, 2 Nov 2023 20:28:34 +1300 Subject: [PATCH] netcmd: add shell command A simple samba-tool shell, can be quite useful to play around with the ldb database and models. All models get imported and the samdb connection variable made available. Example usage: bin/samba-tool shell -H --workgroup --realm >>> silos = AuthenticationSilo.query(ldb) >>> for silo in silos: ... print(silo) ... Signed-off-by: Rob van der Linde Reviewed-by: Joseph Sutton Reviewed-by: Andrew Bartlett Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Fri Dec 15 03:51:55 UTC 2023 on atb-devel-224 --- docs-xml/manpages/samba-tool.8.xml | 18 ++++++++ python/samba/netcmd/main.py | 1 + python/samba/netcmd/shell.py | 74 ++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 python/samba/netcmd/shell.py diff --git a/docs-xml/manpages/samba-tool.8.xml b/docs-xml/manpages/samba-tool.8.xml index c05be31b3d3..6b3a73020e3 100644 --- a/docs-xml/manpages/samba-tool.8.xml +++ b/docs-xml/manpages/samba-tool.8.xml @@ -2406,6 +2406,24 @@ Display an objectclass schema definition. + + shell + Opens an interactive Samba Python shell. + + + + shell [options] + Opens an interactive Python shell for Samba ldb connection. + + + -H, --URL + + LDB URL for database or target server. + + + + + sites Manage sites. diff --git a/python/samba/netcmd/main.py b/python/samba/netcmd/main.py index 0fd4f20d941..f1a0afbecf6 100644 --- a/python/samba/netcmd/main.py +++ b/python/samba/netcmd/main.py @@ -73,6 +73,7 @@ class cmd_sambatool(SuperCommand): subcommands["ntacl"] = None subcommands["rodc"] = None subcommands["schema"] = None + subcommands["shell"] = None subcommands["sites"] = None subcommands["spn"] = None subcommands["testparm"] = None diff --git a/python/samba/netcmd/shell.py b/python/samba/netcmd/shell.py new file mode 100644 index 00000000000..31619eb1354 --- /dev/null +++ b/python/samba/netcmd/shell.py @@ -0,0 +1,74 @@ +# Unix SMB/CIFS implementation. +# +# Interactive Python shell for SAMBA +# +# Copyright (C) Catalyst.Net Ltd. 2023 +# +# Written by Rob van der Linde +# +# 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 . +# + +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) -- 2.47.2