view raw
ryguy20 ryguy20 - 9 months ago 68
Python Question

How can I add Windows Credentials using Python?

I am trying to create a program that automatically installs a network shortcut onto my desktop and downloads a driver (it is a print server for my work), but in order to access the shortcut after it is installed I need to enter my work credentials under the printer network's domain. I tried using the keyring library for Python but that was unsuccessful, I also tried to use win32wnet.WNETADDCONNECTION2() which I have seen posted on several forums but it was also unsuccessful for me.

Here is the code currently

import os, winshell, keyring, win32wnet
from win32com.client import Dispatch
#Add Windows Credentials

win32wnet.WNetAddConnection2(0, None, url, None, USERNAME, PASSWORD)
keyring.set_password(url, USERNAME, PASSWORD)
keyring.get_password(url, USERNAME)

# This is where I am having troubles.

# Create the shortcut
desktop = winshell.desktop()
path = os.path.join(desktop, "MYLINK.lnk")
target = url

# Set path and save
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(path)
shortcut.Targetpath = target

# Open the shortcut

In my full program I have an interface using Kivy that asks for my Username and Password and then I hit an "Install" button and it adds the domain to my Username (domain\username). Using keyring it was properly showing up, just in the wrong area so that should no be an issue, I just can't find a method to add a Windows Credentials instead of General Credentials.

I am using python2.7 on a Windows 10 computer.

If anyone knows of a library I could use or another method that would be great. Thanks!


I'm pretty sure you can achieve this by creating a .bat file and executing it in your program.

For Ex.

def bat_create_with_creds(id, pwd):
    """create Batch file to init printer domain creds with variables"""

    auth_print= open("auth_print.bat", "w")

    auth_print.write("NET USE \\printserverip /USER:%"+id+"% %"+pwd+"%")

I haven't done this for your particular use case, but it works perfectly well for init. rdp sessions using windows mstsc for example.