PyTun-PMD3 (IPv6-ONLY TUN device)

3.0.3 · active · verified Thu Apr 16

pytun-pmd3 is a fork of the original python-pytun library, providing support for TUN/TAP network devices on Darwin (macOS), Windows, and Linux. It exclusively supports IPv6, making it suitable for applications like VPNs that specifically require IPv6 tunneling. The library is currently at version 3.0.3 and has a moderate release cadence, focusing on bug fixes and platform compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an IPv6-only TUN device, bring it up, and configure its IPv6 address. It then enters a brief loop to show packet reception. Remember to run with appropriate permissions.

import os
import sys
import time
from pytun import TunTapDevice, IFF_TUN, IFF_NO_PI

# Note: Requires appropriate permissions (e.g., run as root/administrator
# or user added to 'tun' group on Linux). On Windows, the wintun driver
# might need manual installation first, though pytun-pmd3 attempts to manage it.

try:
    # Use a unique name to avoid conflicts, or a fixed one for specific setup
    device_name = os.environ.get('PYTUN_DEV_NAME', 'pytun-pmd3-tun0')
    tun = TunTapDevice(name=device_name, flags=IFF_TUN | IFF_NO_PI)
    print(f"Created TUN device: {tun.name}")

    tun.up()
    print(f"Device {tun.name} is up.")

    # Configure IPv6 address (pytun-pmd3 is IPv6-ONLY)
    ipv6_address = os.environ.get('PYTUN_IPV6', 'fcd0::1/64')
    tun.ifconfig(ipv6=ipv6_address)
    print(f"Configured IPv6 address {ipv6_address} on {tun.name}")

    print(f"\nDevice {tun.name} configured. You can now try pinging its address.")
    print("Example: ping fcd0::1 (from another host/interface that can route to it)")
    print("Waiting for 10 seconds. Press Ctrl+C to stop.")

    start_time = time.time()
    while time.time() - start_time < 10:
        try:
            # Read up to 1500 bytes with a 1-second timeout
            packet = tun.read(1500, timeout=1)
            if packet:
                print(f"Received {len(packet)} bytes from TUN device.")
            sys.stdout.flush()
        except Exception as e:
            # Ignore common 'Resource temporarily unavailable' when no packets
            pass
        time.sleep(0.1)

except PermissionError:
    print("Error: Permission denied. Try running as root/administrator or")
    print("ensure your user has appropriate permissions (e.g., added to 'tun' group).")
    print("On Linux: sudo usermod -a -G tun $(whoami) && sudo reboot")
except FileNotFoundError as e:
    print(f"Error: File not found: {e}")
    print("On Windows, ensure the wintun driver is correctly installed and accessible.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    if 'tun' in locals():
        try:
            print(f"Closing TUN device: {tun.name}")
            tun.close()
            print("Device closed.")
        except Exception as e:
            print(f"Error closing TUN device: {e}")

view raw JSON →