def main(): # Configure pytun's logger pytun.logger.setLevel(logging.DEBUG) logging.basicConfig() # Open the tunnel try: tun = pytun.open() except pytun.Tunnel.NotPermitted: print print "*" * 80 print "You do have the rights to access the file %s." % ( pytun.TUN_KO_PATH, ) print "Give the access of this file to pytun, or if you trust me," print "elevate this current script to root level." print "*" * 80 print raise print "*" * 80 print print "OK. The tunnel '%s' had been created." % (tun.name, ) print print "If you want to play with it, first configure it." print print "1. Set up the network and set an IP" print " $ ifconfig %s 192.168.42.1" % (tun.name, ) print print "2. Add the network route" print " $ route add -net 192.168.42.0/24 dev %s" % (tun.name, ) print print "Then, try to ping some IP in this network ..." print " $ ping 192.168.42.42" print print "Or do some UDP netcat magic." print " $ nc 192.168.42.42 4242 -u" print print "Enjoy !" print print "*" * 80 try: # Receive loop while True: buf = tun.recv() pytun.logger.info("Packet received !") pprint_buf(buf) print except KeyboardInterrupt: print "Keyboard interrupt. Closing." finally: # Close the tunnel tun.close()
def main(): # Configure pytun's logger pytun.logger.setLevel(logging.DEBUG) logging.basicConfig() # Open the tunnel try: tun = pytun.open() except pytun.Tunnel.NotPermitted: print print "*" * 80 print "You do have the rights to access the file %s." % (pytun.TUN_KO_PATH, ) print "Give the access of this file to pytun, or if you trust me," print "elevate this current script to root level." print "*" * 80 print raise print "*" * 80 print print "OK. The tunnel '%s' had been created." % (tun.name, ) print print "If you want to play with it, first configure it." print print "1. Set up the network and set an IP" print " $ ifconfig %s 192.168.42.1" % (tun.name, ) print print "2. Add the network route" print " $ route add -net 192.168.42.0/24 dev %s" % (tun.name, ) print print "Then, try to ping some IP in this network ..." print " $ ping 192.168.42.42" print print "Or do some UDP netcat magic." print " $ nc 192.168.42.42 4242 -u" print print "Enjoy !" print print "*" * 80 try: # Receive loop while True: buf = tun.recv() pytun.logger.info("Packet received !") pprint_buf(buf) print except KeyboardInterrupt: print "Keyboard interrupt. Closing." finally: # Close the tunnel tun.close()
def builder(name, tun=None): """ Class meta-builder. Returns a class object, not a class instance ! """ tun = tun if tun is not None else pytun.open() cls = type(name, (TunnelWebSocketHandler,), {"tun": tun, "ioloop_state": False, "clients": set()}) return cls
def __init__(self, mode=None): self._logger = logging.getLogger(self.__module__) self._logger.debug('Tunnel class created') self._threadEvent = Event() self._handler = None self._stopsocket1, self._stopsocket2 = socket.socketpair() if mode is None or mode.lower() == 'tun': tunmode = 'tun' elif mode.lower() == 'tap': tunmode = 'tap' else: self._logger.critical('Tunnel Mode %s is not supported', mode) raise Exception("Tunnel mode not supported") self._tun = pytun.open(tunmode) # TODO Set up addressing os.system('ip link set %s up' % self._tun.name) os.system('ip address add 192.168.40.1/32 dev %s' % self._tun.name) os.system('ip route add 192.168.40.2/32 dev %s' % self._tun.name)
#!/usr/bin/env python # encoding: utf-8 import eventlet import pytun import os import sys eventlet.monkey_patch(all=True) tap = pytun.open('tap') os.system("ip link set %s up" % tap.name) os.system("ip link set dev %s mtu 520" % tap.name) os.system("ip addr add 192.167.100.1/24 dev %s" % tap.name) def handlenet(sock): while True: try: x = sock.recv(520) tap.send(x) except Exception, e: print(e) break def handletap(): net = None while True: msg = tap.recv() try: