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.

sarv2k
  • 11
  • 2
  • 2
    Please paste a python script file and contents of the working directory so that it's easy to figure the problem. – ajmartin Oct 15 '19 at 21:37
  • I think you might find my answer to the (mostly unrelated) question [Run multiple programs sequentially in one Windows command prompt?](https://stackoverflow.com/questions/4415134/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](https://stackoverflow.com/questions/4427494/nameerror-name-buffer-is-not-defined-with-ant-based-framework-batch-file). – martineau Oct 15 '19 at 22:33
  • Hi Martineau, Thanks for your response. Will look in to the link and get back to you if any questions. – sarv2k Oct 16 '19 at 08: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. – martineau Oct 16 '19 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). – martineau Oct 16 '19 at 18:36
  • @Martineau, once again thanks for guiding. – sarv2k Oct 17 '19 at 14:58
  • @ajmartin, Thanks for your response. updated the code. – sarv2k Oct 17 '19 at 14:59
  • Sorry but I cannot understand the requirements – Pitto Oct 17 '19 at 15:08
  • @Pitto , no sorry I do read my question back and its difficult to get the requirement clearly. In simple I am trying to call the .bat file via python and this .bat file will prompt for user input so I am looking to run this .bat by taking input from variable which is already declared. – sarv2k Oct 17 '19 at 16:49
  • Start easy before complicating things. Try to understand how to feed password to your batch script without python just using the command prompt. Does it work if you use a text file containing the password? example: your_batch.bat < password.txt – Pitto Oct 17 '19 at 19:54
  • Refer to this answer -- https://stackoverflow.com/a/165662 – ajmartin Oct 22 '19 at 07:35

0 Answers0