My SSH Public Keys + Script Bash
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
ssh/add

178 lines
3.2 KiB

#!/bin/bash
# VARIABLES
script_version="1.1"
ssh_keys_dir="$HOME/.ssh"
netBaseDir="https://megamov.fr/ssh/pub"
#####################################
mega_checkCommands()
{
if ! [ -x "$(command -v wget)" ]; then
show "[x] Error: Please install wget"
exit 1
fi
}
mega_checkCommands
######################################
##### ALL FUNCTIONS USED TO RUN THIS SCRIPT ######
# Show the date with style
mega_showDate()
{
printf "| %-40s |\n" "`date`"
echo "| "
}
# Show text with style
mega_show()
{
printf "|`tput bold` %-40s `tput sgr0` |\n" "$@"
}
# Show text with error color
mega_showerror()
{
printf "|`tput bold``tput setf 4` %-40s `tput sgr0` |\n" "$@"
}
# Show text with success color
mega_showsuccess()
{
printf "|`tput bold``tput setf 2` %-40s `tput sgr0` |\n" "$@"
}
# Show the banner
mega_banner()
{
echo "+-------------------------------------------+"
}
# Ask for the pub key version
mega_getKeyVersion()
{
mega_show "The key have a specific name ?"
echo -n "NAME [default empty] : "
read keyVersion
}
# Check network by asking script
mega_checkNet()
{
status=$(wget -qO- conntest.megamov.fr)
if [ "$status" = "ok" ] ; then
echo 0
return 0
fi
echo 1
return 1
}
# CREATE .SSH DIR IF NOT EXIST
mega_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
mega_checkPubKey()
{
wget -qO- $keyURL
if [ $? = 0 ] ; then
echo 0
return 0
fi
echo 1
return 1
}
# ADD public key in .authorized_key
mega_addPubKey()
{
echo
echo "#Nicolas.P @MeGaMoV SSH Key $keyVersion"
ssh_key=$(wget -qO- $keyURL)
echo $ssh_key
} >> $ssh_keys_dir/authorized_keys
################################################################################
########## MAIN SCRIPT ##########
################################################################################
mega_banner
mega_show
mega_show " Adding SSH Key of Nicolas @MeGaMoV"
mega_show " v$script_version"
mega_show
mega_getKeyVersion
keyURL="$netBaseDir$keyVersion"
# Checking network
res=$(mega_checkNet)
if [ "$res" = 1 ]; then
mega_show
mega_show
mega_showerror "[+] Err: Can't access megamov's server."
mega_show
mega_banner
exit 1
fi
# Create .ssh DIR
res=$(mega_sshDir)
if [ "$res" = 1 ]; then
mega_show
mega_show
mega_showerror "[+] Err: Can't create ssh folder"
mega_showerror " $ssh_keys_dir"
mega_show
mega_banner
exit 1
fi
# Check if the key exist in server
res=$(mega_checkPubKey)
if [ "$res" = 1 ]; then
mega_show
mega_show
mega_showerror "[+] Error: Unable do get the public key"
mega_show
mega_showerror "Please : Check if the NAME you entered"
mega_showerror " is a valid MeGaMoV Key"
mega_show
mega_showerror "NOTE : Leave empty if you don't know"
mega_show
mega_banner
exit 1
fi
# Adding the public key
mega_addPubKey
# Key the IP
IP=$(wget -qO- ip.megamov.fr)
mega_show
mega_show
mega_showsuccess " SSH Public key added !"
mega_showsuccess " Access available from :"
mega_show
mega_showsuccess "$USER@$IP"
mega_banner
exit 0