先翻译一段文章:https://stackoverflow.com/questions/11344557/replacement-for-getstatusoutput-in-python-3
在最后的“注意”一节中,给出笔者自己的看法。
在Python 2中,经常使用commands模块来执行shell的命令,尤其是常用getstatusoutput()函数。但是在Python 3中,突然发现这个函数没有了。为什么呢?
因为getstatusoutput()这个函数有一个很大的缺陷,就是它的返回值中无法区分stderr和stdout.
那么在Python 3中如果要调用一个命令,如何做呢?有2种方法。其实它们在Python 2中也适用。
方法1. 使用subprocess.check_output()函数
如果命令调用失败,check_output()会抛出一个CalledProcessError的异常。
如果要看到stderr的内容,即合并stdout和stderr,可以把 stderr=subprocess.STDOUT 作为check_output的一个参数。
方法2. 自己定制一个get_status_output()函数,内容如下:
-
def get_status_output(*args, **kwargs):
-
p = subprocess.Popen(*args, **kwargs)
-
stdout, stderr = p.communicate()
-
return p.returncode, stdout, stderr
注意:
1. 在调用check_output()或定制化的get_status_output()的时候,“ls -l”这样的命令要写成“ls -l”.split(' '), 即把命令和参数等作为一个数组输入,而并非一个字符串。
2. 笔者做了一些测试:
方法1无论在Python 2还是Python 3中皆是可行,但要注意,在Python 3中返回的output是bytes,不是str类型;
方法2无论在Python 2还是Python 3中皆是失败的:stdout和stderr始终为None,虽然命令确实被执行了并且也有返回,但是stdout和stderr却拿不到返回。
但是如果使用方法1,就必须去捕捉异常,否则无法处理命令执行失败的情况。
相比之下,又有多少人会去在意stdout和stderr呢?大概会有一些特殊的情况需要处理stdout和stderr的区分吧。
所以,回过头来,感觉Python2的commands.getstatusoutput()其实是一个很方便的函数,写起来很快,1行就结束了。
利用commands模块执行shell命令
利用commands模块执行shell命令
用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要函数:
- commands.getoutput('shell command')
执行shell命令,返回结果(string类型)
>>> commands.getoutput('pwd')
'/home/oracle'
- commands.getstatus('file')
该函数已被python丢弃,不建议使用,它返回 ls -ld file 的结果(String)(返回结果太奇怪了,难怪被丢弃)
>>> commands.getstatus('admin.tar')
'-rw-rw-r-- 1 oracle oracle 829440 Jan 29 10:36 admin.tar'
- commands.getstatusoutput('shell command')
执行shell命令, 返回两个元素的元组tuple(status, result),status为int类型,result为string类型。
cmd的执行方式是{ cmd ; } 2>&1, 故返回结果包含标准输出和标准错误.
>>> commands.getstatusoutput('pwd')
(0, '/home/oracle')
下面的一个脚本利用commands模块检测磁盘使用率,标识出大于10%的磁盘(百分比可根据实际情况调整,一般设为90%,本例为了更好的说明情况,设为10%):
import commands
threshold = 10
flag = False
title=commands.getoutput("df -h|head -1")
'''
Check sda disk space usage like below format:
/dev/sda2 20G 2.3G 17G 13% /
/dev/sda6 20G 306M 19G 2% /var
/dev/sda3 49G 2.8G 44G 7% /home
/dev/sda5 49G 4.5G 42G 10% /opt
/dev/sda1 194M 12M 172M 7% /boot
'''
chkDiskList=commands.getoutput("df -h|grep sda").split('\n')
usedPercents=commands.getoutput("df -h|grep sda|awk '{print $5}'|grep -Eo '[0-9]+'").split('\n')
for i in range(0,len(usedPercents)):
if int(usedPercents[i]) >= threshold:
chkDiskList[i] += ' ----Caution!!! space usage >= ' + str(threshold)
flag = True
'''
Check disk space usage like below format:
/dev/mapper/backup-backup_lv
751G 14G 699G 2% /backup
/dev/mapper/data-data_lv
751G 172G 540G 25% /data
'''
chkDiskList_2=commands.getoutput("df -h|grep -v sda|grep -v tmp|grep -v system").split('\n')
usedPercents_2=commands.getoutput("df -h|grep -v map|grep -v sda|grep -v tmp|grep -v system|awk '{print $4}'|grep -Eo '[0-9]+'").split('\n')
for i in range(0,len(usedPercents_2)):
if int(usedPercents_2[i]) >= threshold:
chkDiskList_2[i*2 + 1] += ' ----Caution!!! space usage >= ' + str(threshold)
flag = True
if flag == True:
#combine tile, chkDiskList, chkDisklist_2
result = [title,]
result.extend(chkDiskList)
result.extend(chkDiskList_2)
for line in result:
print line
假设当前的磁盘使用率如下:
oracle@lx200 ~/admin/python]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 2.3G 17G 13% /
/dev/sda6 20G 306M 19G 2% /var
/dev/sda3 49G 2.8G 44G 7% /home
/dev/sda5 49G 4.5G 42G 10% /opt
/dev/sda1 194M 12M 172M 7% /boot
tmpfs 18G 0 18G 0% /dev/shm
/dev/mapper/backup-backup_lv
751G 14G 699G 2% /backup
/dev/mapper/data-data_lv
751G 174G 539G 25% /data
执行该脚本后的结果如下:
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 2.3G 17G 13% / ----Caution!!! space usage >= 10
/dev/sda6 20G 306M 19G 2% /var
/dev/sda3 49G 2.8G 44G 7% /home
/dev/sda5 49G 4.5G 42G 10% /opt ----Caution!!! space usage >= 10
/dev/sda1 194M 12M 172M 7% /boot
/dev/mapper/backup-backup_lv
751G 14G 699G 2% /backup
/dev/mapper/data-data_lv
751G 174G 539G 25% /data ----Caution!!! space usage >= 10