#!/bin/bash # VARIABLES script_version="1.3" ssh_keys_dir="$HOME/.ssh" netBaseDir="https://tech.npssi.com/ssh/pub" ##################################### npssi_checkCommands() { if ! [ -x "$(command -v wget)" ]; then show "[x] Error: Please install wget" exit 1 fi } npssi_checkCommands ###################################### ##### ALL FUNCTIONS USED TO RUN THIS SCRIPT ###### # Show the date with style npssi_showDate() { printf "| %-40s |\n" "`date`" echo "| " } # Show text with style npssi_show() { printf "|`tput bold` %-40s `tput sgr0` |\n" "$@" } # Show text with error color npssi_showerror() { printf "|`tput bold``tput setf 4` %-40s `tput sgr0` |\n" "$@" } # Show text with success color npssi_showsuccess() { printf "|`tput bold``tput setf 2` %-40s `tput sgr0` |\n" "$@" } # Show the banner npssi_banner() { echo "+-------------------------------------------+" } # Check network by asking script npssi_checkNet() { status=$(wget -qO- conntest.npatouillard.net) if [ "$status" = "ok" ] ; then echo 0 return 0 fi echo 1 return 1 } # CREATE .SSH DIR IF NOT EXIST npssi_sshDir() { if [[ ! -e $ssh_keys_dir ]]; then if mkdir -p $ssh_keys_dir ; then echo 0 return 0 else echo 1 return 1 fi fi echo 0 return 0 } # CHECK IF THE KEY EXIST npssi_checkPubKey() { wget -qO- $keyURL if [ $? = 0 ] ; then echo 0 return 0 fi echo 1 return 1 } # ADD public key in .authorized_key npssi_addPubKey() { echo echo "#Nicolas Patouillard @NPSSI SSH Key $keyVersion" ssh_key=$(wget -qO- $keyURL) echo $ssh_key } >> $ssh_keys_dir/authorized_keys ################################################################################ ########## MAIN SCRIPT ########## ################################################################################ npssi_banner npssi_show npssi_show " Adding SSH Key of Nicolas @NPSSI" npssi_show " v$script_version" npssi_show keyVersion=$1 keyURL="$netBaseDir$keyVersion" # Checking network res=$(npssi_checkNet) if [ "$res" = 1 ]; then npssi_showerror "[+] Err: Can't access npatouillard's server." npssi_show npssi_banner exit 1 fi # Create .ssh DIR res=$(npssi_sshDir) if [ "$res" = 1 ]; then npssi_showerror "[+] Err: Can't create ssh folder" npssi_showerror " $ssh_keys_dir" npssi_show npssi_banner exit 1 fi # Check if the key exist in server res=$(npssi_checkPubKey) if [ "$res" = 1 ]; then npssi_showerror "[+] Error: Unable do get the public key" npssi_show npssi_showerror "Please : Check if the NAME you entered" npssi_showerror " is a valid NPSSI Key" npssi_show npssi_showerror "NOTE : Leave empty if you don't know" npssi_show npssi_banner exit 1 fi # Adding the public key npssi_addPubKey # Key the IP IP=$(wget -qO- ip.npatouillard.net) npssi_showsuccess " SSH Public key added !" npssi_showsuccess " Access available from :" npssi_show npssi_showsuccess "$USER@$IP" npssi_banner exit 0