[cvsnt] trying to shadow website on network drive

Misner, Ris rmisner at accelent.com
Wed Aug 13 19:58:17 BST 2003


> Ris,
> 
> 	I am having some dificulties to do it with NET USE.
> 
> 	Could you send me the options you use with NET USE?
> 
> Thanks.

I had to do a lot of experimentation with this to figure out how to get it to work correctly, so I'm happy to share my findings if it will save anyone the 3 days it would have saved me...  I figured I'd copy this to the list so everyone can see this, or look it up in the archives later in case anyone else has the same question...

-------------

First of all, it took a while to figure out the best way to get help on all the options for NET USE.  The command to get extended help is "NET HELP USE" which will give you lots of details on the available options, much better than "NET USE /?" which tells you next to nothing.

-------------

To access the shared directory, I use this:

NET USE * "\\ComputerName\Directory" password /USER:administrator at domain /PERSISTENT:NO

I'll break it down one piece at a time.

The * following NET USE indicates to get the next available drive letter.  I found that it was necessary to use * instead of an explicit drive letter (like W:) because I was often given errors about "duplicate logins to the same resource are not allowed" when asking for a specific drive letter, but I could get duplicate access to the same network share if I used * instead of W:

So my script parses the output of NET USE to determine what drive letter it gave me.  The first line of the output should say "Drive X: assigned..." or something like that, so you can just extract the 6th to 8th character of the output to get the drive letter.  (In python this is easy: output[6:8] is "X:")

Next is the UNC path to the share.  You have to be sure to omit any trailing slashes, or else you will get an error to the effect that NET USE "cannot find computer name" which is deceptive because it really means "don't use a trailing backslash".  I guess if you use a trailing backslash, NET USE assumes that the entire string is a computer name, or perhaps it misinterprets the \" as an escaped quotation mark and tries to find a directory witha " in it.  Either way it doesn't work, so leave out the trailing \.

Next, in an unusual reversal of standard parameter orders, you need to give a password and then a matching user name.  I found that the command would fail unless I explicitly specified the domain name as in "administrator at mycompany.com" instead of just "administrator" though that may be more related to the network share permission setup than to the use of NET USE - I'm not sure.  Be sure that the username and password you provide have permission to access the network share.

The last option, /PERSISTENT:NO, is optional.  I use it because I wanted to watch out for any errors.  I figured if my script failed so badly that I accidentally reserved every drive letter mapped to the same shared resource, at least a reboot of the machine would fix it.  If you do /PERSISTENT:YES (or leave it blank) then the drives are restored later.  I'm not sure if the explicit NET USE X: /DELETE I do after this makes the /PERSISTENT:NO unnecessary, but I do the /PERSISTENT:NO just to be safe.

-------------

When I'm done using the shared directory, I call NET USE with new options to delete the mapping so I don't fill up all the drive letters after only 26 times of my script running.  Those options are:

NET USE X: /DELETE

Where X: is the drive letter specified in the output that I parsed from the original NET USE call.

Note that if your script is running in X: as its working directory, then this call will fail.  Actually, it will prompt you asking "there are open file handles on this share, do you want to delete the drive mapping anyway? [y/n]" but since you're running it through a script you can't answer the prompt, and it ends up assuming NO so you consume all your drive letters pretty fast.  So I always return to the C: drive before trying to delete the network share mapping.

-------------

I don't mind sharing, so following are the exact python functions I use to acquire and release access to the network share.  You may notice that there are some undefined functions called from this code, but it should be fairly obvious what they do.  Enjoy and good luck!
-Ris

-------- BEGIN PYTHON CODE ----------

UNCPATHSTART = os.sep * 2

# shared directory access login info
SHAREUSER = "yourusername"
SHAREPASS = "yourpassword"

# This is a global list of all drives mapped through MapNetAccess
MAPPED_DRIVES_LIST = []

