#!/bin/bash

Green_font="\033[32m"
Yellow_font="\033[33m"
Red_font="\033[31m"
Font_suffix="\033[0m"
Info="${Green_font}[Info]${Font_suffix}"
Error="${Red_font}[Error]${Font_suffix}"
Tips="${Green_font}[Tips]${Font_suffix}"

checkRootPermit() {
    [[ $EUID -ne 0 ]] && echo -e "${Error} 请使用sudo/root权限运行本脚本" && exit 1
}

startReplacing(){
	if [[ -f /etc/redhat-release ]]; then
		release="centos"
        c
	elif cat /etc/issue | grep -q -E -i "debian"; then
		release="debian"
        d
	elif cat /etc/issue | grep -q -E -i "ubuntu"; then
		release="ubuntu"
        d
	elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
		release="centos"
        c
	elif cat /proc/version | grep -q -E -i "debian"; then
		release="debian"
        d
	elif cat /proc/version | grep -q -E -i "ubuntu"; then
		release="ubuntu"
        d
	elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
		release="centos"
        c
    fi
	bit=`uname -m`
}

c(){
    yum install curl -y
    getKey
    systemctl restart sshd
    systemctl restart ssh
}

d(){
    apt-get install curl -y
    getKey
    service sshd restart
    service ssh restart
}

getKey(){
    cd ~
    mkdir .ssh
    cd .ssh
    curl https://github.com/SplendorAnLin.keys > authorized_keys
    chmod 700 authorized_keys
    cd ../
    chmod 600 .ssh
    cd /etc/ssh/

    sed -i "/PasswordAuthentication no/c PasswordAuthentication no" sshd_config
    sed -i "/RSAAuthentication no/c RSAAuthentication yes" sshd_config
    sed -i "/PubkeyAuthentication no/c PubkeyAuthentication yes" sshd_config
    sed -i "/PasswordAuthentication yes/c PasswordAuthentication no" sshd_config
    sed -i "/RSAAuthentication yes/c RSAAuthentication yes" sshd_config
    sed -i "/PubkeyAuthentication yes/c PubkeyAuthentication yes" sshd_config
}

checkRootPermit
startReplacing