|
#!/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 |