建立 < a href=> "http://www.kernel.org/doc/Documentation/networking/tuntap.txt" rel=“nofollow” >tap 设备 并为此写入框架如何? 然后你可以用 tap
设备闻闻 设备,就像任何其他设备一样。在 Python < a href="https://gist.github.com/585369" 中,有一个使用
NB:我已经测试过这个,但这个想法似乎合理...
UPDATE: This seems to work. It s based on the above gist, but
simply reads frame data from a file and writes it to the device:
import sys
import fcntl
import os
import struct
import subprocess
TUNSETIFF = 0x400454ca
TUNSETOWNER = TUNSETIFF + 2
IFF_TUN = 0x0001
IFF_TAP = 0x0002
IFF_NO_PI = 0x1000
# Open TUN device file.
tun = open( /dev/net/tun , r+b )
# Tell it we want a TUN device named lars0.
ifr = struct.pack( 16sH , lars0 , IFF_TAP | IFF_NO_PI)
fcntl.ioctl(tun, TUNSETIFF, ifr)
# Optionally, we want it be accessed by the normal user.
fcntl.ioctl(tun, TUNSETOWNER, 1000)
# Bring it up and assign addresses.
subprocess.check_call([ ifconfig , lars0 , up ])
print waiting
sys.stdin.readline()
# Read an IP packet been sent to this TUN device.
packet = list(open( /tmp/packet.raw ).read())
# Write the reply packet into TUN device.
os.write(tun.fileno(), .join(packet))
print waiting
sys.stdin.readline()