2

We use cygwin in windows platform for some maintaining work. What I want to do is not only start the cygwin using python or start it and pass a single command.

I want to start the cygwin, hold the session, then I pass a command to it if I need to, and get the console output, then if I want, I pass the second command and get the output...

I want to do this is because our company use a bunch of commands communicate with a server, something similar with below: Step 1: login Step 2: initInfo Step 3: getStatus=Port1

I cannot pass the commands once since I need to control which command should I pass next base on the output of the earlier command.

I can use the command below to start the mintty.exe and get the process id, how can I pass a command to this certain mintty application and get the output using python, and I need to communicate with the mintty for several times without starting a new mintty each time when I want to execute a command.

p_mintty = subprocess.Popen(['c:/cygwin/bin/mintty.exe', '-'], shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

Any of your suggestion is warmly welcomed!

lowitty
  • 904
  • 1
  • 8
  • 13

1 Answers1

1

Try this:

program = subprocess.Popen(['c:/cygwin/bin/mintty.exe', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

program.stdin.write("command1")
stdout1, stderr1 = program.communicate()

program.stdin.write(change_something(stdout1))
stdout2, stderr2 = program.communicate()
..etc
icedtrees
  • 6,134
  • 5
  • 25
  • 35
  • Thanks for your answer but it seems that the program just hang on the line, "stdout1, stderr1 = program.communicate()" and the "command1" which I write to the mintty windows didn't show up either. – lowitty Mar 03 '14 at 09:11
  • What exactly are the commands you are putting in, and what are you expecting out? – icedtrees Mar 03 '14 at 09:20
  • A simple command "ls -al", since I just want to verify it, I used a simple command. – lowitty Mar 03 '14 at 09:25
  • When I try to run with ls -al, I get an error on program.stdin.write, since ls -al does not take standard input, which is correct. However program.communicate() works fine. What happens when you try it? – icedtrees Mar 03 '14 at 09:49
  • When I execute the program, it works fine untile the line "stdou1, stderr1 = program.communicate()", the debug will get stuck at this line, I guess it was because the python script cannot communicate with the mintty.exe, like, mitty is more like an application than a shell window. – lowitty Mar 04 '14 at 02:02