Need help with Subprocess usage

I want to open two terminals and provide some commands in both the terminals.
Now I am not able to provide any input in any of the terminals.
process.stdin.write() is not working
Please help me out :frowning:

p = subprocess.Popen(["start", "cmd", "/k", r'ipconfig'], stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE, universal_newlines=True, shell=True)
p1 = subprocess.Popen(["start", "cmd", "/k", "dir"], stdin=p.stdout,
                     stdout=subprocess.PIPE, universal_newlines=True, shell=True)
output = p1.communicate()[0]

In what way? Describe what result you expect, and what result you are seeing instead.

In general, you should avoid using stdin.write directly. Use communicate with an argument instead.

Are you trying to redirect the output of p to the input of p1? If so, try something like this:

from subprocess import run, PIPE

p1 = run(["start", "cmd", "/k", r'ipconfig'], stdout=PIPE, universal_newlines=True)
p2 = run(["start", "cmd", "/k", "dir"] + p1.stdout.split(), universal_newlines=True)

That will append the output of the first command to the second command. Untested because I don’t use Windows, so don’t know if using the output of ipconfig as an argument to dir makes sense.

Please try to explain what you’re looking to accomplish here. It makes no sense to pipe the output of “ipconfig.exe” into the standard input of a shell. The output from “ipconfig.exe” is the current IP configuration, not a set of commands to execute.

At a technical level, I can tell you that the CMD shell relies on implicit inheritance of the standard handles (i.e. stdin, stdout, stderr). However, its internal start command (if executed without the /b option) uses the process creation flag CREATE_NEW_CONSOLE. The only way to inherit a set of standard handles to a new console session is to do so explicitly via the process STARTUPINFO record, as subprocess.Popen does. Therefore, instead of using shell=True to execute CMD’s start command, use creationflags=subprocess.CREATE_NEW_CONSOLE and shell=False.

The above code is just an example I gave i.e. I want to open two terminals and provide some sequence of commands in each of the terminals.
The actual problem statement is as below:

  1. Open a terminal (terminal A) in windows. In that terminal provide ssh command to connect to another system. Once you get connected to that system, provide some series of commands to run a script.
  2. Again open one more terminal and get connected to the same system and perform some more ssh commands.

To achieve this I am trying subprocess, I have different creationflags like CREATE_NEW_CONSOLE and DETACHED_PROCESS, after the terminal opens i am not able to provide any inputs on that terminal by process.stdin(). Nothing is getting typed in the terminal.

Using pipes for standard I/O may not work to interactively drive an ssh session. The ssh program may fully buffer its output when writing to a pipe. In this case the output from a command won’t be written to the pipe until the buffer is full. The size of the buffer is typically 1-8 KiB. The ssh program may have a command-line option to disable buffering. Also, standard input is usually line buffered, so make sure to end a command with a line feed ("\n").

If this worked, it wouldn’t generally require a new console session, so I still don’t see why you would need CREATE_NEW_CONSOLE.