求助:如何用Bash脚本从多台服务器获取Java/Spark版本及pip、Anaconda列表
Hey there! Let's work through this together—gathering environment details from multiple servers via Bash can be tricky, but we can fix up your script to get it working reliably. First, let's address the gaps in your current code and build out the remote execution logic you need.
Key Gaps in Your Current Script
Right now, your script checks the user and sets an environment variable, but it doesn't handle:
- Targeting specific remote servers (you have an
$idparameter but no logic to map it to a server) - Running commands remotely on those servers
- Capturing the specific Java/Spark/pip/conda details you need
Step-by-Step Enhancements
1. Add Server ID Mapping
First, let's map your $id parameter to actual server hostnames/IPs. You can use an associative array for this, or load from a config file if you have many servers:
#!/bin/bash -x set -euo pipefail # Add strict error handling to catch issues early # Map server IDs to hostnames/IPs (adjust this to match your environment) declare -A SERVER_MAP=( ["1"]="iqa-server-01" ["2"]="iqa-server-02" ["3"]="prod-server-01" ) # Validate user current_user=$(whoami) if [[ "$current_user" != "spark" ]]; then echo "Error: You must run this script as the 'spark' user" exit 1 fi # Validate input parameters if [[ $# -lt 1 ]]; then echo "Usage: $0 <server-id> [ENV]" exit 1 fi SERVER_ID=$1 ENV=${2:-IQA} # Default to IQA if no environment is provided # Get target server from ID map TARGET_SERVER=${SERVER_MAP[$SERVER_ID]} if [[ -z "$TARGET_SERVER" ]]; then echo "Error: Invalid server ID '$SERVER_ID'" exit 1 fi
2. Remote Execution via SSH
Assuming you've set up SSH key-based authentication (so you don't need to enter a password for each server), create a helper function to run commands remotely. We'll also handle edge cases like commands outputting to stderr (looking at you, java -version):
# Helper to run commands on remote server with error handling run_remote() { local cmd="$1" # Capture stderr and stdout together, check for SSH failures ssh -q "$TARGET_SERVER" "$cmd" if [[ $? -ne 0 ]]; then echo "Error: Failed to execute command on $TARGET_SERVER" exit 1 fi }
3. Collect Environment Details
Now add commands to fetch each piece of info you need. Adjust paths (like Spark/Conda locations) to match your servers' setup:
echo -e "\n=== Environment Details for $TARGET_SERVER ($ENV) ===" # Java Version (redirect stderr to stdout since java -version outputs there) echo -e "\n--- Java Version ---" run_remote "java -version 2>&1 | head -2" # Spark Version (adjust the spark-submit path based on environment) echo -e "\n--- Spark Version ---" if [[ "$ENV" == "IQA" ]]; then SPARK_CMD="/opt/spark-iqa/bin/spark-submit --version" elif [[ "$ENV" == "PROD" ]]; then SPARK_CMD="/opt/spark-prod/bin/spark-submit --version" else echo "Error: Unsupported environment '$ENV'" exit 1 fi run_remote "$SPARK_CMD | head -1" # Pip Package List (use pip3 if your server defaults to Python 3) echo -e "\n--- Pip Packages ---" run_remote "pip list --format=freeze" # Freeze format is easier to parse later # Conda Package List (source bashrc if conda needs initialization) echo -e "\n--- Conda Packages ---" run_remote "source ~/.bashrc && conda list"
4. Optional: Save Output to a File
If you want to save results for later reference, add this at the end:
OUTPUT_FILE="server-details-${TARGET_SERVER}-$(date +%Y%m%d).txt" echo -e "\nSaving output to $OUTPUT_FILE" # Capture all output to the file { echo "=== Environment Details for $TARGET_SERVER ($ENV) ===" echo -e "\n--- Java Version ---" ssh "$TARGET_SERVER" "java -version 2>&1 | head -2" echo -e "\n--- Spark Version ---" ssh "$TARGET_SERVER" "$SPARK_CMD | head -1" echo -e "\n--- Pip Packages ---" ssh "$TARGET_SERVER" "pip list --format=freeze" echo -e "\n--- Conda Packages ---" ssh "$TARGET_SERVER" "source ~/.bashrc && conda list" } > "$OUTPUT_FILE"
Common Pitfalls to Watch For
- SSH Connectivity: Make sure the
sparkuser has passwordless SSH access to all target servers. Test withssh $TARGET_SERVERfirst to confirm. - Command Paths: If
java,spark-submit,pip, orcondaaren't in the remote user's PATH, use absolute paths (e.g.,/usr/bin/java). - Conda Initialization: Some servers require sourcing
.bashrcor.condarcto activate conda—adjust the remote command if you see "conda: command not found".
Let me know if you hit specific issues like "command not found" errors, SSH timeouts, or need to tweak the output format. We can adjust this further to fit your exact setup!
内容的提问来源于stack exchange,提问作者nk07

