在 Python 中捕获 Scapy 函数输出
我正在尝试将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以像这样调用traceroute:
之后
ips
变量将包含作为跟踪一部分的主机列表。You can also call traceroute like this:
After which the
ips
variable will contain a list of the hosts that are part of the trace.您可以通过使用类似文件的对象(例如
StringIO
)修补 sys.stdout 来做到这一点:无论如何,请注意,您需要的信息可能已经在
traceroute
方法返回的对象:注意:您可以在这个 问题。
You can do that by patching
sys.stdout
with a file-like object (for exampleStringIO
):Anyway, please note that the information that you need is probably already in the objects returned by the
traceroute
method:Note: You can find more information about patching the standard output in the answers to this question.