Re: Win32 drive mapping... aka "net use"


L

Lucas Machado

Alex said:
import win32net
win32net.NetUseAdd(None,1,{'remote':r'\\server\share','local':'K:'})

is an example (not all that easy to fathom from the docs, but I
found it out with a little help from the docs, a little from MSDN,
and a little experimentation).

I looked through the MSDN and was not able to find much information on
how to properly use the NetUseAdd function. I searched for the
function and looked through the results but all it showed was some data
structure, but i was actually looking for a list of all possible
arguments and which arguments were/were not required.

my problem with the above NetUseAdd example is that I would rather not
have to specify a device. In the script I am writing the user may
choose to map multiple shares so I need to be able to map to the next
available device:

net use * \\some_server\share_name

instead of:

net use k: \\server\share

Thanks for the help in advance. Also, if anyone could provide a link
to good windows api docs for python that would be great.

Cheers,
--Lucas Machado
 
Ad

Advertisements

M

mensanator

Lucas said:
I looked through the MSDN and was not able to find much information on
how to properly use the NetUseAdd function.

C:\>net help use
I searched for the
function and looked through the results but all it showed was some data
structure, but i was actually looking for a list of all possible
arguments and which arguments were/were not required.

The syntax of this command is:

NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[[email protected] domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]


NET USE connects a computer to a shared resource or disconnects a
computer from a shared resource. When used without options, it lists
the computer's connections.

devicename Assigns a name to connect to the resource or specifies
the device to be disconnected. There are two kinds of
devicenames: disk drives (D: through Z:) and printers
(LPT1: through LPT3:). Type an asterisk instead of a
specific devicename to assign the next available
devicename.
\\computername Is the name of the computer controlling the shared
resource. If the computername contains blank
characters,
enclose the double backslash (\\) and the computername
in quotation marks (" "). The computername may be from
1 to 15 characters long.
\sharename Is the network name of the shared resource.
\volume Specifies a NetWare volume on the server. You must
have
Client Services for Netware (Windows Workstations)
or Gateway Service for Netware (Windows Server)
installed and running to connect to NetWare servers.
password Is the password needed to access the shared resource.
* Produces a prompt for the password. The password is
not displayed when you type it at the password prompt.
/USER Specifies a different username with which the
connection
is made.
domainname Specifies another domain. If domain is omitted,
the current logged on domain is used.
username Specifies the username with which to logon.
/SMARTCARD Specifies that the connection is to use credentials on
a smart card.
/SAVECRED Specifies that the username and password are to be
saved.
This switch is ignored unless the command prompts for
username
and password. This option is not available on Windows
XP
Home Edition and will be ignored.
/HOME Connects a user to their home directory.
/DELETE Cancels a network connection and removes the
connection
from the list of persistent connections.
/PERSISTENT Controls the use of persistent network connections.
The default is the setting used last.
YES Saves connections as they are made, and restores
them at next logon.
NO Does not save the connection being made or subsequent
connections; existing connections will be restored at
next logon. Use the /DELETE switch to remove
persistent connections.
NET HELP command | MORE displays Help one screen at a time.
my problem with the above NetUseAdd example is that I would rather not
have to specify a device. In the script I am writing the user may
choose to map multiple shares so I need to be able to map to the next
available device:

net use * \\some_server\share_name

net use * \\some_server\share_name
 
L

Lucas Machado

I have already seen the "net help use" and i know how to manage samba
shares from a command prompt. What i need help with is using the win32
api for python to manage shares....

--Lucas
 
R

Roger Upole

You could use win32api.GetLogicalDriveStrings to list
the drive letters currently in use, and find the next free
letter. net use * probably does something like that under
the covers.

hth
Roger
 
L

Lucas Machado

Roger said:
You could use win32api.GetLogicalDriveStrings to list
the drive letters currently in use, and find the next free
letter. net use * probably does something like that under
the covers.

I went and took your advice and this is where I am now:
A:\ C:\ D:\ E:\ Z:\

So I think this function will work great since it gives me a string
that i can simply strip away what i don't need and then split it into a
list and traverse the list. However, when I try to split it, the
following occurs:
['A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00']

I'm a bit of a novice at python (even more so of the win32 api), but
I've used the split and strip functions before (for example to get rid
of '\n' from strings) so it is unclear to me why this does not work.

Thanks
--LM
 
R

Roger Upole

The split should work fine if you remove the r
(raw string) prefix.['A:', 'C:', 'D:', 'E:', 'F:', 'G:', 'H:', 'J:', 'K:', 'Y:', 'Z:', '']

Roger

Lucas Machado said:
Roger said:
You could use win32api.GetLogicalDriveStrings to list
the drive letters currently in use, and find the next free
letter. net use * probably does something like that under
the covers.

I went and took your advice and this is where I am now:
A:\ C:\ D:\ E:\ Z:\

So I think this function will work great since it gives me a string
that i can simply strip away what i don't need and then split it into a
list and traverse the list. However, when I try to split it, the
following occurs:
['A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00']

I'm a bit of a novice at python (even more so of the win32 api), but
I've used the split and strip functions before (for example to get rid
of '\n' from strings) so it is unclear to me why this does not work.

Thanks
--LM
 
Ad

Advertisements

D

Diez B. Roggisch

b = a.strip(r'\\\x00')
['A:\\\x00C:\\\x00D:\\\x00E:\\\x00Z:\\\x00']

I'm a bit of a novice at python (even more so of the win32 api), but
I've used the split and strip functions before (for example to get rid
of '\n' from strings) so it is unclear to me why this does not work.

The string you get ist actually a list of null-terminated strings. And the
byte 0 that you want to have as delimiter for splitting is written

'\x00'

So do this:

b = a.split('\x00')

Read the python docs about strings and raw strings and escaping of
characters to understand the subtle details here.
 

Top