使用 python 将数据包发送到 Tap 接口
我一直在尝试使用 python 将数据包发送到 Tap 接口。我正在使用wireshark 监视tap 接口,但没有收到任何数据包。我这样做主要是为了帮助我理解 VPN、以太网桥接和 Python 中的套接字编程。
我的系统设置如下:
Ubuntu Desktop 11.10
Python 2.7
eth0 ip: 192.168.1.6
tap0 ip: 10.0.0.1
我首先按如下方式设置 Tap:
sudo openvpn --mktun --dev tap0
sudo ifconfig tap0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 promisc up
这将启动 Tap0 接口并通过 Tap0 创建到 10.0.0.1/24 的内核路由规则。
这是路由表:
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 * 255.255.255.0 U 0 0 0 tap0
192.168.1.6 * 255.255.255.0 U 1 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
接下来,我启动 python Interactive 并创建一个简单的 UDP 套接字。
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto('helloworld',('10.0.0.2',12345))
我在 tap0 上运行带有 Wireshark 监控的 sendto 命令。现在,我的网络上没有 10.0.0.2 上的主机,但我至少应该在 tap0 接口上看到一些传出流量。我已经在 Windows 中复制了这个,似乎工作正常。
我只能认为问题出在linux下tap0接口的设置上。那还是我对这件事的平庸理解。
谢谢
I've been trying to send packets to a tap interface using python. I'm monitoring the tap interface using wireshark and no packets are being received. I'm doing this as an exercise mainly to help my understanding of vpns, ethernet bridging and socket programming in python.
My System Setup is as follows:
Ubuntu Desktop 11.10
Python 2.7
eth0 ip: 192.168.1.6
tap0 ip: 10.0.0.1
I first setup the tap as follows:
sudo openvpn --mktun --dev tap0
sudo ifconfig tap0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 promisc up
This starts the tap0 interface and creates a kernel routing rule to 10.0.0.1/24 via tap0.
Here is the route table:
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 * 255.255.255.0 U 0 0 0 tap0
192.168.1.6 * 255.255.255.0 U 1 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
Next I start python interactive and create a simple UDP socket.
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto('helloworld',('10.0.0.2',12345))
I run the sendto command with Wireshark monitoring on tap0. Now, there is no host at 10.0.0.2 on my network, but I should at least see some outgoing traffic on the tap0 interface. I have replicated this in windows and it seems to work ok.
I can only think that the problem lies somewhere in the setup of the tap0 interface under linux. That or my mediocre understand of this stuff.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ethertap 接口上,如果 ARP 缓存仍然具有否定(不完整)条目,您实际上可能看不到看到传出流量。否则,您可能只看到 ARP 数据包。
实际上,我认为这与 python 无关。
您是否尝试过 ping - 您是否看到 icmp 数据包出去? arp表是什么样的?
On an ethertap interface, you probably will actually not see outgoing traffic, if the ARP cache still has the negative (incomplete) entry. Otherwise, you probably seen an ARP packet only.
I don't think this is related to python, actually.
Did you try a ping - do you see an icmp packet go out? What does the arp table look like?