python 可以使用 os 模塊來調用外部的 Linux Shell 命令,常用的方法如下:
os.system():結果輸出在終端上,捕獲不到
os.popen() : 結果返回一個對象,即標准輸出
os.popen2():結果返回兩個對象,分別是標准輸入,標准輸出
os.popen3():結果返回三個對象,分別是標准輸入,標准輸出,標准錯誤輸出
os.popen4():結果返回兩個對象,分別是標准輸入,標准輸出(標准輸出中包括標准錯誤輸出)
In [2]: stdout = os.system('ls /data') # os.system() --- 結果輸出在終端上,會返回執行命令的狀態碼,我們可以用變量來接收 1.txt 2.txt 3.txt In [3]: print(stdout) 0
In [7]: stdout = os.popen('ls /data') # os.popen() --- 結果返回一個對象,即標准輸出,標准輸出需要用read()、readlines()等方法讀取 In [8]: print stdout.read() 1.txt 2.txt 3.txt
In [24]: stdin, stdout = os.popen2('cat') # os.popen2() --- 結果返回兩個對象,分別是標准輸入,標准輸出 In [25]: stdin.write('hello\n') # 我們使用 write() 來進行輸入,使用 close() 來結束輸入,使用 read() 來讀取輸出 In [26]: stdin.write('world\n') In [27]: stdin.close() In [28]: print stdout.read() hello world
使用 os 模塊來調用 shell 命令已經過時了,官方建議我們使用 subprocess 模塊,subprocess 會開啟一個子進程(即shell)來執行命令:
subprocess.call() :用於執行 shell 命令,但結果不能被捕獲到,只會輸出到終端,如 subprocess.call('ls -l /data', shell=True)
subprocess.check_call() :用法與 call() 一致,不同的是,如果命令執行失敗,會拋出 CalledProcessError 異常,我們可以根據這個異常去決定需要做什么
subprocess.Popen() :用於執行 shell 命令,結果返回三個對象,分別是標准輸入,標准輸出,標准錯誤輸出
In [1]: from subprocess import Popen, PIPE In [2]: p = Popen('ls /data', stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True) # stdin用於接收標准輸入,可選參數,可以通過 p.stdin.write('xxx') 添加標准輸入 # stdout用於接收標准輸出,可選參數,可以通過 p.stdout.read() 讀取標准輸出 In [3]: p.stdout.read() # stderr用於接收標准錯誤輸出,可選參數,可以通過 p.stderr.read() 讀取標准錯誤輸出 Out[3]: '1.txt\n2.txt\n3.txt\n' # shell=True用於表明該命令是在shell下執行的,如果不加上該參數會報錯 # 如果我們沒有用PIPE來接收標准輸出和標准錯誤輸出,則默認會打印到終端上 In [4]: p.stderr.read() # 另外要注意的是,當我們寫成 p = Popen(...) 時,命令就已經執行了,執行后的結果放在管道(PIPE)里面 Out[4]: '' # subprocess.Popen()其他常用方法
線上的用法:
import subprocess def runCMD(self, ip, cmd, logfile): out = "" sshcmd = ''' ssh root@%s -n '%s' ''' % (ip, cmd) p = subprocess.Popen(sshcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) while True: buff = p.stdout.readline() if p.poll() != None and buff == "": break if buff: out += buff self.writeLogFile(logfile, buff) return p.returncode, out