To add the IPs from ERROR_IPS2 to ERROR_IPS (sorted), and ensure your script processes both sets of error IPs, you can modify your script as follows:
-
Fix the syntax error in the definition of ERROR_IPS2: There is an extra pipe (| |) in the line defining ERROR_IPS2. Remove the extra pipe to correct the syntax.
-
Combine ERROR_IPS and ERROR_IPS2 into a single list: After extracting both sets of error IPs, merge them and sort uniquely.
Here's how you can modify your script:
# Extract error IPs
ERROR_IPS=$(grep "Connection timed out" "$TMP_FILE" | awk '{print $5}' | sort -u)
ERROR_IPS2=$(grep "Permission denied" "$TMP_FILE" | awk '{print $1}' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | sort -u)
# Combine ERROR_IPS and ERROR_IPS2 into a single sorted list
ALL_ERROR_IPS=$(echo -e "$ERROR_IPS\n$ERROR_IPS2" | sort -u)
- Update the loop to process all error IPs: Use
ALL_ERROR_IPS instead of ERROR_IPS.
# Check if there are any error IPs
if [[ -z "$ALL_ERROR_IPS" ]]; then
echo "No connection errors found. Exiting loop."
break
fi
# Loop through each problematic IP
for ip in $ALL_ERROR_IPS; do
HOST_NAME=$(awk -v ip="$ip" '/Host / { host=$2 } /HostName / && $2 == ip { print host }' ~/.axiom/.sshconfig)
if [[ -n "$HOST_NAME" ]]; then
echo "Processing host $HOST_NAME for IP $ip"
handle_fleet "$HOST_NAME"
else
echo "No host found for IP $ip"
fi
done
By combining ERROR_IPS2 into ALL_ERROR_IPS, your script will now process IPs associated with both "Connection timed out" and "Permission denied" errors, ensuring that all problematic IPs are addressed.
Complete Modified Section of the Script:
while true; do
# Create temporary log file
TMP_FILE=$(mktemp /home/nishant57/BB/XXXXXXXX.log)
# Run command and log output
axiom-exec --cache 'whoami' |& tee -a "$TMP_FILE"
# Extract error IPs
ERROR_IPS=$(grep "Connection timed out" "$TMP_FILE" | awk '{print $5}' | sort -u)
ERROR_IPS2=$(grep "Permission denied" "$TMP_FILE" | awk '{print $1}' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | sort -u)
# Combine ERROR_IPS and ERROR_IPS2 into a single sorted list
ALL_ERROR_IPS=$(echo -e "$ERROR_IPS\n$ERROR_IPS2" | sort -u)
# Clean up the temporary file
rm "$TMP_FILE"
# Check if there are any error IPs
if [[ -z "$ALL_ERROR_IPS" ]]; then
echo "No connection errors found. Exiting loop."
break
fi
# Loop through each problematic IP
for ip in $ALL_ERROR_IPS; do
HOST_NAME=$(awk -v ip="$ip" '/Host / { host=$2 } /HostName / && $2 == ip { print host }' ~/.axiom/.sshconfig)
if [[ -n "$HOST_NAME" ]]; then
echo "Processing host $HOST_NAME for IP $ip"
handle_fleet "$HOST_NAME"
else
echo "No host found for IP $ip"
fi
done
# After processing all errors, execute the following command
axiom-account linode1 ; axiom-ssh --just-generate ; mv ~/.axiom/.sshconfig ~/.axiom/.sshconfig.bak ; axiom-account linode2 ; axiom-ssh --just-generate ; mv ~/.axiom/.sshconfig ~/.axiom/.sshconfig2.bak ; axiom-account linode3 ; axiom-ssh --just-generate ; mv ~/.axiom/.sshconfig ~/.axiom/.sshconfig3.bak ; axiom-account linode4 ; axiom-ssh --just-generate ; cat ~/.axiom/.sshconfig.bak ~/.axiom/.sshconfig2.bak ~/.axiom/.sshconfig3.bak >> ~/.axiom/.sshconfig ; rm ~/.axiom/.sshconfig.bak ~/.axiom/.sshconfig2.bak ~/.axiom/.sshconfig3.bak ; cat ~/.axiom/.sshconfig | grep 'Host ' | cut -d ' ' -f 2 | grep -v '*' > ~/.axiom/selected.conf ; echo "remove extra instances from '~/.axiom/selected.conf'" ; cat ~/.axiom/selected.conf
# Re-execute command
axiom-exec --cache 'whoami'
# Wait
sleep 380
echo "Last whoami command..."
# Re-execute command
axiom-exec --cache 'whoami'
done
Explanation:
-
Syntax Fix: Removed the extra pipe | in ERROR_IPS2 definition.
-
Combining IPs: Used echo -e and combined both ERROR_IPS and ERROR_IPS2 into ALL_ERROR_IPS.
-
Sorting: The combined list is sorted using sort -u to ensure uniqueness.
-
Processing Loop: Updated the for-loop to iterate over ALL_ERROR_IPS so that all identified problematic IPs are processed.
-
Exit Condition: Changed the check to use ALL_ERROR_IPS to determine if the loop should continue.
Note: Always ensure that your script is tested in a safe environment before running it in production to prevent any unintended consequences.