简体   繁体   中英

python:send command with subprocess

I am using ipmitool to start an serial over lan connection on the VSP of an HP. I am trying to send the break command with the letter c .

p = subprocess.Popen(CMD + " sol activate", shell=True, stdout=subprocess.PIPE)
#I want to send '~~B' and 'c' while watching the output.
while True:
    output = p.stdout.readline()
    if output:
        print output

As far as I can understand you want pressing "c" button to kill the process that you run with subprocess. Use this recipe to detect key and write simple function to call inside a thread like this:

import subprocess
import os
import signal
import thread
from getch import _Getch

def kill(pro):
    if _Getch()() == "c":
        os.killpg(os.getpgid(pro.pid), signal.SIGTERM)

CMD="sudo apt-get update"
p = subprocess.Popen(CMD, shell=True, stdout=subprocess.PIPE)
thread.start_new_thread(kill, (p,))
while True:
    output = p.stdout.readline()
    if output:
        print output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM