import sys, os, getpass, subprocess
if (len(sys.argv) != 2):
print("\nRDPPassword is a Python 3 program that inserts username")
print("and password into existing RDP file and creates a LNK file")
print("that can be dragged to taskbar's QuickLaunch area.")
print("\nUSAGE: python %s <RDP file>\n" % os.path.basename(sys.argv[0]))
exit(1)
def InsertPassword(rdpfile, user, password):
fileIn = open(rdpfile, "r", encoding="'utf_16_le")
lines = fileIn.read().splitlines()
fileIn.close()
outLines = []
bPromptCredentials = False
bUsername = False
bPassword = False
for line in lines:
if line.startswith('promptcredentialonce:i:'):
outLines.append('promptcredentialonce:i:1')
bPromptCredentials = True
elif line.startswith('username:s:'):
outLines.append('username:s:%s' % user)
bUsername = True
elif line.startswith('password 51:b:'):
outLines.append('password 51:b:%s' % password)
bPassword = True
elif len(line)>0:
outLines.append(line)
if not bPromptCredentials:
outLines.append('promptcredentialonce:i:1')
if not bUsername:
outLines.append('username:s:%s' % user)
if not bPassword:
outLines.append('password 51:b:%s' % password)
fileOut = open(rdpfile, "w", encoding="utf_16_le")
for line in outLines:
#fileOut.write("%s%s" % (line, os.linesep))
fileOut.write("%s%s" % (line, '\n'))
#print("#%s#"%line)
fileOut.close()
def RunShell(command):
# pipes usage taken from: https://superuser.com/questions/1540516/how-do-i-execute-powershell-commands-with-pipes-in-python
PSEXE = r'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
PSRUNCMD = "Invoke-Command -ScriptBlock "
process = subprocess.run([PSEXE, PSRUNCMD + "{" + command + "}"], stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines = True)
#print("CMD", command)
#print("ERR", process.stderr)
#print("OUT", process.stdout)
return process.stdout
if not os.path.exists(sys.argv[1]):
print("\nFile '%s' not found.\n" % sys.argv[1])
exit(1)
if not ( (sys.argv[1].endswith(".rdp")) or (sys.argv[1].endswith(".RDP")) ):
print("\nFile '%s' not a RDP file.\n" % sys.argv[1])
exit(1)
print("\nInserting username/password into '%s'" % sys.argv[1])
username = input("Username: ")
#password = getpass.getpass("Password: ") #use this if you do not want the password to be seen while typing
password = input("Password: ")
insert username/password into RDP file
**************************************
RDP password generation taken from: https://serverfault.com/questions/867467/rdp-file-with-embedded-password-asks-for-password
PassCmd = "('%s' | ConvertTo-SecureString -AsPlainText -Force) | ConvertFrom-SecureString;" % password
password = RunShell(PassCmd)
InsertPassword(sys.argv[1], username, password)
create a LNK file for RDP
*************************
print("\nCreating a LNK file for '%s'" % sys.argv[1])
https://superuser.com/questions/298974/how-do-i-pin-a-remote-desktop-connection-to-the-taskbar
RunRdp = r'"%windir%\system32\mstsc.exe"'
RunArgs = "'"%s"'" % os.path.abspath(sys.argv[1])
https://superuser.com/questions/392061/how-to-make-a-shortcut-from-cmd
LnkCmd = '$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut("%s"); $S.TargetPath = %s; $S.Arguments = %s; $S.Save()' % (sys.argv[1][:-3]+"lnk", RunRdp, RunArgs)
RunShell(LnkCmd)
print("\nDone.\n")