python执行shell实时输出,并返回code

import subprocess

def run_shell(shell):
    cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=subprocess.PIPE,
                           stdout=subprocess.PIPE, universal_newlines=True, shell=True, bufsize=1)
    # 实时输出
    while True:
        line = cmd.stdout.readline()
        print(line, end='')
        if subprocess.Popen.poll(cmd) == 0:  # 判断子进程是否结束
            break

    return cmd.returncode

if __name__ == '__main__':
    print(run_shell("ping www.baidu.com"))

原文地址:https://www.cnblogs.com/chenqionghe/p/11532245.html

时间: 2024-08-01 03:46:05

python执行shell实时输出,并返回code的相关文章

python执行shell获取硬件参数写入mysql

最近要获取服务器各种参数,包括cpu.内存.磁盘.型号等信息.试用了Hyperic HQ.Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy. 于是乎想到用python执行shell获取这些信息,python执行shell脚本有以下三种方法: 1. os.system() os.system('ls')#返回结果0或者1,不能得到命令的输出 2. os.popen() output = os.popen('ls') print output.read()#打印出的

python执行shell命令

python执行shell命令 #!/usr/bin/python2.7 #coding=utf-8 import shlex import datetime import subprocess import time def execute_command(cmdstring, cwd=None, timeout=None, shell=False): # 执行一个SHELL命令 ## 封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr ## 参数:

python 执行shell 命令,自动化添加攻击IP地址到iptables

通过python执行shell命令的方法有4种,在这里介绍一种常用的. os.system. os.popen. commands. subprocess 接下来介绍subprocess的使用 通过python 日志分析,获取到攻击源IP地址,收集写入到mysql数据库中 mysql如下: iptables.py 脚本 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import MySQLdb import subprocess t

Python 执行Shell 外部命令

1.os.system()此方法执行的外部程序,会将结果直接输出到标准输出.os.system的返回结果为执行shell 的 $? 值.因此请执行没有输出结果的程序时适合使用此方法.如touch .rm 一个文件等. In [1]: import os In [2]: os.system('touch test.txt') Out[2]: 0 In [3]: os.system('rm -rf test.txt') Out[3]: 0 2.os.popen()此方法结合了os.system和 文

python执行shell指令的几种方法

1.os.system() 优点:简单,linux&widnows等平台均可用,只需要判断返回结果是0还是1即可判断是否执行成功. 缺点:无法获取返回输出. 例子: os.system('ls') 2.os.popen() 优点:可获取输出结果 缺点:无法获取执行结果,需要根据输出结果做判断处理 例子: output = os.popen('ls') print output.read() 3.commands.getstatusoutput() 优点:可同时获取执行结果及返回结果 缺点:win

python执行系统命令后获取返回值

import os, subprocess # os.system('dir') #执行系统命令,没有获取返回值,windows下中文乱码 # result = os.popen('dir') #执行系统命令,返回值为result# res = result.read()# for line in res.splitlines():# print(line ) #用subprocess库获取返回值.# p = subprocess.Popen('dir', shell=True, stdout=

C++/Php/Python 语言执行shell命令

编程中经常需要在程序中使用shell命令来简化程序,这里记录一下. 1. C++ 执行shell命令 1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 5 int exec_cmd(std::string cmd, std::string &res){ 6 if (cmd.size() == 0){ //cmd is empty 7 return -1; 8 } 9 10 char

shell脚本中执行python脚本并接收其返回值的例子

1.在shell脚本执行python脚本时,需要通过python脚本的返回值来判断后面程序要执行的命令 例:有两个py程序  hello.py 复制代码代码如下: def main():    print "Hello" if __name__=='__main__':    main()world.py def main():    print "Hello" if __name__=='__main__':    main() shell 脚本 test.sh

[蟒蛇菜谱] Python调用shell命令,获取返回值和返回信息

# -*- coding: utf-8 -*- import os import subprocess import signal class MockLogger(object): '''模拟日志类.方便单元测试.''' def __init__(self): self.info = self.error = self.critical = self.debug def debug(self, msg): print "__LOGGER__:"+msg class Shell(obj