首页 > 代码库 > Python读写tap设备

Python读写tap设备

 1 #!/usr/bin/python
 2 
 3 import os
 4 import struct
 5 import fcntl
 6 import binascii
 7 
 8 TUNSETIFF = 0x400454ca
 9 IFF_TAP   = 0x0002
10 
11 fd = os.open("/dev/net/tun", os.O_RDWR)
12 ifs = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", "t%d", IFF_TAP))
13 tname = ifs[:16].strip("\x00")
14 
15 os.system("ip link set %s up" % (tname))
16 os.system("brctl addif br0 %s" % tname)
17 
18 while True :
19     data = http://www.mamicode.com/os.read(fd, 1500)
20     d = struct.unpack(!%ds % len(data), data)[0]
21     print binascii.hexlify(d)

 

Python读写tap设备