1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | # provide information on connected users and open files
|
---|
5 | # Copyright (c) Jelmer Vernooij 2008
|
---|
6 | #
|
---|
7 | # Based on the original in EJS:
|
---|
8 | # Copyright Andrew Tridgell 2005
|
---|
9 | # Released under the GNU GPL version 3 or later
|
---|
10 | #
|
---|
11 |
|
---|
12 | import os, sys
|
---|
13 |
|
---|
14 | # make sure the script dies immediately when hitting control-C,
|
---|
15 | # rather than raising KeyboardInterrupt. As we do all database
|
---|
16 | # operations using transactions, this is safe.
|
---|
17 | import signal
|
---|
18 | signal.signal(signal.SIGINT, signal.SIG_DFL)
|
---|
19 |
|
---|
20 | sys.path.insert(0, "bin/python")
|
---|
21 |
|
---|
22 | import optparse
|
---|
23 | import samba.getopt as options
|
---|
24 | from samba import irpc, messaging
|
---|
25 |
|
---|
26 | def show_sessions(conn):
|
---|
27 | """show open sessions"""
|
---|
28 |
|
---|
29 | sessions = conn.smbsrv_information(irpc.SMBSRV_INFO_SESSIONS).next()
|
---|
30 | print "User Client Connected at"
|
---|
31 | print "-" * 79
|
---|
32 | for session in sessions:
|
---|
33 | fulluser = "%s/%s" % (session.account_name, session.domain_name)
|
---|
34 | print "%-30s %16s %s" % (fulluser, session.client_ip, sys.httptime(session.connect_time))
|
---|
35 | print ""
|
---|
36 |
|
---|
37 | def show_tcons(open_connection):
|
---|
38 | """show open tree connects"""
|
---|
39 | conn = open_connection("smb_server")
|
---|
40 | tcons = conn.smbsrv_information(irpc.SMBSRV_INFO_TCONS).next()
|
---|
41 | print "Share Client Connected at"
|
---|
42 | print "-" * 79
|
---|
43 | for tcon in tcons:
|
---|
44 | print "%-30s %16s %s" % (tcon.share_name, tcon.client_ip, sys.httptime(tcon.connect_time))
|
---|
45 |
|
---|
46 |
|
---|
47 | def show_nbt(open_connection):
|
---|
48 | """show nbtd information"""
|
---|
49 | conn = open_connection("nbt_server")
|
---|
50 | stats = conn.nbtd_information(irpc.NBTD_INFO_STATISTICS).next()
|
---|
51 | print "NBT server statistics:"
|
---|
52 | fields = [("total_received", "Total received"),
|
---|
53 | ("total_sent", "Total sent"),
|
---|
54 | ("query_count", "Query count"),
|
---|
55 | ("register_count", "Register count"),
|
---|
56 | ("release_count", "Release count")]
|
---|
57 | for (field, description) in fields:
|
---|
58 | print "\t%s:\t%s" % (description, getattr(stats, field))
|
---|
59 | print
|
---|
60 |
|
---|
61 | parser = optparse.OptionParser("%s [options]" % sys.argv[0])
|
---|
62 | sambaopts = options.SambaOptions(parser)
|
---|
63 | parser.add_option_group(sambaopts)
|
---|
64 | parser.add_option("--messaging-path", type="string", metavar="PATH",
|
---|
65 | help="messaging path")
|
---|
66 | parser.add_option("--nbt", help="show NetBIOS status", action="store_true")
|
---|
67 |
|
---|
68 | opts, args = parser.parse_args()
|
---|
69 |
|
---|
70 | lp = sambaopts.get_loadparm()
|
---|
71 |
|
---|
72 | print "%s" % lp.get("server string")
|
---|
73 |
|
---|
74 | messaging_path = (opts.messaging_path or os.path.join(lp.get("private dir"), "smbd.tmp", "messaging"))
|
---|
75 |
|
---|
76 | def open_connection(name):
|
---|
77 | return messaging.ClientConnection(name, messaging_path=messaging_path)
|
---|
78 |
|
---|
79 | if opts.nbt:
|
---|
80 | show_nbt(open_connection)
|
---|
81 | else:
|
---|
82 | try:
|
---|
83 | conn = open_connection("smb_server")
|
---|
84 | except RuntimeError, (num, msg):
|
---|
85 | if msg == 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
|
---|
86 | print "No active connections"
|
---|
87 | else:
|
---|
88 | show_sessions(conn)
|
---|
89 | show_tcons(conn)
|
---|