Skip to content

Commit

Permalink
Add init_ssh.sh (also added to init_session.sh)
Browse files Browse the repository at this point in the history
Original idea by @GonzSanch (gsanchez): GonzSanch@0834348
  • Loading branch information
alexandregv committed Nov 6, 2021
1 parent 42889e7 commit 50748c0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ You can also use the scripts of your choice without cloning the repository. You
### init_session
A script to init your session at 42 when you log in. It can:
- Connect your bluetooth device
- Init SSH (see [init_ssh](#init_ssh))
- Init docker (see [init_docker](#init_docker))
- Check if all your apps are installed and start them
- Update brew and its formulas
- Clean your disk (also check [free_space](#free_space))
- Start [RP42](https://github.com/alexandregv/RP42)
- Open System Preferences (You could need it to edit your keyboard/screen settings, etc)

### init_ssh
A script to create an SSH key pair if needed, then see how to add it on the intra.
It can copy the public key to your clipboard if supported (macOS or Linux with xsel/xclip) and open the intra settings page automatically.

### init_docker
A script to init Docker for Mac at 42.
It checks if Docker for Mac is properly installed and makes symlinks to the goinfre to not fill your session disk.
Expand Down
6 changes: 6 additions & 0 deletions init_session.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ reset=$'\033[0;39m'
# Config
toolbox_path="$HOME/42toolbox" #=> 42toolbox path. See https://github.com/alexandregv/42toolbox
bluetooth_device="" #=> Bluetooth device name. Empty = skip bluetooth setup
init_ssh=true #=> Init SSH? Creates SSH key if needed and shows how to add it on the intra
init_ssh_path="$toolbox_path/init_ssh.sh" #=> Location of init_ssh.sh file. See https://github.com/alexandregv/42toolbox/blob/master/init_ssh.sh
init_docker=true #=> Init Docker for Mac? See https://github.com/alexandregv/42toolbox/blob/master/init_docker.sh
init_docker_path="$toolbox_path/init_docker.sh" #=> Location of init_docker.sh file. See https://github.com/alexandregv/42toolbox/blob/master/init_docker.sh
install_apps=true #=> Install desired apps if they are missing?
Expand All @@ -52,6 +54,10 @@ declare -a desired_apps=(
)

################################################################################
# Create SSH key if needed and show how to add it on the intra
# See init_ssh.sh
[ "$init_ssh" = true ] && $init_ssh_path

# Pair and connect a bluetooth device
if [ -n "$bluetooth_device" ]; then
echo -e "${blue}Setting up Bluetooth.${reset}"
Expand Down
58 changes: 58 additions & 0 deletions init_ssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash
# **************************************************************************** #
# #
# ::: :::::::: #
# init_ssh.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: aguiot-- <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/11/06 18:07:12 by aguiot-- #+# #+# #
# Updated: 2021/11/06 18:21:44 by aguiot-- ### ########.fr #
# #
# **************************************************************************** #

# Colors
blue=$'\033[0;34m'
cyan=$'\033[1;96m'
reset=$'\033[0;39m'

# Generate SSH key pair if needed
if [ -f ~/.ssh/id_rsa ]; then
echo -e "${blue}SSH key \`${cyan}~/.ssh/id_rsa${blue}\` already exists, using it${reset}"
key_already_exists=true
else
echo -e "${blue}Generating SSH key \`${cyan}~/.ssh/id_rsa${blue}\`${reset}"
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -C "$USER"
fi

# Start and use SSH agent
echo -e "\n${blue}Starting SSH agent and adding key${reset}"
eval "$(ssh-agent >/dev/null)"
ssh-add ~/.ssh/id_rsa

# Show the public key
echo -e "\n${blue}Here is your public key:\n---------${reset}"
cat ~/.ssh/id_rsa.pub
echo -e "${blue}---------${reset}"

# If possible, propose to copy the public key to the clipboard
if [ -x "$(command -v pbcopy)" ] || [ -x "$(command -v xclip)" ] || [ -x "$(command -v xsel)" ]; then
read -n1 -p "${blue}Do you want to copy it to your clipboard? [${cyan}Y${blue}/n]${reset} " input
echo -e "\n"
if [ -n "$input" ] && [ "$input" = "y" ]; then
[ -x "$(command -v pbcopy)" ] && pbcopy < ~/.ssh/id_rsa.pub
[ -x "$(command -v xclip)" ] && xclip -selection clipboard < ~/.ssh/id_rsa.pub
[ -x "$(command -v xsel)" ] && xsel --clipboard < ~/.ssh/id_rsa.pub
fi
fi

# Show some help to add the key to intra.42.fr
echo -e "${blue}Make sure that it's set in your intra settings (or github/gitlab/whatever): ${cyan}https://profile.intra.42.fr/gitlab_users\n${reset}"
if [ ! -z ${key_already_exists+x} ]; then
fingerprint="$(ssh-keygen -E md5 -l -f ~/.ssh/id_rsa | cut -d: -f2- | cut -d' ' -f1)"
echo -e "${blue}To check if your key is already set, open the settings page and check if one of your keys has this fingerprint:\n${cyan}${fingerprint}\n${reset}"
fi

# If possible, open intra SSH settings page
[ -x "$(command -v open)" ] && open 'https://profile.intra.42.fr/gitlab_users' &>/dev/null ;:
[ -x "$(command -v xdg-open)" ] && xdg-open 'https://profile.intra.42.fr/gitlab_users' &>/dev/null ;:

0 comments on commit 50748c0

Please sign in to comment.