python stderr,stdout写入变量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python stderr,stdout写入变量相关的知识,希望对你有一定的参考价值。

#!/bin/bash

#echo "hello"

# assign filename to variable
err_file_var=err_file.tmp

# check if the file already exists
# if yes, delete; if no create
if [ -e $err_file_var ]; then
        echo "file here.."
        ls -la $err_file_var
        echo "removing file"
        rm $err_file_var
else
        echo "file not here.."
        echo "creating file..."
        touch $err_file_var
        ls -la $err_file_var
fi


################################################
# #writeHello.sh generates STDOUT and STDERR
# kerem@tilki:~/bash_scripts$ cat writeHello.sh
# #!/bin/bash
# echo "hello"
# cp noFile
################################################

out=$(./writeHello.sh 2>$err_file_var)
err=$(< $err_file_var)

echo "STDOUT : "
echo $out
echo "---------"
echo "STDERR : "
echo $err
#!/usr/bin/python
import os
#from subprocess import Popen, PIPE
print '<<<_kerem_python_info>>>'


def get_health_info(os_environ, sid_liste, info_string):
        print info_string
        os.environ['SYMCLI_CONNECT'] = os_environ
        if len(sid_liste) == 1:
                sid = sid_liste[0]
                command = "/usr/symapi/bin/symcfg -sid %s list -env_data" % sid
                text = os.popen(command).readlines()
                return text

        elif len(sid_liste) > 1:
                for sid in sid_liste:
                        command = "/usr/symapi/bin/symcfg -sid %s list -env_data" % sid
                        text = os.popen(command).readlines()



#local systems
#os.system("unset SYMCLI_CONNECT")
#local_sid_list = ["2199","1025","1047","1472","1474"]


#HANNOVER
print '<<<Symetrix_Health_Hannover>>>'
os.environ['SYMCLI_CONNECT'] = "SYM_H"
SID_Hannover = "2202"
command = "/usr/symapi/bin/symcfg -sid %s list -env_data" % SID_Hannover

text = os.popen(command).readlines()





count = 0
error = 0

if text:
        for line in text:
                line = line.lower()
                if "fail" in line or "degraded" in line:
                        error = error +1
                elif "normal" in line:
                        count = count +1

sym_name = text[1].strip()
sym_check_date = text[2].strip()

print sym_name
print sym_check_date
if error == 0:
        print "NO_ERRORS_FOUND"
        print "Normal counters %i" % count

else:
        print "%i errors found!!!" % error
from subprocess import Popen, PIPE

proc = Popen(['./test.sh'],stdout=PIPE, stderr=PIPE)

stdout, stderr = proc.communicate()
print "<<<fdsfsfs>>>"
print "error msg is %s" % stderr.strip()
print "output msg is %s" % stdout.strip()
#!/usr/bin/python
# chmod +x <fileName>
# ./<fileName>
# python interpreter no more needed 
# for checkmk plugins necessary


import os
print '<<<_kerem_python_info>>>'

#local systems
#os.system("unset SYMCLI_CONNECT")
#local_sid_list = ["2199","1025","1047","1472","1474"]

print '<<<Symetrix_Health_Hannover>>>'
#HANNOVER

############
# os.environ to set the environments variable, 
# os.system(export SYMCLI_CONNECt=XYZ) does not work.
############
os.environ['SYMCLI_CONNECT'] = "SYM_H"
SID_Hannover = "2202"
command = "/usr/symapi/bin/symcfg -sid %s list -env_data" % SID_Hannover

text = os.popen(command).readlines()

以上是关于python stderr,stdout写入变量的主要内容,如果未能解决你的问题,请参考以下文章

输出重定向是不是按顺序写入 stdout 和 stderr 信息?

为啥“git submodule add ...”写入stderr而不是stdout?

Subprocess.Popen:将 stdout 和 stderr 克隆到终端和变量

如何在 Perl 中读取和写入大缓冲区到进程 stdin/stdout/stderr?

捕获程序 stdout 和 stderr 以分隔变量

使用python sh模块,如何保存组合的stdout和stderr?