Splunk Enterprise

Run Python scripts with universal forwarder to modify files on the server

antoniocarletto
Engager

Hello,

i would need to run a python script, using splunk's universal forwarder, on the servers where the forwarder is installed.

The goal of the script is to check the hostname in the input.conf file (in $ SPLUNK_HOME / etc / system / local) so that if the server hostname and the hostname in the input.conf file are different, the script change hostname in input.conf file and restart splunk.
If the hostnames are the same, it does nothing and exits the script.

The script must be run every night at 2:00 AM, and I would like once the app is created it will be distributed to all servers.

 

 

import sys
import fileinput
import subprocess
import re

print("[+] Splunk Hostname Changer")


def get_hostname_to_command():
    hostC = subprocess.getoutput('hostname')
    print("[+] Result to hostname command: " + hostC)
    return hostC


def get_hostname_to_file(path):
    file = open(path,"r")
    hostRow = file.readlines()[1]
    hostF = re.search(r'[^host\s\=\s][A-Za-z]*[\-]*[A-Za-z]+',hostRow).group(0)
    print("[+] Hostname in inputfile.conf: " + hostF)
    return hostF


def hostname_check(hostC,hostF,path):
    if hostC != hostF:
        print("[-] Hostname mismatch. Change the hostname in input.conf!")
        for line in fileinput.input(path,inplace=True):
            line = line.replace(hostF, hostC)
            sys.stdout.write(line)
        print("[+] Hostname changed. Operation Complete!")
        print("[+] Restart Splunk...")
        subprocess.call(["C:\Program Files\SplunkUniversalForwarder\\bin\splunk.exe","restart"])
        print("[+] Restart Splunk completed!")
    else:
        print("[+] Hostname match. Exit to script!")


hostCommand = get_hostname_to_command()
hostFile = get_hostname_to_file("C:\Program Files\SplunkUniversalForwarder\etc\system\local\input.conf")
hostname_check(hostCommand,hostFile, "C:\Program Files\SplunkUniversalForwarder\etc\system\local\input.conf")

 

 

I have read some information scattered around the web and I still don't understand if this type of business is available or not.
Is it possible to do this?

I'm here for further informations.

Best regards,

Antonio

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Universal Forwarders do not come with Python so they cannot run Python scripts.  You'll need a heavy forwarder for that.  If you can't run heavy forwarders then you'll need to write a shell script that does the work.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

feelcool
Explorer
0 Karma

richgalloway
SplunkTrust
SplunkTrust

Universal Forwarders do not come with Python so they cannot run Python scripts.  You'll need a heavy forwarder for that.  If you can't run heavy forwarders then you'll need to write a shell script that does the work.

---
If this reply helps you, Karma would be appreciated.
0 Karma

marksheinbaum
Explorer

Is there a way to simulate that, i.e., hack. Say set up symlinks to python in Splunk's bin and lib directories?

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Think Like an Architect: Introducing the Splunk Certified Cybersecurity Defense ...

In cybersecurity, defenders respond to threats. Architects design the systems that stop them.    As ...

Index This | What has goals but no motivation?

June 2026 Edition  Hayyy Splunk Education Enthusiasts and the Eternally Curious!   We’re back with this ...

Deep Dive: Accelerate threat investigation with Splunk’s AI Assistant in Security

AI is one of the biggest topics in the market today, and for security teams, its value goes far beyond the ...