commands 模塊
通過python調用系統命令 只適用於linux
commands是提供linux系統環境下支持使用shell命令的一個模塊
commands.getoutput(cmd)
只返回執行shell命令的結果
import commands
cmd = 'ls /opt'
a = commands.getoutput(cmd)
print(type(a))
print(a)
# <type 'str'>
# auto-mongo-primary.sh
# install-zabbix.sh
# python.py
# soft
commands.getstatusoutput(cmd)
import commands
cmd = 'ls /home/admin'
c = commands.getstatusoutput(cmd)
print(type(c))
status, output = commands.getstatusoutput(cmd)
print(status)
print(output)
print(type(output))
# <type 'str'>
# 0
# auto-mongo-primary.sh
# install-zabbix.sh
# python.py
# soft
返回結果是一個tuple,第一個值是shell執行的結果,如果shell執行成功,返回0,否則,為非0,第二個是一個字符串,就是我們shell命令的執行結果,python通過一一對應的方式復制給status和output
