Search code examples
pythonwindowsubuntu-16.04pywin32

accessing files via python using a service account


I am playing with windows server 2012 r2. I have some files on the server. I have a separate service account which has the read access to the files. What i want to do is using python access the files by network share(any other suggestions welcomed) but only through the service account.

PS: i cannot use RDP.


Solution

  • The underlying WINAPIs for this task are part of [MS.Docs]: WNetAddConnection2W function family.

    The [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions wrapper is [ActiveState]: Module win32wnet (it's not the official doc (I couldn't find any at this point) - I don't know for how long the URL will be valid, but it's the best I could find).

    I've prepared a trivial example.

    code00.py:

    #!/usr/bin/env python3
    
    import sys
    import os
    import pywintypes
    import win32wnet
    
    
    CONNECT_INTERACTIVE = 0x00000008
    
    HOST_NAME = "192.168.1.3"
    SHARE_NAME = "Work"
    SHARE_FULL_NAME = os.path.sep * 2 + os.path.sep.join((HOST_NAME, SHARE_NAME))
    SHARE_USER = "cfati"
    SHARE_PWD = "********"
    
    
    def main():
        net_resource = win32wnet.NETRESOURCE()
        net_resource.lpRemoteName = SHARE_FULL_NAME
        flags = 0
        #flags |= CONNECT_INTERACTIVE
        print("Trying to create connection to: {:s}".format(SHARE_FULL_NAME))
        try:
            win32wnet.WNetAddConnection2(net_resource, SHARE_PWD, SHARE_USER, flags)
        except pywintypes.error as e:
            print(e)
        else:
            print("Success!")
    
    
    if __name__ == "__main__":
        print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
        main()
    

    Notes:

    • The password is obfuscated (obviously)
    • This is the simplest functionality (the equivalent of your command), however the function can do much more:

      • One thing that I want to point out. If you:

        • Input some invalid credentials, and
        • Decomment the flags |= CONNECT_INTERACTIVE line

        A credentials dialog box will then pop up

    Output:

    (py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use
    New connections will be remembered.
    
    There are no entries in the list.
    
    
    (py35x64_test) e:\Work\Dev\StackOverflow\q050602112>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code00.py
    Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
    
    Trying to create connection to: \\192.168.1.3\Work
    Success!
    
    (py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use
    New connections will be remembered.
    
    
    Status       Local     Remote                    Network
    
    -------------------------------------------------------------------------------
    OK                     \\192.168.1.3\Work        Microsoft Windows Network
    The command completed successfully.
    
    
    (py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use * /delete /y
    You have these remote connections:
    
                        \\192.168.1.3\Work
    Continuing will cancel the connections.
    
    The command completed successfully.
    
    
    (py35x64_test) e:\Work\Dev\StackOverflow\q050602112>net use
    New connections will be remembered.
    
    There are no entries in the list.