设为首页 收藏本站
查看: 1378|回复: 0

[经验分享] python之commands模块(执行Linux Shell命令)

[复制链接]

尚未签到

发表于 2018-8-8 12:14:26 | 显示全部楼层 |阅读模式
  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) >= threshold:
  chkDiskList += '    ----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) >= 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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-548657-1-1.html 上篇帖子: python中socket与socketserver模块简单入门使用 下篇帖子: centos6.5下安装OpenCV+Python支持
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表