3

I want to call an exe-file and already input parameters/input-data.

cmd = dir_path + 'file.exe do something test'
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT)

This is already working fine. If you do that by using the exe-file you would press enter to execute the next step which would be entering your username. Then password and so on. Now I dont know how to implement this into my script, that this steps are all done my the script. Basically i dont know how to do the ENTER step with python. Thx for any help.

UPDATE

Tried now 2 ways (all mentioned within this question or others on stackoverflow).

way 1>

p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)
# cmd represents the path of the exe file 
p.stdin.write(username+"\n")
p.stdin.write(password+"\n")
p.sdin.close()

way 2

p = subprocess.Popen(cmd, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, universal_newlines=True)

out,err = p.communicate("\n".join([username,password,"\n"]))
# last \n should terminate the exe file and communicate should wait for the process to exit

Both ways present me with the same output in one line:

Username: Password:

normally i hoped it would be:

Username: "username wrote by process + \n"
Password: "password wrote by process + \n"

the application is running on windows7 (has to be) and the exe file runs within command line.

So is it maybe possible that the exe does not support way1 and way2 so its not possible to write it through a subprocesss or am I making a major mistake?!

thanks again for any help.

vicR
  • 789
  • 4
  • 23
  • 52
  • search for `PyAutoIt` – dot.Py Jun 15 '16 at 14:03
  • looks good, but i cannot install software or libs on my working pc. is there a solution with subprocess or something similar ? – vicR Jun 15 '16 at 14:12
  • How do you normally entering username, password? Is it a GUI application? Note: [the application may read/write directly to the console outside of its standard input/output streams](http://stackoverflow.com/q/20980965/4279) i.e., `stdin=PIPE` might not help. What is your OS? On Unix, you could [use `pexpect` and here's why](http://pexpect.readthedocs.org/en/stable/FAQ.html#whynotpipe). – jfs Jun 16 '16 at 00:43

1 Answers1

3

You're almost there!

You can do the following:

import subprocess
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, shell=True)
p.communicate(input='\r')

You may need to use \n instead of \r if there are any problems. \r is a carriage return, and \n is a newline.

AbrahamB
  • 1,348
  • 8
  • 13
  • hmm ok. means that communicate sends an "\r" which terminates. but i want to stay in "terminal" and just make a "virtual" enter. – vicR Jun 15 '16 at 15:15
  • /r should simulate a carriage return (which is an enter press), but /n would be a newline. Does /n work for you? – AbrahamB Jun 15 '16 at 16:13
  • ah ok. so if want to add a new cmd after the '\r' can i do another p.communicate(input='some cmd') or do i need to Popen another subprocess (guess not wouldnt make sense). – vicR Jun 15 '16 at 22:12
  • means: 1. cmd = "file.exe parameter" 2. input="\r" 3. new cmd = username 4. input = "\r" and so on – vicR Jun 15 '16 at 22:14
  • @vicR If `cmd` accepts input from its stdin then you could pass the input as: `p.communicate("\n".join([username, password, etc])` (pass `universal_newlines=True` to `Popen()`, to enable text mode). To emulate Enter, pass `'\n'`. – jfs Jun 16 '16 at 00:46
  • @RobBlagg, I would encourage you to file a new request for that question, since it seems a bit different. – AbrahamB Aug 16 '17 at 17:12