前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python之commands模块(执行

python之commands模块(执行

作者头像
py3study
发布2020-01-09 16:22:12
1.5K0
发布2020-01-09 16:22:12
举报
文章被收录于专栏:python3

commands模块

用于执行Linux shell命令,要获得shell命令的输出只需要在后面参数写入('命令')就可以了。

需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果。

看一下三个函数: 1). commands.getstatusoutput(命令)

执行shell命令, 返回两个元素的元组tuple(status, result),status为int类型,result为string类型。

cmd命令的执行方式是{ cmd ; } 2>&1, 故返回结果包含标准输出和标准错误.

>>> import commands

>>> commands.getstatusoutput('ls /bin/ls')

(0, '/bin/ls')

>>> commands.getstatusoutput('pwd')  

(0, '/home/test')  

>>> commands.getstatusoutput('cat /bin/junk')

(256, 'cat: /bin/junk: No such file or directory')

>>> commands.getstatusoutput('/bin/junk')

(256, 'sh: /bin/junk: not found')

2). commands.getoutput(cmd) 只返回执行的结果, 忽略返回值.

>>> commands.getoutput('ls /bin/ls')

'/bin/ls'

3). commands.getstatus(file) #现已被弃用 返回ls -ld file执行的结果.

>>> commands.getstatus('/bin/ls')    #该函数已被python丢弃,不建议使用,它返回 ls -ld file 的结果(String)(返回结果太奇怪了,难怪被丢弃)

'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

例 1 :

获取系统最大文件描述符

#!/usr/bin/python 

import os,sys,commands 

_open_file=65533 

try: 

    getulimit=commands.getstatusoutput('source /etc/profile;ulimit -n')

except Exception,e:

    pass

if getulimit[0]==0:

    host_open_file=int(getulimit[1])

if host_open_file = _open_file:

    print "max_open_file is ok"

例 2 :

下面的一个脚本利用commands模块检测磁盘使用率,标识出大于10%的磁盘(百分比可根据实际情况调整,一般设为90%,本例为了更好的说明情况,设为10%):

#!/usr/bin/python

import commands  

threshold = 10  

flag = False  

title=commands.getoutput("df -h|head -1")  

'''

Check sda disk space usage like below format

'''  

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: 

'''        

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  

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档