简体   繁体   English

使用Python与MAC OSX(Lion)的TUN \\ TAP连接

[英]Interfacing with TUN\TAP for MAC OSX (Lion) using Python

I found the following tun\\tap example program and can not get it to work: 我找到了以下tun \\ tap示例程序,但无法使其工作:

http://www.secdev.org/projects/tuntap_udp/files/tunproxy.py http://www.secdev.org/projects/tuntap_udp/files/tunproxy.py

I have modified the following lines: 我修改了以下几行:

f = os.open("/dev/tun0", os.O_RDWR)
ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "toto%d", TUNMODE))
ifname = ifs[:16].strip("\x00")

The first line was modified to reflect the real location of the driver. 修改了第一行以反映驱动程序的实际位置。 It was originally 它原来是

f = os.open("/dev/net/tun", os.O_RDWR)

Upon running I get the following error: 运行后,我收到以下错误:

 sudo ./tuntap.py -s 9000
 Password:
 Traceback (most recent call last):
   File "./tuntap.py", line 65, in <module>
     ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "toto%d", TUNMODE))
 IOError: [Errno 25] Inappropriate ioctl for device

I am using the latest tun\\tap drivers installed from http://tuntaposx.sourceforge.net/download.xhtml 我正在使用从http://tuntaposx.sourceforge.net/download.xhtml安装的最新tun \\ tap驱动程序

The OSX tun/tap driver seems to work a bit different. OSX tun / tap驱动程序似乎有点不同。 The Linux example dynamically allocates a tun interface, which does not work in OSX, at least not in the same way. Linux示例动态分配一个tun接口,该接口在OSX中不起作用,至少不是以相同的方式。

I stripped the code to create a basic example of how tun can be used on OSX using a self-selected tun device, printing each packet to the console. 我剥离了代码以创建一个基本示例,说明如何使用自选的tun设备在OSX上使用tun,将每个数据包打印到控制台。 I added Scapy as a dependency for pretty printing, but you can replace it by a raw packet dump if you want: 我添加了Scapy作为漂亮打印的依赖项,但如果您需要,可以通过原始数据包转储替换它:

import os, sys
from select import select
from scapy.all import IP

f = os.open("/dev/tun12", os.O_RDWR)
try:
    while 1:
        r = select([f],[],[])[0][0]
        if r == f:
            packet = os.read(f, 4000)
            # print len(packet), packet
            ip = IP(packet)
            ip.show()
except KeyboardInterrupt:
    print "Stopped by user."

You will either have to run this as root, or do a sudo chown your_username /dev/tun12 to be allowed to open the device. 您必须以root用户身份运行它,或者执行sudo chown your_username /dev/tun12以允许打开设备。

To configure it as a point-to-point interface, type: 要将其配置为点对点接口,请键入:

$ sudo ifconfig tun12 10.12.0.2 10.12.0.1

Note that the tun12 interface will only be available while /dev/tun12 is open, ie while the program is running. 请注意,只有在/dev/tun12打开时,即程序运行时, tun12接口才可用。 If you interrupt the program, your tun interface will disappear, and you will need to configure it again next time you run the program. 如果中断程序,tun接口将消失,下次运行程序时需要再次配置它。

If you now ping your endpoint, your packets will be printed to the console: 如果您现在ping您的端点,您的数据包将打印到控制台:

$ ping 10.12.0.1

Ping itself will print request timeouts, because there is no tunnel endpoint responding to your ping requests. Ping本身将打印请求超时,因为没有隧道端点响应您的ping请求。

so about the 'No such file or directory' error when doing: 这样做时:'没有这样的文件或目录'错误:

f = os.open("/dev/tun12", os.O_RDWR)

this worked for me: 这对我有用:

brew install Caskroom/cask/tuntap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM