2016-03-21 22 views
-1

pythonスクリプトで次のコマンドを実行する方法は?Pythonスクリプトにbashコマンドを実行

echo "show stat" | socat unix-connect:/var/run/haproxy/admin.sock stdio | grep webservers,BACKEND | cut -d , -f8 

私はこの

import subprocess 
var = subprocess.check_output(echo "show stat" | socat unix-connect:/var/run/haproxy/admin.sock stdio '| grep ' webservers,BACKEND' | cut -d , -f8', shell=True) 
var=int(var) 

を試みたが、それは動作しません!

+1

'VAR = subprocess.check_output( """エコー "ショースタット" | UNIX-接続socatに関する:/var/run/haproxy/admin.sockのstdioの '| grepを' Webサーバー、BACKEND '| cut -d、-f8' "" "、shell = True)' –

+0

この質問を確認するhttp://stackoverflow.com/questions/27911820/python-subprocess-with-quotes-and-pipe-grep –

+1

コマンドではなく文字列を渡す必要があります。 – asimoneau

答えて

0

Popenで試してみてください:

from subprocess import Popen, PIPE 

p1 = Popen(["echo", "show stat"], stdout=PIPE) 
p2 = Popen(["socat", "unix-connect:/var/run/haproxy/admin.sock", "stdio"], stdin=p1.stdout) 
p3 = Popen(["grep", "webservers,BACKEND"], stdin=p2.stdout) 
p4 = Popen(["cut", "-d", ",", "-f8"], stdin=p3.stdout) 
関連する問題