Skip to content

Instantly share code, notes, and snippets.

@v1k0d3n
Last active September 4, 2024 15:38
Show Gist options
  • Select an option

  • Save v1k0d3n/252d6761e041043f1a88910f8c79fabf to your computer and use it in GitHub Desktop.

Select an option

Save v1k0d3n/252d6761e041043f1a88910f8c79fabf to your computer and use it in GitHub Desktop.

Revisions

  1. v1k0d3n revised this gist Sep 4, 2024. 1 changed file with 31 additions and 1 deletion.
    32 changes: 31 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,34 @@

    This is just a crude, simple script to help users run the most basic of Redfish commands like power On/Off the system, and mount ISO files (via web URL). The only tools required to run this script are `curl` and `jq` (for json pretty printing and better json readability). The script will also allow users to explore Redfish API endpoints, which is my primary use for it.

    Leave questions or comments below. I'm happy to take suggestions if anyone really finds it useful. For the most part, I'm going to suggest better tooling as this script is just supposed to be pretty basic. This tool is supposed to be a butter-knife, not a scalpel.
    Leave questions or comments below. I'm happy to take suggestions if anyone really finds it useful. For the most part, I'm going to suggest better tooling as this script is just supposed to be pretty basic. This tool is supposed to be a butter-knife, not a scalpel.

    Usage:

    ```
    curl -L https://gist.github.com/v1k0d3n/252d6761e041043f1a88910f8c79fabf/raw/f4cef77613fac2450f929814d377bc55b318f570/redfish-helper.sh -o redfish-helper.sh
    chmod +x redfish-helper.sh
    ❯ ./redfish-helper.sh
    Current settings:
    1) Redfish Host: 192.168.1.84
    2) Redfish Port: null
    3) Redfish Method: https
    4) Redfish SSL Verification: no
    5) Redfish System ID: 1
    6) Redfish Manager ID: 1
    7) Redfish Username: bjozsa
    8) Redfish ISO URL: http://192.168.1.36/iso/Fedora-Server-dvd-x86_64-34-1.2.iso
    Are the settings above correct? (y/n): y
    Enter Redfish password: *********************
    Select an option:
    1) Fetch system information
    2) Fetch manager information
    3) Explore system/manager endpoints
    4) Turn on system
    5) Turn off system
    6) Check virtual media state
    7) Mount and boot to ISO
    Enter your choice (1-6):
    ```
  2. v1k0d3n created this gist Sep 4, 2024.
    5 changes: 5 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ## Redfish Helper Script

    This is just a crude, simple script to help users run the most basic of Redfish commands like power On/Off the system, and mount ISO files (via web URL). The only tools required to run this script are `curl` and `jq` (for json pretty printing and better json readability). The script will also allow users to explore Redfish API endpoints, which is my primary use for it.

    Leave questions or comments below. I'm happy to take suggestions if anyone really finds it useful. For the most part, I'm going to suggest better tooling as this script is just supposed to be pretty basic. This tool is supposed to be a butter-knife, not a scalpel.
    244 changes: 244 additions & 0 deletions redfish-helper.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,244 @@
    #!/bin/bash

    # Define a path to store the environment variables
    ENV_FILE="$HOME/.redfish_vars"

    # Function to save variables to a file
    save_vars() {
    cat << EOF > "$ENV_FILE"
    export REDFISH_HOST="$REDFISH_HOST"
    export REDFISH_PORT="$REDFISH_PORT"
    export REDFISH_METHOD="$REDFISH_METHOD"
    export REDFISH_METHOD_VERIFY="$REDFISH_METHOD_VERIFY"
    export REDFISH_SYSTEM="$REDFISH_SYSTEM"
    export REDFISH_MANAGER="$REDFISH_MANAGER"
    export REDFISH_USER="$REDFISH_USER"
    export REDFISH_ISO_URL="$REDFISH_ISO_URL"
    EOF
    }

    # Function to read password with hidden input and show asterisks (*)
    read_password() {
    echo -n "Enter Redfish password: "
    password=""
    while IFS= read -r -s -n 1 char; do
    if [[ -z $char ]]; then
    break
    fi
    if [[ $char == $'\177' ]]; then # Handle backspace (delete last character)
    if [[ -n "$password" ]]; then
    password=${password%?}
    echo -ne "\b \b" # Erase last character
    fi
    else
    password+="$char"
    echo -n "*"
    fi
    done
    echo # Ensure newline after password input
    REDFISH_PASS="$password" # Set the password to the variable
    }

    # Function to show the current variables and allow changes
    review_variables() {
    echo "Current settings:"
    echo "1) Redfish Host: ${REDFISH_HOST}"
    echo "2) Redfish Port: ${REDFISH_PORT}"
    echo "3) Redfish Method: ${REDFISH_METHOD}"
    echo "4) Redfish SSL Verification: ${REDFISH_METHOD_VERIFY}"
    echo "5) Redfish System ID: ${REDFISH_SYSTEM}"
    echo "6) Redfish Manager ID: ${REDFISH_MANAGER}"
    echo "7) Redfish Username: ${REDFISH_USER}"
    echo "8) Redfish ISO URL: ${REDFISH_ISO_URL}"

    while true; do
    read -p "Are the settings above correct? (y/n): " change_input
    if [[ $change_input == "y" || $change_input == "Y" ]]; then
    break
    elif [[ $change_input == "n" || $change_input == "N" ]]; then
    read -p "Enter the number of the setting you'd like to change (1-8): " change_number
    case $change_number in
    1)
    read -p "Enter new Redfish Host: " REDFISH_HOST
    ;;
    2)
    read -p "Enter new Redfish Port (leave blank for default): " REDFISH_PORT
    REDFISH_PORT=${REDFISH_PORT:-"null"} # Set REDFISH_PORT to "null" if left blank
    ;;
    3)
    read -p "Enter new Redfish Method (http/https): " REDFISH_METHOD
    ;;
    4)
    read -p "Verify self-signed certificates? (yes/no): " REDFISH_METHOD_VERIFY
    ;;
    5)
    read -p "Enter new Redfish System ID: " REDFISH_SYSTEM
    ;;
    6)
    read -p "Enter new Redfish Manager ID: " REDFISH_MANAGER
    ;;
    7)
    read -p "Enter new Redfish Username: " REDFISH_USER
    ;;
    8)
    read -p "Enter new Redfish ISO URL: " REDFISH_ISO_URL
    ;;
    *)
    echo "Invalid choice. Please enter a number between 1 and 8."
    ;;
    esac
    else
    echo "Please answer with 'y' for yes or 'n' for no."
    fi
    done
    }

    # Load variables from the file if it exists
    if [ -f "$ENV_FILE" ]; then
    source "$ENV_FILE"
    fi

    # If any variable is not set, prompt the user for input
    if [ -z "$REDFISH_HOST" ]; then
    read -p "Enter the Redfish host IP: " REDFISH_HOST
    fi

    if [ -z "$REDFISH_PORT" ]; then
    read -p "Use a custom port? (Enter port or leave blank for default): " REDFISH_PORT
    REDFISH_PORT=${REDFISH_PORT:-"null"} # Set REDFISH_PORT to "null" if left blank
    fi

    if [ -z "$REDFISH_METHOD" ]; then
    read -p "Enter the Redfish method (http/https) [https]: " REDFISH_METHOD
    REDFISH_METHOD=${REDFISH_METHOD:-https} # Default to https if nothing is entered
    fi

    if [ -z "$REDFISH_METHOD_VERIFY" ]; then
    read -p "Verify self-signed certificates? (yes/no) [no]: " REDFISH_METHOD_VERIFY
    REDFISH_METHOD_VERIFY=${REDFISH_METHOD_VERIFY:-no} # Default to no if nothing is entered
    fi

    if [ -z "$REDFISH_SYSTEM" ]; then
    read -p "Enter Redfish System ID (default System.Embedded.1): " REDFISH_SYSTEM
    REDFISH_SYSTEM=${REDFISH_SYSTEM:-System.Embedded.1} # Default to System.Embedded.1
    fi

    if [ -z "$REDFISH_MANAGER" ]; then
    read -p "Enter Redfish Manager ID (default iDRAC.Embedded.1): " REDFISH_MANAGER
    REDFISH_MANAGER=${REDFISH_MANAGER:-iDRAC.Embedded.1} # Default to iDRAC.Embedded.1
    fi

    if [ -z "$REDFISH_USER" ]; then
    read -p "Enter Redfish username: " REDFISH_USER
    fi

    if [ -z "$REDFISH_ISO_URL" ]; then
    read -p "Enter Redfish ISO URL (or leave blank): " REDFISH_ISO_URL
    fi

    # Save the variables to the environment file for future runs
    save_vars

    # Review and allow changes to variables
    review_variables

    # Always prompt for the password
    read_password

    # Menu options
    while true; do
    echo "Select an option:"
    echo "1) Fetch system information"
    echo "2) Fetch manager information"
    echo "3) Explore system/manager endpoints"
    echo "4) Turn on system"
    echo "5) Turn off system"
    echo "6) Check virtual media state"
    echo "7) Mount and boot to ISO"
    read -p "Enter your choice (1-6): " choice

    case $choice in
    1)
    # Fetch system information
    if [ "$REDFISH_PORT" == "null" ]; then
    SYSTEM_URL="$REDFISH_METHOD://$REDFISH_HOST/redfish/v1/Systems/$REDFISH_SYSTEM"
    else
    SYSTEM_URL="$REDFISH_METHOD://$REDFISH_HOST:$REDFISH_PORT/redfish/v1/Systems/$REDFISH_SYSTEM"
    fi
    echo "Fetching system information..."
    curl -s -k -u "$REDFISH_USER:$REDFISH_PASS" "$SYSTEM_URL" | jq '.'
    ;;
    2)
    # Fetch manager information
    if [ "$REDFISH_PORT" == "null" ]; then
    MANAGER_URL="$REDFISH_METHOD://$REDFISH_HOST/redfish/v1/Managers/$REDFISH_MANAGER"
    else
    MANAGER_URL="$REDFISH_METHOD://$REDFISH_HOST:$REDFISH_PORT/redfish/v1/Managers/$REDFISH_MANAGER"
    fi
    echo "Fetching manager information..."
    curl -s -k -u "$REDFISH_USER:$REDFISH_PASS" "$MANAGER_URL" | jq '.'
    ;;
    3)
    # Explore system/manager endpoints
    read -p "Enter the endpoint to explore (e.g., Managers/1/VirtualMedia/): " endpoint
    EXPLORATION_URL="$REDFISH_METHOD://$REDFISH_HOST/redfish/v1/$endpoint"
    echo "Exploring endpoint: $EXPLORATION_URL"
    curl -s -k -u "$REDFISH_USER:$REDFISH_PASS" "$EXPLORATION_URL" | jq '.'
    ;;
    4)
    # Turn on system
    echo "Turning on system..."
    curl -X POST -k -u "$REDFISH_USER:$REDFISH_PASS" -d '{"ResetType": "On"}' "$SYSTEM_URL/Actions/ComputerSystem.Reset"
    ;;
    5)
    # Turn off system
    echo "Turning off system..."
    curl -X POST -k -u "$REDFISH_USER:$REDFISH_PASS" -d '{"ResetType": "GracefulShutdown"}' "$SYSTEM_URL/Actions/ComputerSystem.Reset"
    ;;

    6)
    # Check virtual media state
    if [ "$REDFISH_PORT" == "null" ]; then
    VIRTUAL_MEDIA_URL="$REDFISH_METHOD://$REDFISH_HOST/redfish/v1/Managers/$REDFISH_MANAGER/VirtualMedia/CD1"
    else
    VIRTUAL_MEDIA_URL="$REDFISH_METHOD://$REDFISH_HOST:$REDFISH_PORT/redfish/v1/Managers/$REDFISH_MANAGER/VirtualMedia/CD1"
    fi
    echo "Checking virtual media state..."
    curl -s -k -u "$REDFISH_USER:$REDFISH_PASS" "$VIRTUAL_MEDIA_URL" | jq '.'
    ;;

    7)
    # Mount and boot to ISO
    if [ -z "$REDFISH_ISO_URL" ]; then
    echo "No ISO URL provided."
    else
    echo "Mounting and booting to ISO..."

    # Ensure MANAGER_URL is correctly set
    if [ "$REDFISH_PORT" == "null" ]; then
    MANAGER_URL="$REDFISH_METHOD://$REDFISH_HOST/redfish/v1/Managers/$REDFISH_MANAGER"
    SYSTEM_URL="$REDFISH_METHOD://$REDFISH_HOST/redfish/v1/Systems/$REDFISH_SYSTEM"
    else
    MANAGER_URL="$REDFISH_METHOD://$REDFISH_HOST:$REDFISH_PORT/redfish/v1/Managers/$REDFISH_MANAGER"
    SYSTEM_URL="$REDFISH_METHOD://$REDFISH_HOST:$REDFISH_PORT/redfish/v1/Systems/$REDFISH_SYSTEM"
    fi

    # Mount ISO using VirtualMedia
    curl -X POST -k -u "$REDFISH_USER:$REDFISH_PASS" -d "{\"Image\": \"$REDFISH_ISO_URL\"}" "$MANAGER_URL/VirtualMedia/CD1/Actions/VirtualMedia.InsertMedia"

    # Set boot override to CD and boot once
    curl -X PATCH -k -u "$REDFISH_USER:$REDFISH_PASS" -d '{"Boot": {"BootSourceOverrideEnabled": "Once", "BootSourceOverrideMode": "UEFI", "BootSourceOverrideTarget": "Cd"}}' "$SYSTEM_URL"
    fi
    ;;

    *)
    echo "Invalid option, please choose a valid option."
    ;;
    esac

    read -p "Would you like to perform another action? (y/n): " continue_action
    if [[ "$continue_action" != "y" && "$continue_action" != "Y" ]]; then
    echo "Have a wonderful day!"
    break
    fi
    done