1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | # provide information on connected users and open files
|
---|
5 | # Copyright Ç 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 | sys.path.insert(0, "bin/python")
|
---|
15 |
|
---|
16 | import optparse
|
---|
17 | import samba.getopt as options
|
---|
18 | from samba import irpc, messaging
|
---|
19 |
|
---|
20 | def show_sessions(conn):
|
---|
21 | """show open sessions"""
|
---|
22 |
|
---|
23 | sessions = conn.smbsrv_information(irpc.SMBSRV_INFO_SESSIONS).next()
|
---|
24 | print "User Client Connected at"
|
---|
25 | print "-" * 79
|
---|
26 | for session in sessions:
|
---|
27 | fulluser = "%s/%s" % (session.account_name, session.domain_name)
|
---|
28 | print "%-30s %16s %s" % (fulluser, session.client_ip, sys.httptime(session.connect_time))
|
---|
29 | print ""
|
---|
30 |
|
---|
31 | def show_tcons(open_connection):
|
---|
32 | """show open tree connects"""
|
---|
33 | conn = open_connection("smb_server")
|
---|
34 | tcons = conn.smbsrv_information(irpc.SMBSRV_INFO_TCONS).next()
|
---|
35 | print "Share Client Connected at"
|
---|
36 | print "-" * 79
|
---|
37 | for tcon in tcons:
|
---|
38 | print "%-30s %16s %s" % (tcon.share_name, tcon.client_ip, sys.httptime(tcon.connect_time))
|
---|
39 |
|
---|
40 |
|
---|
41 | def show_nbt(open_connection):
|
---|
42 | """show nbtd information"""
|
---|
43 | conn = open_connection("nbt_server")
|
---|
44 | stats = conn.nbtd_information(irpc.NBTD_INFO_STATISTICS).next()
|
---|
45 | print "NBT server statistics:"
|
---|
46 | fields = [("total_received", "Total received"),
|
---|
47 | ("total_sent", "Total sent"),
|
---|
48 | ("query_count", "Query count"),
|
---|
49 | ("register_count", "Register count"),
|
---|
50 | ("release_count", "Release count")]
|
---|
51 | for (field, description) in fields:
|
---|
52 | print "\t%s:\t%s" % (description, getattr(stats, field))
|
---|
53 | print
|
---|
54 |
|
---|
55 | parser = optparse.OptionParser("%s [options]" % sys.argv[0])
|
---|
56 | sambaopts = options.SambaOptions(parser)
|
---|
57 | parser.add_option_group(sambaopts)
|
---|
58 | parser.add_option("--messaging-path", type="string", metavar="PATH",
|
---|
59 | help="messaging path")
|
---|
60 | parser.add_option("--nbt", help="show NetBIOS status", action="store_true")
|
---|
61 |
|
---|
62 | opts, args = parser.parse_args()
|
---|
63 |
|
---|
64 | lp = sambaopts.get_loadparm()
|
---|
65 |
|
---|
66 | print "%s" % lp.get("server string")
|
---|
67 |
|
---|
68 | messaging_path = (opts.messaging_path or os.path.join(lp.get("private dir"), "smbd.tmp", "messaging"))
|
---|
69 |
|
---|
70 | def open_connection(name):
|
---|
71 | return messaging.ClientConnection(name, messaging_path=messaging_path)
|
---|
72 |
|
---|
73 | if opts.nbt:
|
---|
74 | show_nbt(open_connection)
|
---|
75 | else:
|
---|
76 | try:
|
---|
77 | conn = open_connection("smb_server")
|
---|
78 | except RuntimeError, (num, msg):
|
---|
79 | if msg == 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
|
---|
80 | print "No active connections"
|
---|
81 | else:
|
---|
82 | show_sessions(conn)
|
---|
83 | show_tcons(conn)
|
---|