def MapNetAccess(dir):
	""" MapNetAccess(dir):
	executes NET USE to map a drive letter to the UNC path indicated by 'dir'
	returns the drive letter string in the form "X:" or None if there were errors
	Note that the next available drive letter will be mapped
	"""
	assert dir.startswith(UNCPATHSTART)
	# "net use" will fail if the path ends in a trailing slash, so remove it if applicable
	if dir.endswith(os.sep):
		dir = dir[:-1]
	global SHAREPASS
	global SHAREUSER
	sCommand = "NET USE * \"%s\" %s /USER:%s /PERSISTENT:NO" % (dir, SHAREPASS, SHAREUSER)
	(errorcode, outputlines) = DoSysCall(sCommand)
	if errorcode:
		return None
		
	# the format of the output if successful should be something like this:
	#   Drive Y: is now connected to \\computername\directory.
	#   The command completed successfully.
	for line in outputlines:
		if line.startswith("Drive "):
			newpath = line[6:8]
			Print("Network share mapped to %s" % newpath)
			global MAPPED_DRIVES_LIST
			MAPPED_DRIVES_LIST.append(newpath)
			return newpath
		
	# if we got this far, then we failed to map the drive but didn't get an error code result
	Print("ERROR: failed to access network share \"%s\" because NET USE returned unexpected data:" % (dir), 1)
	Print("".join(outputlines), 1)
	return None
	

def ReleaseNetAccess(drive):
	"""ReleaseNetAccess(drive):
	releases a drive letter mapping to a network share following acquisition
	of that drive letter using MapNetAccess()

	drive should be a string in the form "X:"
	"""
	sCommand = "NET USE %s /DELETE" % drive
	(errorcode, outputlines) = DoSysCall(sCommand)
	if errorcode:
		Print("WARNING: failed to release network share \"%s\" because of the following errors:" % (drive), 1)
		Print("".join(outputlines), 1)
		# allow execution to continue
	# remove the drive from the global MAPPED_DRIVES_LIST
	global MAPPED_DRIVES_LIST
	MAPPED_DRIVES_LIST.remove(drive)
	

def ReleaseAllNetAccess():
	global MAPPED_DRIVES_LIST
	for drive in MAPPED_DRIVES_LIST:
		ReleaseNetAccess(drive)

-------- END PYTHON CODE ----------

> -----Original Message-----
> From: Fabiano Marinho Carneiro da Cunha 
> [mailto:fmccunha at choice.com.br]
> Sent: Wednesday, August 13, 2003 2:29 PM
> To: Misner, Ris
> Subject: RE: [cvsnt] trying to shadow website on network drive
> 
> 
> Ris,
> 
> 	I am having some dificulties to do it with NET USE.
> 
> 	Could you send me the options you use with NET USE?
> 
> Thanks.
> 
> 
> -----Original Message-----
> From: Misner, Ris [mailto:rmisner at accelent.com] 
> Sent: quarta-feira, 13 de agosto de 2003 14:11
> To: cvsnt at cvsnt.org
> Subject: RE: [cvsnt] trying to shadow website on network drive
> 
> 
> > Misner, Ris wrote:
> > 
> > > One of the projects in this repository is the company
> > website, so the idea is that developers will commit a change
> > to the website in CVS, and then this postcommit script will 
> > automatically update the website directory on the server so 
> > that the change appears on the internet immediately after 
> > that change is committed to CVS.  This would be very 
> > convenient integration if I could get it working.
> > > 
> > 
> > You are hardly the only person who thought this would be an
> > excellent use of 
> > CVS; you are also not the only person to discover it is very 
> > difficult to manage 
> > with NT!
> > 
> > My best suggestion is to make the CVSNT and web server the
> > same box.  Then you 
> > can use the script and instructions here:
> > 
> > 	http://www.cvsnt.org/wiki/CvsChapter180
> > 
> > to keep the web server updated.  This will work and will not
> > require as much 
> > hairloss as the other method below.
> > 
> 
> In case anyone is interested or following this topic, I was 
> able to execute "NET USE" with appropriate options to acquire 
> network shared drive access from my script, thus solving my problem.
> 
> Thanks to everyone who contributed with suggestions and info. 
>  I appreciate all of your help very much!!
> 
> -Ris
> _______________________________________________
> cvsnt mailing list
> cvsnt at cvsnt.org
> http://www.cvsnt.org/cgi-bin/mailman/listinfo/cvsnt
> 
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.509 / Virus Database: 306 - Release Date: 12/08/2003
>  
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.509 / Virus Database: 306 - Release Date: 12/08/2003
>  
> 


More information about the cvsnt mailing list