【问题标题】:continuous read from subprocess.Popen从 subprocess.Popen 连续读取
【发布时间】:2018-02-24 03:10:41
【问题描述】:

在 Python 中,我有一个关于 subprocess.Popen 函数的问题,我的问题是我无法理解连续读取 stdout 流。当我在函数结束时使用communicate() 时,我得到了我喜欢的输出。但是我这里有两个问题。首先,communicate() 在打印任何内容之前缓冲整个输出,并且获得连续输出会很好。其次,我在communicate() 文档中读到communicate() 不适用于大数据流,在我的场景中就是这种情况。

#!/usr/bin/python

import os
import sys
from subprocess import *
import itertools


def combinate(hash_mode,hash_file,directory):
erg = Popen(['hashcat', '-a', '0', '-m', hash_mode, hash_file, '-O', '--potfile-disable'],
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     universal_newlines=True)
file = []
with os.scandir(directory) as listOfEntries:
    for entry in listOfEntries:
        if entry.is_file() and entry.name is not ".DS_Store":
            file.append(open(directory+entry.name).readlines())
    file = list(itertools.permutations(file))
    for b in range(0, len(file)):
        for i in itertools.product(*file[b]):
            test = '\n'.join(i).replace("\n", "")
            erg.stdin.writelines(test+'\n')

print(erg.communicate()[0])

这是我的交流输出:

Session..........: hashcat
Status...........: Cracked
Hash.Type........: SHA-512
Hash.Target......:          7ba4e9da57a7d3bd8b1b43c0b028a96d77721f6b33e3b85f0b2...298b56
Time.Started.....: Sat Feb 24 03:52:05 2018 (0 secs)
Time.Estimated...: Sat Feb 24 03:52:05 2018 (0 secs)
Guess.Base.......: Pipe
Speed.Dev.#2.....:   969.7 kH/s (0.13ms)
Recovered........: 1/1 (100.00%) Digests, 1/1 (100.00%) Salts
Progress.........: 384
Rejected.........: 0
Restore.Point....: 0
Candidates.#2....: telefon1telefon3telefon2 -> tasse2tasse3tasse1

这是我的输出,带有一个带有 stdout.readline 的 for 循环:

Session..........: hashcat
Status...........: Running
Hash.Type........: SHA-512
Hash.Target......:  7ba4e9da57a7d3bd8b1b43c0b028a96d77721f6b33e3b85f0b2...298b56
Time.Started.....: Sat Feb 24 04:14:30 2018 (10 secs)
Time.Estimated...: Sat Feb 24 04:14:40 2018 (0 secs)
Guess.Base.......: Pipe
Speed.Dev.#2.....:        0 H/s (0.00ms)
Recovered........: 0/1 (0.00%) Digests, 0/1 (0.00%) Salts
Progress.........: 0
Rejected.........: 0
Restore.Point....: 0
Candidates.#2....: [Copying]

如您所见,我得到了一个输出,但是 hashcat 进程没有得到我的标准输入流或没有处理它,我不知道为什么。

如何用我的代码实现连续输出?

【问题讨论】:

  • 我认为您需要将stdout.readline 代码发布为communicate 永远无法满足您的要求,在这里发布此代码是没有用的。顺便说一句,如果你能发布一个可运行的代码,那很好。
  • 是的,这正是我想用我的长描述来表达的。我已经得出结论,communicate() 是不正确的。那是(或多或少)可运行代码,您只需调用 combine() 并为 3 个变量提供函数参数值。前两个参数是 hashcat 的典型参数,所以如果你知道 hashcat,你就知道参数需要什么值。第三个参数只是一个目录的字符串路径,该函数迭代找到的密码字典并将它们相互组合。有不清楚的地方见谅^^
  • 您应该考虑使用线程并让stdoutstdin 在单独的线程中运行,例如在我的回答中:stackoverflow.com/a/48777349/7738328
  • 哇,你不知道我现在有多感激。整个晚上我都尝试了许多线程示例,而您的示例正是我想要的。谢谢

标签: python hashcat


【解决方案1】:

cmets 中 JohanL 的回答是我的解决方案。非常感谢。

您应该考虑使用线程并让 stdout 和 stdin 在单独的线程中运行,例如在我的回答中:[帖子链接][stackoverflow.com/a/48777349/7738328 – JohanL]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2015-01-26
    • 2016-04-16
    • 2019-12-20
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多