]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
netcmd: add shell command
authorRob van der Linde <rob@catalyst.net.nz>
Thu, 2 Nov 2023 07:28:34 +0000 (20:28 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 15 Dec 2023 03:51:55 +0000 (03:51 +0000)
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 <host> --workgroup <workgroup> --realm <realm>

>>> silos = AuthenticationSilo.query(ldb)
>>> for silo in silos:
...     print(silo)
...

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Fri Dec 15 03:51:55 UTC 2023 on atb-devel-224

docs-xml/manpages/samba-tool.8.xml
python/samba/netcmd/main.py
python/samba/netcmd/shell.py [new file with mode: 0644]

index c05be31b3d398884ad1c6a606b3ccf4e321a0a0f..6b3a73020e33f54f7bd691636caceafc636979f7 100644 (file)
        <para>Display an objectclass schema definition.</para>
 </refsect3>
 
+<refsect2>
+       <title>shell</title>
+       <para>Opens an interactive Samba Python shell.</para>
+</refsect2>
+
+<refsect3>
+       <title>shell [options]</title>
+       <para>Opens an interactive Python shell for Samba ldb connection.</para>
+       <variablelist>
+               <varlistentry>
+                       <term>-H, --URL</term>
+                       <listitem><para>
+                               LDB URL for database or target server.
+                       </para></listitem>
+               </varlistentry>
+       </variablelist>
+</refsect3>
+
 <refsect2>
        <title>sites</title>
        <para>Manage sites.</para>
index 0fd4f20d9412650226a3f02f483c1058a1ea9f5f..f1a0afbecf6db56806ad83c1798ff19aaa8ec171 100644 (file)
@@ -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 (file)
index 0000000..31619eb
--- /dev/null
@@ -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 <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)