Search code examples
pythonwindowscygwinmintty

Communicate with (Execute commands in) Cygwin(mintty) using python and get the output


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!


Solution

  • 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