Created
May 12, 2024 23:06
-
-
Save curtischong/b11a121bba1e1f52ce25e6949d859016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Prompt for the EC2 endpoint | |
echo "Please paste the EC2 endpoint:" | |
read EC2_ENDPOINT | |
# Check if the input looks like an EC2 endpoint | |
if [[ "$EC2_ENDPOINT" =~ ec2-[0-9-]+\.compute-[0-9]+\.amazonaws\.com ]]; then | |
# Create a temporary file for the modified config | |
TMP_FILE=$(mktemp) | |
# Flag to track whether the 'calan' host has been found | |
HOST_FOUND=0 | |
# Read .ssh/config line by line | |
while IFS= read -r line | |
do | |
# Check for the host calan | |
if [[ "$line" =~ "Host calan" ]]; then | |
HOST_FOUND=1 | |
echo "$line" >> "$TMP_FILE" | |
elif [[ "$HOST_FOUND" -eq 1 && "$line" =~ HostName ]]; then | |
# Update the HostName line | |
echo " HostName $EC2_ENDPOINT" >> "$TMP_FILE" | |
HOST_FOUND=0 | |
else | |
# Copy the line as is | |
echo "$line" >> "$TMP_FILE" | |
fi | |
done < ~/.ssh/config | |
# Move the temporary file to the original config file location | |
mv "$TMP_FILE" ~/.ssh/config | |
echo "HostName updated to $EC2_ENDPOINT in .ssh/config" | |
# Remove the temporary file if needed | |
# rm "$TMP_FILE" | |
else | |
echo "Error: The input does not contain a valid EC2 endpoint" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment