summaryrefslogtreecommitdiff
path: root/source4/scripting/bin/smbstatus
blob: cc06c4c4290e6849dda737969c8b04f00c904ab4 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  provide information on connected users and open files
#  Copyright (c) Jelmer Vernooij 2008
#
#  Based on the original in EJS:
#  Copyright Andrew Tridgell 2005
#  Released under the GNU GPL version 3 or later
#
from __future__ import print_function
import os, sys

# make sure the script dies immediately when hitting control-C,
# rather than raising KeyboardInterrupt. As we do all database
# operations using transactions, this is safe.
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

sys.path.insert(0, "bin/python")

import optparse
import samba.getopt as options
from samba import irpc, messaging

def show_sessions(conn):
    """show open sessions"""

    sessions = next(conn.smbsrv_information(irpc.SMBSRV_INFO_SESSIONS))
    print("User                                  Client      Connected at")
    print("-" * 79)
    for session in sessions:
        fulluser = "%s/%s" % (session.account_name, session.domain_name)
        print("%-30s %16s   %s" % (fulluser,
                                   session.client_ip,
                                   sys.httptime(session.connect_time)))
    print()

def show_tcons(open_connection):
    """show open tree connects"""
    conn = open_connection("smb_server")
    tcons = next(conn.smbsrv_information(irpc.SMBSRV_INFO_TCONS))
    print("Share                                 Client      Connected at")
    print("-" * 79)
    for tcon in tcons:
        print("%-30s %16s   %s" %
              (tcon.share_name, tcon.client_ip, sys.httptime(tcon.connect_time)))


def show_nbt(open_connection):
    """show nbtd information"""
    conn = open_connection("nbt_server")
    stats = next(conn.nbtd_information(irpc.NBTD_INFO_STATISTICS))
    print("NBT server statistics:")
    fields = [("total_received", "Total received"),
              ("total_sent", "Total sent"),
              ("query_count", "Query count"),
              ("register_count", "Register count"),
              ("release_count", "Release count")]
    for (field, description) in fields:
        print("\t%s:\t%s" % (description, getattr(stats, field)))
    print()

parser = optparse.OptionParser("%s [options]" % sys.argv[0])
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option("--messaging-path", type="string", metavar="PATH",
                  help="messaging path")
parser.add_option("--nbt", help="show NetBIOS status", action="store_true")

opts, args = parser.parse_args()

lp = sambaopts.get_loadparm()

print("%s" % lp.get("server string"))

messaging_path = (opts.messaging_path or os.path.join(lp.get("private dir"), "smbd.tmp", "messaging"))

def open_connection(name):
    return messaging.ClientConnection(name, messaging_path=messaging_path)

if opts.nbt:
    show_nbt(open_connection)
else:
    try:
        conn = open_connection("smb_server")
    except RuntimeError as e:
        if e.args[1] == 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
            print("No active connections")
    else:
        show_sessions(conn)
        show_tcons(conn)