PyRad RADIUS Library

2.5.4 · active · verified Wed Apr 15

PyRad is a Python library for implementing RADIUS (Remote Authentication Dial-In User Service) clients and servers. It simplifies handling RADIUS packets, attributes, and dictionaries. The current version is 2.5.4, and the project maintains an active release cadence with minor updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic RADIUS client using `pyrad` to send an `Access-Request` packet and process the server's reply. It includes robust dictionary loading and environment variable usage for sensitive information like server address and secret.

import os
from pyrad.client import RadiusClient
from pyrad.packet import AccessRequest
from pyrad.dictionary import Dictionary, getDictionaryPaths

# Pyrad relies on RADIUS dictionaries for attribute definitions.
# It attempts to find default dictionaries (e.g., in /usr/share/pyrad/dictionaries).
# For robustness, specify a dictionary path or ensure defaults are installed.
# In a real app, you might use Dictionary('/etc/raddb/dictionary') or a custom path.
try:
    # Try to load dictionaries from common system paths
    radius_dict = Dictionary(getDictionaryPaths())
except FileNotFoundError:
    print("Warning: Could not find system RADIUS dictionaries. Using a minimal inline dictionary for example.")
    # Fallback to a minimal dictionary for demonstration if no default found.
    radius_dict = Dictionary({"RADIUS": [
        {"name": "User-Name", "type": "string", "code": 1},
        {"name": "User-Password", "type": "string", "code": 2},
        {"name": "NAS-IP-Address", "type": "ipv4addr", "code": 4}
    ]})

# Configure RADIUS server details (use environment variables for security in real apps)
RADIUS_SERVER = os.environ.get('RADIUS_SERVER', '127.0.0.1')
RADIUS_PORT = int(os.environ.get('RADIUS_PORT', '1812'))
RADIUS_SECRET = os.environ.get('RADIUS_SECRET', 'testing123')

try:
    # Initialize the RADIUS client
    client = RadiusClient(
        server=RADIUS_SERVER, 
        authport=RADIUS_PORT, 
        secret=RADIUS_SECRET.encode(), 
        dict=radius_dict
    )

    # Create an Access-Request packet
    request = client.CreateAuthPacket(code=AccessRequest)
    request["User-Name"] = "testuser"
    request["User-Password"] = "testpassword"
    request["NAS-IP-Address"] = "192.168.1.100"

    print(f"Sending Access-Request to {RADIUS_SERVER}:{RADIUS_PORT}...")
    
    # Send the packet and wait for a reply
    reply = client.SendPacket(request)

    if reply:
        print(f"Received reply: {reply.code}")
        if reply.code == 2:  # Access-Accept
            print("Authentication successful!")
        else:
            print("Authentication failed.")
        print("Reply attributes:")
        for attr_name in reply.keys():
            # Using .get() is safer for potentially multi-valued attributes
            print(f"  {attr_name}: {reply.get(attr_name)}")
    else:
        print("No reply received from RADIUS server (timeout or network issue).")

except Exception as e:
    print(f"An error occurred during RADIUS communication: {e}")

view raw JSON →