Reads a file containing a list of share names, and prints a report of the current free space on the volumes.
See the end of the source for an example of the required DRIVESPACE.CFG file.
| Python |
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 | # drivespace.py - M.Keranen (mksql@yahoo.com) [10/05/2004]
# -------------------------------------------------------------
# Reads a file containing a list of share names,
# and prints a report of the current free space on the volumes.
# -------------------------------------------------------------
import getpass,os,string, sys
import win32file, win32net, win32wnet, win32netcon
cfgfile=sys.path[0]+'/drivespace2.cfg'
cfglines = open(cfgfile,'r').readlines()
cfglines.sort()
# Compile a list of domains in the config file
domains = []
for line in cfglines:
target = string.split(string.strip(line),'\\')
if len(target)>2 and target[0] not in domains: domains.append(target[0])
# Create a dict of domain\user_id:password
domid ={}
#domid['(local)']=getpass.getuser(),getpass.getpass()
for domain in domains:
string.strip(domain)
uid = string.strip(raw_input('UserID for %s: ' % domain))
if uid != '':
pw = string.strip(getpass.getpass())
domid[domain] = domain+'\\'+uid,pw
else: domid[domain] = '',''
print
lastserv = ""
xit = " "
while xit == " ":
print "\n%s \t %s \t %s\t%s" % ("Server\share","%Free","Avail MB","Total MB")
print "%s \t %s \t %s\t%s" % ("------------","-----","--------","--------")
for line in cfglines:
if line[0]<>'#':
line = string.strip(line)
target = string.split(line,'\\')
if len(target)==2:
server = '\\\\'+target[0]
share = '\\\\'+line
uid = getpass.getuser()
pw = None
elif len(target)==3:
domain = target[0]
server = '\\\\'+target[1]
share = '\\\\'+target[1]+'\\'+target[2]
uid = domid[domain][0]
pw = domid[domain][1]
if server != lastserv and lastserv != "":
try: win32wnet.WNetCancelConnection2(lastserv, 0, 0)
except: warn = "!"
if server != lastserv and uid != '':
try: win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, None, server, None, uid, pw, 0)
except: print "%s\t (WNetAddConnection2 failed [%s])" % (server,uid)
lastserv = server
fs,ts,fp = 0,0,0
if uid != '':
try: space = win32file.GetDiskFreeSpaceEx(share)
except:
warn = " "
fp = "(GetDiskFreeSpaceEx failed)"
fs,ts = '',''
else:
fs = int(space[0]/1048576)
ts = int(space[1]/1048576)
fp = int((float(space[0])/float(space[1]))*100)
if fp<16: warn = "!"
else: warn = " "
print "%s \t %s%s \t %s\t\t%s" % (share[2:],warn,fp,fs,ts)
try: win32wnet.WNetCancelConnection2(lastserv, 0, 0)
except: warn = "!"
xit = raw_input('\nPress Enter to exit (Space, Enter to rerun)...')
"""
Example of drivespace.cfg file:
-------------------------------
local1\c$
domain2\remote2\c$
"""
|
Discussion
- 2nd Update, October 2004, to simplify the config file, now containing only the share path, with domain name where needed. Supports multiple NT domains, and will prompt for ID/password for each. Also added cleanup code to cancel network connections established.


Comments
Minor Update. Formatting of report modified to keep columns in line when the server name is less that 5 characters.
Sign in to comment