在 Python 中捕获 Scapy 函数输出

发布于 2024-12-22 22:27:07 字数 284 浏览 2 评论 0原文

我正在尝试将 scapy 函数(traceroute)的输出捕获到 python 脚本中的字符串。我知道我需要将此函数通过管道传输到 stdout (就像使用 subproces.call() 所做的那样,但不确定如何使用 scapy 执行此操作,有人能够提供任何帮助吗?我是 Python 新手。

下面是相关代码。

#!/usr/bin/env python
from scapy.all import traceroute

traceroute('www.google.com')

I am trying to capture the output of a scapy function (traceroute) to a string in a python script. I understand I need to pipe this function to stdout (as you do with subproces.call() but unsure how to do this using scapy, is anybody able to provide any assistance? I am new to Python.

Relevent code below.

#!/usr/bin/env python
from scapy.all import traceroute

traceroute('www.google.com')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖个坑埋了你 2024-12-29 22:27:07

您还可以像这样调用traceroute:

trace, _ = traceroute("www.example.org", verbose=0)
# trace.get_trace() returns a rather impractical format, so we need
# to convert it. First, we only want the first trace available
hosts = trace.get_trace().values()[0]

# hosts will be in the format { 1: ("1.2.3.4",     False), 
#                               2: ("10.20.30.40", False) ... }
# We convert it to ["1.2.3.4", "10.20.30.40", ...] here:
ips = [hosts[i][0] for i in range(1, len(hosts) + 1)]

之后ips 变量将包含作为跟踪一部分的主机列表。

You can also call traceroute like this:

trace, _ = traceroute("www.example.org", verbose=0)
# trace.get_trace() returns a rather impractical format, so we need
# to convert it. First, we only want the first trace available
hosts = trace.get_trace().values()[0]

# hosts will be in the format { 1: ("1.2.3.4",     False), 
#                               2: ("10.20.30.40", False) ... }
# We convert it to ["1.2.3.4", "10.20.30.40", ...] here:
ips = [hosts[i][0] for i in range(1, len(hosts) + 1)]

After which the ips variable will contain a list of the hosts that are part of the trace.

路还长,别太狂 2024-12-29 22:27:07

您可以通过使用类似文件的对象(例如 StringIO)修补 sys.stdout 来做到这一点:

#!/usr/bin/env python                              
from scapy.all import traceroute
from StringIO import StringIO
import sys

stdout = StringIO()
sys.stdout = stdout
result, unanswered = traceroute('www.google.com')
sys.stdout = sys.__stdout__

print 'Captured stdout:', stdout.getvalue()

无论如何,请注意,您需要的信息可能已经在traceroute 方法返回的对象:

print result.summary()                                      
print unanswered.summary()

注意:您可以在这个 问题

You can do that by patching sys.stdout with a file-like object (for example StringIO):

#!/usr/bin/env python                              
from scapy.all import traceroute
from StringIO import StringIO
import sys

stdout = StringIO()
sys.stdout = stdout
result, unanswered = traceroute('www.google.com')
sys.stdout = sys.__stdout__

print 'Captured stdout:', stdout.getvalue()

Anyway, please note that the information that you need is probably already in the objects returned by the traceroute method:

print result.summary()                                      
print unanswered.summary()

Note: You can find more information about patching the standard output in the answers to this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文