在 Python 环境中使用 Scapy 发送数据包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17971398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending packets with Scapy within Python environment
提问by Evan Clark
I am playing around with Scapy and I want to use it within a Python script but sending packets seem to be a problem. Here is my code.
我正在玩 Scapy,我想在 Python 脚本中使用它,但发送数据包似乎是一个问题。这是我的代码。
Scapy Shell:
Scapy壳:
send(IP(src="10.0.99.100",dst="10.1.99.100")/ICMP()/"Hello World")
This works fine and sends the packet.
这工作正常并发送数据包。
Python script:
蟒蛇脚本:
#! /usr/bin/env python
from scapy.all import sr1,IP,ICMP
p=sr1(IP(src="10.0.99.100",dst="10.1.99.100")/ICMP()/"Hello World")
This runs fine but when it tries to send the packet I get:
这运行良好,但是当它尝试发送数据包时,我得到:
WARNING: No route found for IPv6 destination :: (no default route?)
Begin emission:
.Finished to send 1 packets.
....^C
Received 5 packets, got 0 answers, remaining 1 packets
回答by RyPeck
When you run this in the Python environment you are using the sr1
function. The sr1
function will send a packet and then wait for an answer, keeping a count of received packets. See more here -
当您在 Python 环境中运行它时,您正在使用该sr1
函数。该sr1
函数将发送一个数据包,然后等待应答,保持接收数据包的计数。在这里查看更多 -
http://www.secdev.org/projects/scapy/doc/usage.html#send-and-receive-packets-sr
http://www.secdev.org/projects/scapy/doc/usage.html#send-and-receive-packets-sr
To get the behavior you desire, you need to use the send
function, just like you did when using the Scapy shell.
要获得您想要的行为,您需要使用该send
函数,就像您在使用 Scapy shell 时所做的那样。
#! /usr/bin/env python
from scapy.all import send, IP, ICMP
send(IP(src="10.0.99.100",dst="10.1.99.100")/ICMP()/"Hello World")