Python Authentication for SAMBA Sharing

I can map a drive without network resource issues without authentication. But I will miss something when I try to authenticate with a username and password. Here is the current working code example with the error message that I keep getting.

#!/usr/bin/python
# Drive Map Script
import pywintypes
import win32com.client

# Disconnect previous mapped drives
testnetwork = win32com.client.Dispatch('Wscript.Network')
network_drives = testnetwork.EnumNetworkDrives()
for mapped_drive in [network_drives.Item(i)
                     for i in range(0, network_drives.Count() -1 , 2)
                     if network_drives.Item(i)]:
    testnetwork.RemoveNetworkDrive(mapped_drive, True, True)

# Mount the drives
drive_mapping = [
    ('z:', '\\\\192.168.1.100\\Some_Share', 'someuser', 'somepass')]

for drive_letter, network_path, user_name, user_pass in drive_mapping:
    try:
        testnetwork.MapNetworkDrive(drive_letter, network_path)
    except Exception, err:
        print err

And the error that the code generates on execution:

(- 2147352567, "An exception occurred", (0, u'WSHNetwork.MapNetworkDrive ', u'Logon failure: unknown username or invalid password. \ R \ n', None, 0, -2147023570), None)

+3
source share
1 answer

You do not go through user_nameand user_passto MapNetworkDrive. Try instead:

testnetwork.MapNetworkDrive(drive_letter, network_path, True, user_name, user_pass)

: True , , .

+2

Source: https://habr.com/ru/post/1732015/


All Articles