https://www.cnblogs.com/wjoyxt/p/5087951.html

要获得shell命令的输出只需要`cmd`命令就可以了,

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

看一下三个函数:
1). commands.getstatusoutput(cmd)
用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result),其中 status为int类型,result为string类型。cmd执行的方式是{ cmd ; } 2>&1, 这样返回结果里面就会包含标准输出和标准错误.

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

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

看一下这些函数使用的例子:

>>> import commands

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

(0, '/bin/ls')

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

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

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

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

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

'/bin/ls'

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

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

#!/usr/bin/python
#coding:utf-8
import os,sys,commands def openfile():
grains = {}
_open_file=65533
try:
getulimit=commands.getstatusoutput('source /etc/profile;ulimit -n')
except Exception,e:
pass
if getulimit[0]==0:
_open_file=int(getulimit[1])
grains['max_open_file'] = _open_file
return grains

commands模块【转】的更多相关文章

  1. python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...

  2. python中的commands模块

    commands模块用于调用shell命令 有3中方法: commands.getstatus()   返回执行状态 commands.getoutput()   返回执行结果 commands.ge ...

  3. python标准库介绍——34 commands 模块详解

    ==commands 模块== (只用于 Unix) ``commands`` 模块包含一些用于执行外部命令的函数. [Example 3-7 #eg-3-7] 展示了这个模块. ====Exampl ...

  4. Python的logging模块、os模块、commands模块与sys模块

    一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...

  5. [Python2.x] 利用commands模块执行Linux shell命令

    用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...

  6. python commands 模块

    commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...

  7. python 基础 7.5 commands 模块

    一. commands 模块   1.commands 模块只使用与linxu 的shell 模式下 在我们平时码字时,经常需要调用系统脚本或者系统命令来解决很多问题,接下来,我们就介绍给大家一个很好 ...

  8. 利用commands模块执行shell命令

    利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...

  9. python commands模块在python3.x被subprocess取代

    subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 ...

  10. python之commands模块

    要获得shell命令的输出只需要`cmd`命令就可以了, 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果.看一下三个函数:1). comm ...

随机推荐

  1. Ubuntu安装php7.0环境

    1.下载必须组件 sudo apt-get install libxml2-dev sudo apt-get install curl 参考文献:http://php.net/manual/zh/in ...

  2. pve 导入 ova

    匆忙记录 Proxmox includes qm importdisk as command. Extract your ova: tar -xvf *.ova Create a new VM wit ...

  3. Dockers安装nginx

    方法一.通过 Dockerfile构建 创建Dockerfile 首先,创建目录nginx,用于存放后面的相关东西. runoob@runoob:~$ mkdir -p ~/nginx/www ~/n ...

  4. 膜态沸腾UDF【转载】

    膜态沸腾的UDF,添加注释.其中获取VOF梯度的方法详见前面的日志,其中很多宏无法通过UDF手册查阅, 蒸汽相中的质量源项的一般形式为: 式中: 通过一阶近似,热流之差可表达为: 式中: 通过此近似, ...

  5. PostgreSQL定时自动备份

    PostgreSQL定时自动备份 简介 PostgreSQL数据库中未提供数据库的定时备份功能,所以需要结合备份和定时job功能来共同实现. 这里我选取了2种定时job方式,crontab是Linux ...

  6. Windowless

    参考: 浏览器插件(Plugins)开发 - II http://wen-bing.github.io/blog/2013-04-24/NPAPI-plugin-intro-2/ 无窗口型(Windo ...

  7. hdfs 通过命令坏块监测和删除或者地址获取参数做监控

    [root@nodecm.oldboy.org.cn /data/dfs/nn/current]# hdfs fsck -list-corruptfileblocksConnecting to nam ...

  8. Java基础 return 退出main方法的示例

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  9. Mac 终端显示git分支

    1 进入你的home目录 cd ~ 2 编辑.bashrc文件 vi .bashrc 3 将下面的代码加入到文件的最后处 function git_branch { branch="`git ...

  10. 003-结构型-05-桥接模式(Bridge)

    一.概述 将抽象部分与它的具体实现部分分离.使它们都可以独立地变化.通过组合的方式建立两个类之间联系,而不是继承. Bridge 模式又叫做桥接模式,是构造型的设计模式之一.Bridge模式基于类的最 ...