さて、そうです。私は非常に基本的なPythonスクリプトを書いています。このスクリプトはdf -hからの出力を画面に表示します。私はいくつかの他の機能を実装しようとしていますが、この部分は私を困惑させてしまいます。Pythonのsubprocess.Popen stdoutは空白を追加し、用語を破損します。
私は自分のコードが正しいと確信しています。私はtime.sleep()文を入れて出力があまりにも速くなっていたかどうかを確認しました。しかし、stdoutを繰り返しても、rstrip()であっても、新しい行が作成されるたびに端末出力が奇妙な間隔で配置され、端末が破損します。
Anythoughts?
ここに私のコードです:
#!/usr/bin/python
import sys
import os
import re
import subprocess
import time
np1=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
np2=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p1=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p2=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p3=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
p4=subprocess.Popen('ssh -qt <redacted>/usr/bin/sudo df -h /opt/saswork', shell=True, stdout=subprocess.PIPE)
for x in np1.stdout:
x=x.rstrip()
x=re.findall('.\d%', x)
for y in x:
print "SAS Grid NP01 is at ", str(y)
time.sleep(1)
for x in np2.stdout:
x=x.rstrip()
x=re.findall('.\d%', x)
for y in x:
print "SAS Grid NP02 is at ", str(y)
time.sleep(1)
for x in p1.stdout:
x=x.rstrip()
x=re.findall('.\d%', x)
for y in x:
print "SAS Grid P01 is at ", str(y)
time.sleep(1)
for x in p2.stdout:
x=x.rstrip()
x=re.findall('.\d%', x)
for y in x:
print "SAS Grid P02 is at ", str(y)
time.sleep(1)
for x in p3.stdout:
x=x.rstrip()
x=re.findall('.\d%', x)
for y in x:
print "SAS Grid P03 is at ", str(y)
time.sleep(1)
for x in p4.stdout:
x=x.rstrip()
x=re.findall('.\d%', x)
for y in x:
print "SAS Grid P04 is at ", str(y)
それがサービスアカウントの私をドロップするまで、ここで私は、この時点で何をすべきか、出力
SAS Grid NP01 is at 33%
SAS Grid NP02 is at 36%
SAS Grid P01 is at 3%
SAS Grid P02 is at 23%
SAS Grid P03 is at 41%
SAS Grid P04 is at 24%
[<service account>@werindgatep01 ~]$
[<service account>@werindgatep01 ~]$
は、CTRL-CおよびCTRL-Dでいます私の標準的なユーザーアカウントに戻ります。そこからsu sudoサービスアカウント
私は迷っています。 。 .quite lost
次の質問をする前に、[ask]、具体的には[mcve]をお読みください。サンプルプログラムのサイズを縮小し、無関係な要素を取り除くことで、受け取ったレスポンスの質が向上します。 –
使用しているOSは何ですか? Linuxの場合は、どのディストリビューションとバージョンですか? –
'print'ステートメントは、印刷するものの間にスペースを追加するものです。カンマで区切ったリストのたびにスペースを挿入します。それをより良くコントロールするには、関数形式を使うか、単に 'sys.stdout.write()'メソッドを使います。 – Keith