大部分的程序是这样的:
from subprocess import Popen,PIPE,STDOUT
p = Popen(cmd,stdout=PIPE,stderr=STDOUT,shell=True)
while True:
print(p.stdout.readline())
if not line:
break
但是由于子程序没有进行 flush 的话,会把结果缓存到系统中。导致程序运行完成,上面的程序才会进行打出(会一直卡在readline这个函数)。
解决方法:
p = subprocess.Popen(cmd,stdout=subprocess.PIPE,bufsize=1)
for line in iter(p.stdout.readline,b''):
print line,p.stdout.close()
p.wait()
实际弱口令我是这样写的
import subprocess #Popen
proc = subprocess.Popen(medusaCMD,stderr=subprocess.STDOUT,shell=True)
for line in iter(proc.stdout.readline,'b'):
print line
if not subprocess.Popen.poll(proc) is None:
if line == "":
break
proc.stdout.close()
记小的写法
proc = subprocess.Popen(cmd,shell=True,stderr=subprocess.STDOUT)
try:
while True:
buff = proc.stdout.readline()
print(buff)
if buff == '' and proc.poll() != None:
break
else:
.....
except Exception:
data["status"] = -1
finally:
return data
单次管道输出写法
方法一
# -*- coding: UTF-8 -*-
import re
import sys
import subprocess
from subprocess import Popen,STDOUT
#docker_info = {"CONTAINER ID":"","NAME":"","cpu %":"","MEM USAGE / LIMIT":"",\
# "MEM %":"","NET I/O":"","BLOCK I/O":"","PIDS":""}
docker_list = []
cmd = "docker stats -a --no-stream"
proc = subprocess.Popen(cmd,stderr=subprocess.STDOUT)
try:
buff = proc.communicate()
stritem = buff[0]
str_list = re.split(r' +|\n',stritem)
for i in range(8,len(str_list)-1):
if i % 8 == 0:
value = 0
docker_info = {}
docker_info["CONTAINER ID"] = str_list[i]
else:
value += 1
if value == 1:
docker_info["NAME"] = str_list[i]
elif value == 2:
docker_info["cpu %"] = str_list[i]
elif value == 3:
docker_info["MEM USAGE / LIMIT"] = str_list[i]
elif value == 4:
docker_info["MEM %"] = str_list[i]
elif value == 5:
docker_info["NET I/O"] = str_list[i]
elif value == 6:
docker_info["BLOCK I/O"] = str_list[i]
elif value == 7:
docker_info["PIDS"] = str_list[i]
docker_list.append(docker_info)
value = 0
print docker_list
except Exception as e:
print "error",e
sys.exit(1)
proc.stdout.close()
方法二(待测试)
import subprocess
from multiprocessing.dummy import Pool as ThreadPool
command = poc + ' -t ' + ip + ' -p ' + port
result = subprocess.getoutput(command)
if 'WARNING: SERVER IS VULNERABLE' in result:
result = AAAAA
else:
result = BBBBBB
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。