0

I am trying to query a database from Python. Unfortunately this database uses Windows Authentication which is not the account I log in to my computer with. To get around this issue I decided to try to query the database with a cmd runas command:

runas /netonly /env /user:<DOMAIN>\<USER> "sqlcmd /i query.sql /o output.csv /E /S SERVER /d DATABASE

Running this command in a cmd.exe window will prompt me for my password and then give me the desired csv file.

But I can't get Python to properly deal with it. When I do:

p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate(password)

It seems that Popen finishes without giving me chance to input the password. When I put a sleep between the two commands the csv already appears before the sleep finishes. So when do I get the chance to input the password?

When I do:

p = subprocess.call(command)

The process never finishes. I guess it is waiting on me to input my password. But now I believe I don't have a way to input it...


Using python to open cmd and automatically enter a password

The both answers to the above question fails for the same reason as my first option. I get the csv file just containing the error message that the credentials are no good (even though the credentials are fine if I run the prompt from a cmd.exe window).

Bob
  • 614
  • 1
  • 7
  • 19
  • That question is indeed similar, but the solutions do not solve my case. So there must be a relevant difference in this case. – Bob Jun 27 '17 at 13:32
  • PowerShell makes it easy to `Get-Credential` and `Invoke-Sqlcmd`. http://duffney.io/AddCredentialsToPowerShellFunctions – lit Jun 27 '17 at 21:38

0 Answers0