python - Supress output of subprocess -
i want use subprocess module control processes spawned via ssh.
by searching , testing found works:
import subprocess import os import time node = 'guest@localhost' my_cmd = ['sleep','1000'] devnull = open(os.devnull, 'wb') cmd = ['ssh', '-t', '-t', node] + my_cmd p = subprocess.popen(cmd, stderr=devnull, stdout=devnull) while true: time.sleep(1) print 'normal output'
the -t -t option provide allows me terminate remote process instead of ssh command. this, scrambles program output newlines no longer effective making long , hard read string.
how can make ssh not affecting formatting of python program?
sample output:
guest:~$ python2 test.py normal output normal output normal output normal output normal output normal output normal output (first ctrl-c) normal output normal output normal output (second ctrl-c) ^ctraceback (most recent call last): file "test.py", line 13, in <module> time.sleep(1) keyboardinterrupt
ok, output clear. not know why, command ssh -t -t
puts local terminal in raw mode. makes sense anyway, because intended allow directly use curses programs (such vi) on remote, , in case, no conversion should done, not simple \n
-> \r\n
allows simple new line leave cursor on first column. not find reference on in ssh documentation.
it (-t -t
) allows kill remote process because raw mode let ctrl + c sent remote instead of being processed local tty driver.
imho, design smell, because use side effect of pty allocation pass ctrl + c remote , suffer side effect raw mode on local system. should rather process standard input (stdinput = subprocess.pipe
) , explicitely send chr(3)
when input special character on local keyboard, or install signal handler sig-int it.
alternatively, workaround, can use os.system("stty opost -igncr")
(or better subprocess equivalent) after starting remote command reset local terminal in acceptable mode.
Comments
Post a Comment