1

Need help in calling .bat file via python in windows server.

Currently the .bat file is used to encrypt the password and generate password file .

when the admin run this .bat file, they ll execute below command

runbatch.bat filename

On executing the above it prompt us to enter password

....Enter the password to encrypt

On entering the password it generate the password file in the specified folder.

Now the requirement is to call this .bat file via python. Reason behind this to create an API and give option to user to generate the password file from their end. When user tries to generate the password file it prompts for password and password file name and passes those parameters to python script which in return calls a .bat file

I am not sure how to echo password input as we do directly in cmd.

Echo password| runbatch.bat filename

In python I have defined .bat location on os.dir and using popen subprocess option to run the.bat` but not sure how to pass password to the command.

 import sys 
 import os 
 import subprocess 
 sPassword = $Password 
 sPasswordFile = $PasswordFile 
 sInstance = "D:/API/bin" os.chdir(sInstance) 
 ScriptName = sInstance + "/encryptpassword.bat" 
 command = '%s "%s"' % (ScriptName, sPasswordFile) 
 p = subprocess.Popen(command) retcode = p.wait()

Any help will be highly appreciated.

11
  • 2
    Please paste a python script file and contents of the working directory so that it's easy to figure the problem. Commented Oct 15, 2019 at 21:37
  • I think you might find my answer to the (mostly unrelated) question Run multiple programs sequentially in one Windows command prompt? handles I/O with subprocess.Popen() helpful. There's a simpler example in my answer to NameError: name 'buffer' is not defined with Ant Based framework batch file. Commented Oct 15, 2019 at 22:33
  • Hi Martineau, Thanks for your response. Will look in to the link and get back to you if any questions. Commented Oct 16, 2019 at 8:41
  • @sarv2k: OK, but be sure to prefix my username with an @ character so I will be notified — the only reason I'm responding now is because I happened to take a look at your question again. Commented Oct 16, 2019 at 18:32
  • @@sarv2k: Also, please paste that .bat file's code into your question, not down here in a comment (where it's very hard to read). Commented Oct 16, 2019 at 18:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.