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.
161 lines
3.0 KiB
161 lines
3.0 KiB
#!/bin/bash
|
|
|
|
# VARIABLES
|
|
script_version="1.3"
|
|
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 "+-------------------------------------------+"
|
|
}
|
|
|
|
# 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
|
|
|
|
keyVersion=$1
|
|
keyURL="$netBaseDir$keyVersion"
|
|
|
|
# Checking network
|
|
res=$(mega_checkNet)
|
|
if [ "$res" = 1 ]; then
|
|
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_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_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_showsuccess " SSH Public key added !"
|
|
mega_showsuccess " Access available from :"
|
|
mega_show
|
|
mega_showsuccess "$USER@$IP"
|
|
mega_banner
|
|
exit 0
|
|
|