2

@hybris95 - since the scope of the question has changed and you are responsive, I'll mark your initial answer as the solution, but I posted a follow-up question

Script cannot read password


My python script executes another script as an elevated user. Unfortunately it asks me for password in the middle of execution.

How do I hardcode the password, or automatically get it read in from elsewhere? Is there a workaround that can be done in Python?

import os
import sys
import win32com.shell.shell as sh   
ASADMIN = '/user:DOMAIN\user'
os.system('"runas /user:DOMAIN\user "D:/Python27/python.exe myscript.py""')
sys.exit(0)

if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ''.join([ASADMIN] + ['D:\Python27\python.exe',script] + sys.argv[1:])
    sh.ShellExecuteEx(lpVerb='runas',lpFile=sys.executable,lpParameters=params)
    sys.exit(0)

Updated code, but still getting error

import os
import sys, subprocess, socket, string
import wmi, win32api, win32con
import win32com.shell.shell as sh   

command = 'runas /user:DOMAIN\username "D:/Python27/python.exe myscript.py"'

pst = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE)

pst.communicate("password123")

Error I get

Attempting to start D:/Python27/python.exe script.py as user "DOMAIN\username" ...
RUNAS ERROR: Unable to run - D:/Python27/python.exe myscript.py
1326: Logon failure: unknown user name or bad password.
Community
  • 1
  • 1
Glowie
  • 2,271
  • 21
  • 60
  • 104
  • @goncalopp --- I am mentioning Python in case there is a workaround that can be done in Python – Glowie May 19 '14 at 14:13

2 Answers2

1

You can use subprocess.Popen() to spawn subprocess and Popen.communicate() to provide input (password) to it:

p = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE)
p.communicate(password)
endragor
  • 368
  • 2
  • 9
  • I tried this but I get error "1326: Logon failure: unknown user name or bad password." Will post updated code in original question – Glowie May 19 '14 at 17:12
1

Since RunAs doesn't provide anything about not prompting password.
You can pass the password via a file.

For example (within a shell) I would do that :
runas /user:DOMAIN\user "D:/Python27/python.exe myscript.py < password.txt
Where password.txt contains my password

Edit :
You can also give the password hardcoded with a pipe :
echo "MyPassword" | runas /user:DOMAIN\user "D:/Python27/python.exe myscript.py

Hybris95
  • 2,286
  • 2
  • 16
  • 33
  • 1
    When I try os.system('"runas /user:DOMAIN\username "D:/Python27/python.exe script.py < password.txt""') the program still prompts me for password AND when I try os.system('"echo password123 | runas /user:DOMAIN\username "D:/Python27/python.exe script.py""') I get "1326: Logon failure: unknown user name or bad password." – Glowie May 19 '14 at 17:43
  • Does it work when you type yourself the password after being prompted for it ? – Hybris95 May 20 '14 at 08:28
  • 1
    I tried and it didn't work. I am 100% positive I am using correct password. – Glowie May 20 '14 at 17:25
  • since the scope of the question has changed and you are responsive, I'll mark your initial answer as the solution, but I'll post another question. – Glowie May 20 '14 at 17:55
  • 1
    The trouble is a lot of command line tools that ask for a password don't use Std In so pipes etc don't work – Geordie Dec 28 '17 at 20:34