2 # -*- coding: utf-8 -*-
4 # provide information on connected users and open files
5 # Copyright (c) Jelmer Vernooij 2008
7 # Based on the original in EJS:
8 # Copyright Andrew Tridgell 2005
9 # Released under the GNU GPL version 3 or later
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.
18 signal
.signal(signal
.SIGINT
, signal
.SIG_DFL
)
20 sys
.path
.insert(0, "bin/python")
23 import samba
.getopt
as options
24 from samba
import irpc
, messaging
26 def show_sessions(conn
):
27 """show open sessions"""
29 sessions
= conn
.smbsrv_information(irpc
.SMBSRV_INFO_SESSIONS
).next()
30 print "User Client Connected at"
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
))
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"
44 print "%-30s %16s %s" % (tcon
.share_name
, tcon
.client_ip
, sys
.httptime(tcon
.connect_time
))
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
))
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")
68 opts
, args
= parser
.parse_args()
70 lp
= sambaopts
.get_loadparm()
72 print "%s" % lp
.get("server string")
74 messaging_path
= (opts
.messaging_path
or os
.path
.join(lp
.get("private dir"), "smbd.tmp", "messaging"))
76 def open_connection(name
):
77 return messaging
.ClientConnection(name
, messaging_path
=messaging_path
)
80 show_nbt(open_connection
)
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"