Filip Kujawa | 5bdc75f | 2023-11-26 08:35:18 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # This script sets up SSH configuration for tunnel forwarding so you can |
| 4 | # build and deploy code without having the code locally. |
| 5 | # It creates or appends to the local SSH config file (~/.ssh/config) |
| 6 | # and connects to the build server to create or append the remote SSH config. |
| 7 | # It prompts the user for their SSH username, the name of the local identity file, |
| 8 | # and the name of the remote identity file. |
| 9 | |
| 10 | # Instructions: |
| 11 | # 1. Run this script locally on your machine. |
| 12 | # 2. Make the script executable: chmod +x setup_ssh_configs.sh |
| 13 | # 3. Run the script: ./setup_ssh_configs.sh |
| 14 | |
| 15 | # Note: Ensure that the local and remote SSH config template files are |
| 16 | # in the same directory as this script. |
| 17 | |
| 18 | # TODO: Use jinja for better templating |
| 19 | # TODO: Use python to also support windows devices. |
| 20 | |
| 21 | # Get the script's directory |
| 22 | script_dir=$(dirname "$(realpath "$0")") |
| 23 | |
| 24 | # Prompt for user input |
| 25 | read -p "Enter your username for SSH connection: " username |
| 26 | read -p "Enter the name of the local identity file (e.g., id_971_ed25519): " local_identity_file |
| 27 | read -p "Enter the name of the remote identity file (e.g., id_971_ed25519): " remote_identity_file |
| 28 | |
| 29 | local_ssh_config_template="$script_dir/local_config_template.txt" |
| 30 | remote_ssh_config_template="$script_dir/remote_config_template.txt" |
| 31 | |
| 32 | |
| 33 | local_ssh_config_content=$(cat "$local_ssh_config_template" | sed -e "s/{{username}}/$username/g" -e "s/{{identity_file}}/$local_identity_file/g") |
| 34 | local_ssh_config="$HOME/.ssh/config" |
| 35 | |
| 36 | # Check if the local SSH config file already exists |
| 37 | if [ -f "$local_ssh_config" ]; then |
| 38 | # Append |
| 39 | echo "$local_ssh_config_content" >> "$local_ssh_config" |
| 40 | else |
| 41 | echo "$local_ssh_config_content" > "$local_ssh_config" |
| 42 | chmod 600 "$local_ssh_config" |
| 43 | fi |
| 44 | |
| 45 | remote_ssh_config_content=$(cat "$remote_ssh_config_template" | sed -e "s/{{username}}/$username/g" -e "s/{{identity_file}}/$remote_identity_file/g") |
| 46 | remote_ssh_config="~/.ssh/config" |
| 47 | |
| 48 | ssh "frc971" "echo '$remote_ssh_config_content' >> $remote_ssh_config" |
| 49 | |
| 50 | echo "SSH configuration for tunnel forwarding has been set up locally and on the build server." |