#!/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()
分享到:
相关推荐
今天小编就为大家分享一篇python 执行shell命令并将结果保存的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
主要介绍了使用python执行shell脚本 并动态传参 及subprocess的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
主要介绍了python执行shell脚本创建用户及相关操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
1.在shell脚本执行python脚本时,需要通过python脚本的返回值来判断后面程序要执行的命令 例:有两个py程序 hello.py 代码如下: def main(): print “Hello” if __name__==’__main__’: main() world.py def ...
一、使用python内置commands模块执行shell commands对Python的os.popen()进行了封装,使用SHELL命令字符串作为其参数,返回命令的结果数据以及命令执行的状态; 该命令目前已经废弃,被subprocess所替代; # coding...
python中执行shell命令的几个方法小结.docx
今天小编就为大家分享一篇在python 中实现运行多条shell命令,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
python获取执行shell命令后返回值得几种方式: # 1.os模块 ret = os.popen(supervisorctl status) ret_data = ret.read() # 2.subprocess模块 ret = subprocess.Popen('supervisorctl status',shell=True,stdout=...
主要介绍了python执行使用shell命令方法分享,具有一定参考价值,需要的朋友可以了解下。
最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system(‘cat /proc/cpuinfo’) 但是发现页面上打印的命令执行结果 0或者1,当然不满足需求了。 尝试第二种方案 os...
在window环境,在pycharm中写python脚本,执行shell脚本命令,但因为是window操作系统,语法稍有变化,这是我在做向kafka中发送短信信息时发现的小坑,在window上测试发现的,测试好了后,才改成liniux版本,发布到...
主要介绍了Python执行Linux系统命令的4种方法,即在Python脚本中调用Shell命令,需要的朋友可以参考下
和之前C++执行Linux Bash命令的方法 一样,Python依然支持system调用和popen()函数来执行linux bash命令。 方法一:system调用 #仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 import os os....
GstreamerCodeSnippets, 在C python 和 shell ( gst启动) 中,Gstreamer代码 Fragment GstreamerCodeSnippets描述C 。python 。Rust 和 shell ( gst启动) 中的Gstreamer代码 Fragment 。其他源 GStreamer文档语言:...
于是乎想到用python执行shell获取这些信息,python执行shell脚本有以下三种方法: 1. os.system() 复制代码 代码如下:os.system(‘ls’) #返回结果0或者1,不能得到命令的输出 2. os.popen() 复制...
用pandas读取excel数据,并把数据分组保存在txt文件里,自动生成shell脚本,把window的格式转换为unix格式可直接拖到linux执行shell脚本
在用pyinstaller打包后不想要后面的终端命令框,但是打包时加了-w或者–noconsole命令后会导致cmd程序不能运行从而出错。这个时候用subprocess可以解决该类问题。...以上这篇python隐藏终端执行cmd命令的方法就