fetch-logs.sh
1 #!/bin/bash 2 3 # Determine the number of AWS EC2 instances by checking ~/.ssh/config 4 NODE_ID=0 5 while [ -n "$(grep "aws-n${NODE_ID}" ~/.ssh/config)" ]; do 6 NODE_ID=$((NODE_ID + 1)) 7 done 8 9 # Read the number of AWS EC2 instances to query from the user 10 read -p "Enter the number of AWS EC2 instances to query (default: $NODE_ID): " NUM_INSTANCES 11 NUM_INSTANCES="${NUM_INSTANCES:-$NODE_ID}" 12 13 echo "Using $NUM_INSTANCES AWS EC2 instances for querying." 14 15 # Define the directory where logs will be saved 16 log_directory="$HOME/snarkos_logs" 17 18 # Create the log directory if it doesn't already exist 19 mkdir -p "$log_directory" 20 21 # Loop from 0 to 49 22 for i in $(seq 0 $(($NUM_INSTANCES - 1))); do 23 echo "Connecting to aws-n$i..." 24 # Use sftp to connect, execute commands, and exit 25 sftp aws-n$i << EOF 26 cd /tmp 27 get snarkos.log "$log_directory/snarkos-$i.log" 28 EOF 29 echo "Downloaded snarkos.log from aws-n$i as snarkos-$i.log into $log_directory" 30 done 31 32 echo "All files have been downloaded to $log_directory."