Splunk SDK for Python

2.1.1 · active · verified Sat Apr 11

The Splunk Software Development Kit for Python allows developers to programmatically interact with the Splunk platform's REST API. It provides a Pythonic interface for searching data, managing configurations, working with indexes and inputs, and building custom applications. The library is actively maintained with frequent minor releases and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a Splunk Enterprise instance using username and password authentication, and then lists the installed applications. It uses environment variables for credentials, which is a common practice for security.

import os
import splunklib.client as client

# Configure connection details using environment variables or replace directly
HOST = os.environ.get('SPLUNK_HOST', 'localhost')
PORT = int(os.environ.get('SPLUNK_PORT', 8089))
USERNAME = os.environ.get('SPLUNK_USERNAME', 'admin')
PASSWORD = os.environ.get('SPLUNK_PASSWORD', 'your_password') # Use a strong password or token in production

try:
    # Connect to Splunk
    service = client.connect(
        host=HOST,
        port=PORT,
        username=USERNAME,
        password=PASSWORD,
        autologin=True,
        # Set verify=False for self-signed certificates in development, but not recommended for production
        # verify=False # Example: os.environ.get('SPLUNK_SSL_VERIFY', 'true').lower() == 'true'
    )

    # Print connected user and Splunk version
    print(f"Connected as: {service.username}")
    print(f"Splunk version: {service.info['version']}")

    # List available apps
    print("\nAvailable apps:")
    for app in service.apps:
        print(f"- {app.name}")

except Exception as e:
    print(f"Error connecting to Splunk: {e}")
    print("Please ensure Splunk is running and connection details (host, port, username, password) are correct.")
    print("For self-signed certificates, you might need to set verify=False (not recommended for production).")

view raw JSON →