python - 将 Nmap 子进程中的数据直接存储到列表中

标签 python list subprocess nmap

我有以下方法可以通过运行 nmap 子进程获取 IP 列表:

import subprocess
import os

def get_hosts(ips, exclude_file, outfile):
    # Run the NMAP for given IPs
    output = subprocess.Popen(['nmap', ips, '-sn', '-T4', '-oG', 'temp.txt', '--excludefile', exclude_file], shell=False, stdout=subprocess.PIPE)
    # Open temp file
    file = open('temp.txt', 'r')
    # Get IP addresses
    ip_list = []
    for line in file:
        a = line.split(" ")
        # Bypass first and last line
        if a[1] == 'Nmap':
            pass
        else:
            ip_list.append(a[1])
    # Close file
    file.close()
    # Remove temp file
    os.remove('temp.txt')
    return ip_list

ips = get_hosts('10.0.0.0/8', 'exclude.txt', 'output.txt')
print (ips)

我希望能够将它们存储在列表中,而不必创建临时文件并将其删除。这可能吗?我对 python 还很陌生,所以任何东西都会有所帮助。

最佳答案

来自nmap文档:

Output

...

Nmap makes output available in five different formats. The default is called interactive output,. and it is sent to standard output (stdout).. There is also normal output,. which is similar to interactive except that it displays less runtime information and warnings since it is expected to be analyzed after the scan completes rather than interactively.

...

While interactive output is the default and has no associated command-line options, the other four format options use the same syntax. They take one argument, which is the filename that results should be stored in.

所以我认为,不要将 nmap 进程的输出保存到 temp.txt 文件中,而是默认将其发送到 stdout ,这样:

>>> output = subprocess.Popen(['nmap', ips, '-sn', '-T4'], shell=False, stdout=subprocess.PIPE)

然后要检查结果,请使用 Popen.communicate :

>>> output.communicate()

Popen.communicate(input=None)

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.

communicate() returns a tuple (stdoutdata, stderrdata).

Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

Note

The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

输出类似于:

>>> output.communicate()
('\nStarting Nmap 6.40 ( http://nmap.org ) at 2015-12-05 00:58 GST\nNmap scan report for 10.0.2.3\nHost is up (0.011s latency).\nNmap scan report for 10.0.2.15\nHost is up (0.000059s latency).\n', None)

这是一个(stdout,stderr)的元组,然后只需取出该元组的第一个元素并通过解析检索您想要的IP,例如使用regex .

关于python - 将 Nmap 子进程中的数据直接存储到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34096593/

相关文章:

python - 与 Pandas 并排绘制的箱线图

python - 使用 Pandas 将连接的字符串拆分为单独的列

r - 将R中不完整的数据帧组合成矩阵

python - 这个 python 列表删除循环有什么问题?

Windows 7 中的 Python 文件资源管理器

Python 子进程语法无效

python - 任何 python 包来加速循环计算?

python - 在 python 中列出,带有引用

html - 当计数器在 10、11、12...之后插入 0 到 9 之间的数字时如何添加 0(零)?有可能吗?

python - "pwd"通过 python 子进程运行时在 Windows 机器上给出路径