Skip to content

Instantly share code, notes, and snippets.

@reveal79
Forked from K-Yo/splunk-download.sh
Created January 3, 2025 17:12
Show Gist options
  • Select an option

  • Save reveal79/d1c832886c888590c60df06723fb461f to your computer and use it in GitHub Desktop.

Select an option

Save reveal79/d1c832886c888590c60df06723fb461f to your computer and use it in GitHub Desktop.
Bash script to help download the latest splunk version of your choice.
#!/bin/bash
# code used for the article https://olivontech.com/en/posts/splunk/download-splunk-without-login/
# inspired from https://gist.github.com/ThinGuy/ee76f181151047267cdb38b7e1c1f1e3
# compatible with bash 4.4+
# you need curl for the dowloads to happen
# if download is interrupted, it will resume where it left off
download-splunk() {
# Array of Splunk URLS
echo "⏳ fetching the list of splunk enterprise URLs..."
local -a SPLUNK_ENTERPRISE_URLS
mapfile -t SPLUNK_ENTERPRISE_URLS < <(curl -sSlL https://www.splunk.com/en_us/download/splunk-enterprise.html | grep -oP '(?<=data-link=")[^"]+')
echo "⏳ fetching the list of splunk universal forwarder URLs..."
local -a SPLUNK_UF_URLS
mapfile -t SPLUNK_UF_URLS < <(curl -sSlL https://www.splunk.com/en_us/download/universal-forwarder.html | grep -oP '(?<=data-link=")[^"]+')
local -a ALL_URLS
ALL_URLS=("${SPLUNK_ENTERPRISE_URLS[@]}" "${SPLUNK_UF_URLS[@]}")
# Display the array elements to the user
echo "❓Please choose a value from the following list:"
for i in "${!ALL_URLS[@]}"; do
printf "%2d. %s\n" "$((i + 1))" "${ALL_URLS[i]}"
done
# Prompt for user selection
while true; do
read -rp "Enter the number of your choice (1-${#ALL_URLS[@]}): " choice
# Validate input
if ((choice > 0)) && ((choice <= ${#ALL_URLS[@]})); then
# Adjust for zero-based indexing
local SELECTED_URL="${ALL_URLS[$((choice - 1))]}"
local FILENAME="${SELECTED_URL##*/}"
# Download the file
echo " ⤵️ Dowloading to current directory: $FILENAME"
curl -L -O -C - "$SELECTED_URL"
echo "Downloaded \"$FILENAME\""
echo "🎉Done, have a great day!"
break
else
echo "❌Invalid selection. Please enter a number between 1 and ${#ALL_URLS[@]}."
fi
done
}
download-splunk
@reveal79
Copy link
Author

reveal79 commented Jan 3, 2025

Here is another one shell script friendly

`#!/bin/bash

Function to fetch URLs from a page

get_urls_from_page() {
local url=$1
local pattern=$2
curl -s "$url" | grep -oP "$pattern"
}

Main function to download Splunk

download_splunk() {
echo "⏳ Fetching the list of Splunk Enterprise URLs..."
SPLUNK_ENTERPRISE_URLS=$(get_urls_from_page "https://www.splunk.com/en_us/download/splunk-enterprise.html" 'data-link="[^"]+"')

echo "⏳ Fetching the list of Splunk Universal Forwarder URLs..."
SPLUNK_UF_URLS=$(get_urls_from_page "https://www.splunk.com/en_us/download/universal-forwarder.html" 'data-link="[^"]+"')

# Combine all URLs into an array
ALL_URLS=($(echo "$SPLUNK_ENTERPRISE_URLS" | tr -d 'data-link="' | tr '\n' ' '))
ALL_URLS+=($(echo "$SPLUNK_UF_URLS" | tr -d 'data-link="' | tr '\n' ' '))

if [ ${#ALL_URLS[@]} -eq 0 ]; then
    echo "❌ No URLs were fetched. Please check the URLs or your internet connection."
    exit 1
fi

# Display the list of URLs to the user
echo "❓ Please choose a value from the following list:"
for i in "${!ALL_URLS[@]}"; do
    printf "%2d. %s\n" $((i + 1)) "${ALL_URLS[$i]}"
done

# Prompt for user selection
while true; do
    read -p "Enter the number of your choice (1-${#ALL_URLS[@]}): " choice
    if [[ $choice =~ ^[0-9]+$ ]] && [ $choice -ge 1 ] && [ $choice -le ${#ALL_URLS[@]} ]; then
        SELECTED_URL="${ALL_URLS[$((choice - 1))]}"
        FILENAME=$(basename "$SELECTED_URL")

        # Check if file already exists and resume download if possible
        if [ -f "$FILENAME" ]; then
            echo "⏳ Resuming download for: $FILENAME"
            curl -C - -O "$SELECTED_URL"
        else
            echo "⤵️ Downloading to current directory: $FILENAME"
            curl -O "$SELECTED_URL"
        fi

        echo "Downloaded \"$FILENAME\""
        echo "🎉 Done, have a great day!"
        break
    else
        echo "❌ Invalid selection. Please enter a number between 1 and ${#ALL_URLS[@]}."
    fi
done

}

download_splunk
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment