Python Forum
How to use subprocess send commands to windows shell
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use subprocess send commands to windows shell
#1
I want to send multiple commands to a shell program depends on the output.
But I can not figure out how to send multiple commands.

Python ver 3.6.8
Windows 10

Base on the reference, but my code is not workring, it only print the "ipconig", the "dir" is not working and raising error.
from subprocess import Popen, PIPE

process = Popen("ipconfig", stdin=PIPE, stdout=PIPE, shell = True, cwd = "d:")
print(repr(process.stdout.read()))
process.stdin.flush()
process.stdin.write(b'dir\n')
print(repr(process.stdout.read()))
process.stdin.flush()
process.stdin.write(b'tasklist\n')
print(repr(process.stdout.read()))
Error:
Quote:Traceback (most recent call last):
File "d:\Inbox\Desktop\delete m2e.py", line 8, in <module>
process.stdin.flush()
OSError: Errno 22 Invalid argument
Reply
#2
Here's a modified version from python docs: https://docs.python.org/3/library/subprocess.html
run on Linux
import shlex, subprocess


def run_subprocess(command_line):
    args = shlex.split(command_line)
    print(args)
    p = subprocess.Popen(args) # Success!


if __name__ == '__main__':
    run_subprocess('/bin/ls -latr')
    run_subprocess('ls -latr /home')
Reply
#3
(Nov-21-2019, 07:27 PM)Larz60+ Wrote: Here's a modified version from python docs: https://docs.python.org/3/library/subprocess.html
run on Linux

Thank you for your reply Larz60+.

From my understanding, your code is calling 2 subprocess.

What I want to do is to call one subprocess, then read that process's outputs, then send other commands to that process.
Reply
#4
How to use subprocess send commands to windows shell
See : https://www.pythonforbeginners.com/os/su...nistrators
Run the command described by "args".
We can run the command line with the arguments passed as a list of strings
(example 1) or by setting the shell argument to a True value (example 2)
Note, the default value of the shell argument is False.
Let's look at two examples where we show the summary of disk usage using
subprocess.call()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  batch file for running python scipt in Windows shell MaartenRo 2 1,883 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,576 May-17-2021, 11:29 AM
Last Post: throwaway34
  Using a .bat file to execute simple anaconda commands in Windows 10 gacg1 0 4,670 Nov-30-2020, 03:24 PM
Last Post: gacg1
  Need to access Windows Machine or Personal Certificate Store and send to web app seswho 0 1,634 Sep-14-2020, 04:57 PM
Last Post: seswho
  Subprocess command prompt (Windows) arnaur 6 10,293 Sep-06-2018, 07:22 AM
Last Post: arnaur
  Subprocess output in windows Ohmganesh83 5 8,697 Aug-20-2018, 02:32 PM
Last Post: Ohmganesh83
  [inconsistent output] subprocess.call to run cmd commands to get Kerberos ticket Yelin 2 4,986 Jun-08-2018, 09:02 AM
Last Post: Yelin
  SOLVED: best way to block (wait on) shell calls to multiple windows programs at once? ezdev 0 2,598 Dec-10-2017, 06:42 AM
Last Post: ezdev
  Windows/linux shell emulation AceScottie 3 3,439 Nov-27-2017, 01:31 PM
Last Post: wavic
  os.environ not setting current shell variables in Windows? brian6667 2 10,808 Apr-26-2017, 12:59 PM
Last Post: brian6667

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020