import struct import sys import os import time from fcntl import ioctl IFF_TUN = 0x0001 # tunnel IP packets IFF_TAP = 0x0002 # tunnel ethernet frames IFF_NO_PI = 0x1000 # don't pass extra packet info tun = os.open("/dev/net/tun", os.O_RDWR) ifs = ioctl(tun, 0x400454ca, struct.pack("16sH", "tun%d", IFF_TUN | IFF_NO_PI)) ifname = ifs[:16].strip("\x00") os.system("ifconfig %s 192.168.1.1" % (ifname,)) os.system("route add -net 192.168.1.0 netmask 255.255.255.0 dev %s" % (ifname,)) while 1: print os.read(tun, 8192)