/ .devnet / config.sh
config.sh
 1  #!/bin/bash
 2  
 3  # Read the EC2 instance name from the user
 4  read -p "Enter the EC2 instance name to filter by (e.g. Name) (default: devnet): " INSTANCE_NAME
 5  INSTANCE_NAME="${INSTANCE_NAME:-devnet}"
 6  
 7  # Read the PEM file path from the user or use the default in ~/.ssh
 8  read -p "Enter the PEM file path (default: ~/.ssh/s3-devnet.pem): " PEM_FILE
 9  PEM_FILE="${PEM_FILE:-~/.ssh/s3-devnet.pem}"
10  
11  # Use the AWS CLI to describe running EC2 instances, filter by the provided name, and store the JSON output in a variable
12  instance_info=$(aws ec2 describe-instances \
13    --filters "Name=tag:Name,Values=$INSTANCE_NAME" "Name=instance-state-name,Values=running" \
14    --output json)
15  
16  # Parse the JSON output to extract information about the instances
17  instance_ids=($(echo "$instance_info" | jq -r '.Reservations[].Instances[].InstanceId'))
18  instance_names=($(echo "$instance_info" | jq -r '.Reservations[].Instances[].Tags[] | select(.Key=="Name") | .Value'))
19  instance_states=($(echo "$instance_info" | jq -r '.Reservations[].Instances[].State.Name'))
20  instance_ips=($(echo "$instance_info" | jq -r '.Reservations[].Instances[].PublicIpAddress'))
21  
22  # Initialize the SSH config string
23  SSH_CONFIG=""
24  
25  # Loop through the instance IDs and print information for each instance
26  for i in ${!instance_ids[@]}; do
27  #    echo "Instance ID: ${instance_ids[$i]}"
28  #    echo "Instance Name: ${instance_names[$i]}"
29  #    echo "Instance State: ${instance_states[$i]}"
30  #    echo "Instance IP: ${instance_ips[$i]}"
31  #    echo "------------------------"
32  
33      # Append SSH config entries to the string
34      SSH_CONFIG+="Host aws-n$i"$'\n'
35      SSH_CONFIG+="  HostName ${instance_ips[$i]}"$'\n'
36      SSH_CONFIG+="  User ubuntu"$'\n'
37      SSH_CONFIG+="  IdentityFile $PEM_FILE"$'\n'
38      SSH_CONFIG+="  Port 22"$'\n'
39      SSH_CONFIG+=$'\n'
40  done
41  
42  # Print or save the SSH config string as needed
43  echo -e "\n\n# AWS Devnet Nodes\n\n$SSH_CONFIG"