python 执行shell 事务_python执行shell命令的四种方法

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import os

import platform

import subprocess

import commands

def subproc():

print "系统进程数:"

subprocess.call("ps -ef|wc -l",shell=True)

def os_popen():

print "IP地址:"

os1 = platform.system()

if os1 == "Linux":

print os1

ip1 =os.popen("/sbin/ifconfig eth0|grep 'inet addr'").read().strip().split(":")[1].split()[0]

print "\033[1;32;40m%s\033[0m" % ip1

def os_system():

os_command = 'free -m'

cls_node1 = "命令执行成功...."

cls_node2 = "命令执行失败...."

if os.system(os_command) == 0:

print "\n\033[1;32;40m%s\033[0m" % cls_node1

else:

print "\n\033[1;31;40m%s\033[0m" % cls_node2

def os_commands():

(status, output) = commands.getstatusoutput('pwd')

print status, output

def main():

subproc()

os_popen()

os_system()

os_commands()

if __name__ == "__main__":

main()

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2016-12-28 22:36

浏览 508

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值