原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://koumm.blog.51cto.com/703525/1438687
整理:python执行shell命令四法,示例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#!/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()
|
本文出自 “koumm的linux技术博客” 博客,请务必保留此出处http://koumm.blog.51cto.com/703525/1438687