Python 与 NMap 通信() [英] Python communicate() with NMap

查看:30
本文介绍了Python 与 NMap 通信()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

#!/usr/bin/python

## test communicate()

# Import the module
import subprocess

# Ask the user for input
host = raw_input("Enter a host to map: ")   

# Set up the echo command and direct the output to a pipe
p1 = subprocess.Popen(['nmap', '-T0', '-F', host], stdout=subprocess.PIPE)

# Run the command
output = p1.communicate()[0]

print output

当我进入主机时,它没有给我任何输出,我可以看到多个 nmap 实例在进程中使用不同的 PID 运行,因此它实际执行命令.

When I enter the host it doesn't give me any output and I can see multiple instances of nmap running with different PIDs in processes so it actually executes command.

当我 Z^ 它说: [n+1]+ Stopped ./sample.py 所以 nmap 实际上运行了 n+1 次而不打印任何输出.

When I Z^ it says: [n+1]+ Stopped ./sample.py So nmap is actually running n+1 times without printing any output.

它与 ping 和 traceroute 完美配合,如下所示:

It works perfectly well with ping and traceroute like this:

# Set up the echo command and direct the output to a pipe
p1 = subprocess.Popen(['ping', '-c 2', host], stdout=subprocess.PIPE)
p1 = subprocess.Popen(['traceroute', host], stdout=subprocess.PIPE)

它也可以在没有 nmap [options] 的情况下工作,如 nmap google.com

It also works without nmap [options] like nmap google.com

p1 = subprocess.Popen(['nmap', host], stdout=subprocess.PIPE)

我的问题:

它与Python或NMap有关吗?这段代码有什么问题还是我遗漏了什么?

Is it related to Python or NMap? Is there anything wrong with this code or am I missing something?

推荐答案

来自文档:

T0 的主要作用是序列化扫描,因此一次只扫描一个端口,并在发送每个探测之间等待五分钟.

The main effects of T0 are serializing the scan so only one port is scanned at a time, and waiting five minutes between sending each probe.

-F 参数表示仅扫描 100 个端口",但在两次探测之间的 5 分钟内,至少需要 8 小时 20 分钟,仅针对端口扫描阶段.这是假设这些探测中没有一个被丢弃和重新传输,并且目标对所有这些探测做出响应.

The -F argument means "scan only 100 ports," but at five minutes between probes, that's a minimum of 8 hours and 20 minutes, just for the port scan phase. That's assuming that none of those probes gets dropped and retransmitted, and that the target responds to all of them.

在绝大多数情况下,-T3(默认)就可以了.凭借快速连接而不是大量目标,-T4 甚至是可靠的.除非您的目标正在主动检测和阻止扫描,否则 -T2 是您需要的最慢的.

In the vast majority of cases, -T3 (the default) is just fine. With a fast connection and not a ton of targets, -T4 is even reliable. Unless your target is actively detecting and blocking scans, -T2 is the slowest you'll ever need to go.

这篇关于Python 与 NMap 通信()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