看效果!

脚本内容:

#!/bin/bash
# -*- shell scripts -*-
# Version: 4.1v
# Latest revision time: 2023/04/04
# Amendment No:148
set -

host_lock='False'   #[True] [False] [default: False]

[ -f /etc/profile ] && source /etc/profile
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
LANG=en_US.UTF-8

LOGIN_HOME="${HOME}/.login/"
Empty_dir="${LOGIN_HOME}/null/"
Host_file="${LOGIN_HOME}/.data/.UserLoginhost.file"
Host_file_ts="${Empty_dir}/hostinfo_transfer_${RANDOM}_file"    # .UserLoginhost.file transfer station file(unlock)
login_shell_scripts="/${Empty_dir}/.login_${RANDOM}.sh"
add_hosts_scripts="/${Empty_dir}/.add_hosts_${RANDOM}.sh"

Parameter_num="$#"
Parameter1="$1"
Parameter2="$2"

# Clear Residual file scripts (Long term unchanged)
Crsf="${Empty_dir}/.clear_${RANDOM}_file.sh"

# Getting Password Temporary File (Long term unchanged)
Login_temporary_file_old="${Empty_dir}/.Login_server_${RANDOM}_temporary_1_file"
Login_temporary_file_new="${Empty_dir}/.Login_server_${RANDOM}_temporary_2_file"

trap "signout_clear" 2 3 6

function init(){
    local cmd_path=

    if [ ! -d ${LOGIN_HOME} ];then
        mkdir -p ${LOGIN_HOME}
    fi

    if [ ! -d ${Empty_dir} ]; then
        mkdir -p ${Empty_dir}
    fi

    if [ ! -f ${Host_file_ts} ]; then
        touch ${Host_file_ts}
        chmod 600 ${Host_file_ts}
    fi

    if [ ! -f ${Host_file} ] && [ ! -f ${Host_file}.lock ]; then
        mkdir -p $(dirname ${Host_file})
        touch ${Host_file}
        chmod 600 ${Host_file}
    fi

    if [ ! -f ${login_shell_scripts} ]; then
        write_exp
    fi

    # cmd Is there any existence
    cmd_path="$(which sshpass 2>/dev/null)"
    if [[ -z ${cmd_path} || ! -f ${cmd_path} && ! -x ${cmd_path} ]]; then
        echo -e "\033[33mINFO\033[0m: [ sshpass ] command Can't find"
        exit 
    fi

    cmd_openssl=$(which openssl &>/dev/null;echo $?)
    if [ ${cmd_openssl} -ne 0 ]; then
        echo -e "\033[33mINFO\033[0m: [ openssl ] command Can't find"
        exit 
    fi
}

function generate_userloginhost_transfer_station_file(){
    unlock_host_file
    if [ -f ${Host_file} ]; then
        egrep '^[0-9]|^[ ]+[0-9]' ${Host_file}|awk 'BEGIN{num=000}{num++}{print num,$1,$2,$3,"none",$5}'|column -t >${Host_file_ts}
        if [ ! -f ${Host_file_ts} ]; then
            generate_userloginhost_transfer_station_file
        fi
    else
        echo -e "\033[31mError\033[0m: userloginfile no success Write in, Please check."
        exit 12
    fi
    lock_host_file
}

function lock_host_file(){
    if [ "${host_lock:-False}" == 'False' ]; then
        return
    fi

    if [ -f ${Host_file} ] && [ ! -f ${Host_file}.lock ]; then
        cat ${Host_file} | openssl des3 -e -k 'DmXy666' >${Host_file}.lock 2>/dev/null
        if [ -f ${Host_file}.lock ]; then
            chmod 600 ${Host_file}.lock
            rm -f ${Host_file} 2>/dev/null
        else
            echo 'Ignorable warnings: host file lock up false.'
        fi
    elif [ -f ${Host_file}.lock ] && [ ! -f ${Host_file} ]; then
        chmod 600 ${Host_file}.lock
    elif [ -f ${Host_file}.lock ] && [ -f ${Host_file} ]; then
        rm -f ${Host_file}.lock 2>/dev/null
        cat ${Host_file} | openssl des3 -e -k 'DmXy666' >${Host_file}.lock 2>/dev/null
        if [ -f ${Host_file}.lock ]; then
            chmod 600 ${Host_file}.lock
            rm -f ${Host_file} 2>/dev/null
        else
            echo 'Ignorable warnings: host file lock up false.'
        fi
    else
        init
    fi

}

function unlock_host_file(){
    if [ "${host_lock:-False}" == 'False' ]; then
        if [ -f ${Host_file}.lock ]; then
            rm -f ${Host_file} 2>/dev/null
            if cat ${Host_file}.lock | openssl des3 -d -k 'DmXy666' >${Host_file} 2>/dev/null; then
                chmod 600 ${Host_file}
                rm -f ${Host_file}.lock 2>/dev/null
            fi
        fi
        return
    fi

    if [ -f ${Host_file}.lock ] && [ ! -f ${Host_file} ]; then
        cat ${Host_file}.lock | openssl des3 -d -k 'DmXy666' >${Host_file} 2>/dev/null
        if [ -f ${Host_file} ]; then
            chmod 600 ${Host_file}
            rm -f ${Host_file}.lock 2>/dev/null
        else
            echo -e "\033[31mError: host file Unlock False. Please check.\033[0m"
            exit 10
        fi
    elif [ -f ${Host_file} ] && [ ! -f ${Host_file}.lock ]; then
        chmod 600 ${Host_file}
    elif [ -f ${Host_file}.lock ] && [ -f ${Host_file} ]; then
        rm -f ${Host_file} 2>/dev/null
        cat ${Host_file}.lock | openssl des3 -d -k 'DmXy666' >${Host_file} 2>/dev/null
        if [ -f ${Host_file} ]; then
            chmod 600 ${Host_file}
            rm -f ${Host_file}.lock 2>/dev/null
        else
            echo -e "\033[31mError: host file Unlock False. Please check.\033[0m"
            exit 10
        fi
    else
        init
    fi

}

function add_hosts(){
unlock_host_file

cat >${add_hosts_scripts} << EOF
#!/bin/bash

source ${Login_temporary_file_new}

home_dir=\$(echo ~)
Passwd=\$(echo \$Passwd|sed "s#~#\${home_dir}#")

iP_num=\$(grep -E '^[0-9]|^[ ]+[0-9]' ${Host_file} 2>/dev/null | awk '{print \$1}' |grep -w "\${Host}"|grep '[0-9]'|wc -l|xargs)

if [ \${iP_num} -eq 0 ];then
    if [ ! -f "\${Passwd}" ];then
        sshpass -p "\${Passwd}" ssh -o 'StrictHostKeyChecking no' -p\${Port} \${User}@\${Host} 'hostname' &>/dev/null
        RETURN=\$?
    else
        ssh -o 'StrictHostKeyChecking no' -i "\${Passwd}" -p\${Port} \${User}@\${Host} 'hostname' &>/dev/null
        RETURN=\$?
    fi

    if [ "\${RETURN}" -eq 0 ]; then
        echo "\${Host}    \${User}    \${Port}    \${Passwd}    \${Server_tag} " >>${Host_file}
    else
        #echo -e "\033[33mWARNING: The host address was not successfully written to the file\033[0m"
        echo -e "\033[31mERROR: login false, Please check [ user,host,port,passwd ] Is it correct\033[0m"
        exit 10
    fi

fi

rm -f \$0 2>/dev/null
exit 0
EOF
chmod 700 ${add_hosts_scripts} 2>/dev/null
/bin/sh ${add_hosts_scripts} 2>/dev/null
lock_host_file
}

function write_exp(){

cat > ${login_shell_scripts} << EOF
#!/bin/bash

source ${Login_temporary_file_new}
rm -f ${Login_temporary_file_new}

home_dir=\$(echo ~)
Passwd=\$(echo \$Passwd|sed "s#~#\${home_dir}#")

if [ ! -f "\${Passwd}" ];then
    sshpass -p "\${Passwd}" ssh -o 'StrictHostKeyChecking no' -p\${Port} \${User}@\${Host}
else
    ssh -o 'StrictHostKeyChecking no' -i "\${Passwd}" -p\${Port} \${User}@\${Host}
fi

rm -f \$0 2>/dev/null
exit 0
EOF
chmod 700 ${login_shell_scripts} 2>/dev/null

}

function service_info(){
local temporary_file
local num= link= Y=

unlock_host_file
generate_userloginhost_transfer_station_file
clear 2>/dev/null
num=$(cat ${Host_file_ts} 2>/dev/null|wc -l)

echo -e 'ID  登陆IP  登陆用户  SSH端口  备注' >/tmp/.dloginhostinfofile_t1.txt

cat >> /tmp/.dloginhostinfofile_t1.txt << END
$(awk '{print $1,$2,$3,$4,$6}' ${Host_file_ts})
END
cat /tmp/.dloginhostinfofile_t1.txt|column -t >/tmp/.dloginhostinfofile.txt

link=$(cat /tmp/.dloginhostinfofile.txt|wc -l)
link=$(( ${link} - 1 ))
Y=$(( $(stty size|awk '{print $2}') - $(head -1 /tmp/.dloginhostinfofile.txt|wc -c) + 11 ))

echo -e "\033[42;30m$(head -1 /tmp/.dloginhostinfofile.txt)$(printf "%${Y}s" ' ')\033[0m"
echo -e "\033[37m$(tail -${link}  /tmp/.dloginhostinfofile.txt)\033[0m"
echo '----------------------------------------------------------------------------'
echo -e "\033[46;30m功能选项:OP, 机器数量:${link} \033[0m"
lock_host_file
}

function info(){
unlock_host_file
Host=`grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $1}'`
User=`grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $2}'`
Port=`grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $3}'`
Passwd=`grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $4}'`

echo "Host_file=${Host_file}\nIP=${IP}" >${Login_temporary_file_old}
cat >> ${Login_temporary_file_old} << EON
Host=$(grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $1}')
User=$(grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $2}')
Port=$(grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $3}')
Passwd="$(grep -vE '^#|^$' ${Host_file} | grep -w "^${IP}" | awk '{print $4}'|sed 's#\$#\\$#g')"
EON
cat ${Login_temporary_file_old} 2>/dev/null >${Login_temporary_file_new}
rm -f ${Login_temporary_file_old} 2>/dev/null
lock_host_file
}

function info_2(){
    sum=0
    while [ ${sum} -lt 3 ]
    do
      read -t120 -p '               server user: ' User
      if [ -z ${User} ];then
          sum=$((sum+=1))
          echo -e '\033[33mWarning: Please enter the UserName to log in... \033[0m'
          if [ ${sum} -ge 3 ];then
            exit 3
          fi
        else
          echo "User=${User}" >>${Login_temporary_file_new}
          break
      fi
    done

    sum=0
    while [ ${sum} -lt 3 ]
    do
      read -t120 -p '               server port: ' Port
      if [ -z ${Port} ];then
          sum=$((sum+=1))
          echo -e '\033[33mWarning: Please enter the correct login Port... \033[0m'
          if [ ${sum} -ge 3 ];then
            exit 3
          fi
        else
          echo "Port=${Port}" >>${Login_temporary_file_new}
          break
      fi
    done

    sum=0
    while [ ${sum} -lt 3 ]
    do
      read -t120 -sp '             server passwd: ' Passwd
      if [ -z ${Passwd} ];then
          sum=$((sum+=1))
          echo -e '\033[33mWarning: Please enter a Password.... \033[0m'
          if [ ${sum} -ge 3 ];then
             exit 3
          fi
        else
          echo "Passwd=${Passwd}" >>${Login_temporary_file_new}
          break
      fi
    done
}

function input_tag(){
    sum=0
    while [ ${sum} -lt 1 ]
    do
      read -t120 -p '                server tag: ' Server_tag
      if [ -z ${Server_tag} ];then
          sum=$((sum+=1))
          echo -e "\033[33minfo: You did not define a label for your server\033[0m"
        else
          sum=$((sum+=1))
          echo -e "\033[33minfo: Your server define tag is [ ${Server_tag} ] \033[0m"
          echo "Server_tag=${Server_tag}" >>${Login_temporary_file_new}
      fi
    done
}

function operational_options_delhost(){
    local delhosts host_num= IP=
    clear
    generate_userloginhost_transfer_station_file
    service_info
    while true;do
        read -t60 -p "$(echo -e "\033[33mDelete host [ exit/IP ]: \033[0m")" delhosts
        if [ -z ${delhosts} ];then
            operational_options_delhost
        elif [ "${delhosts}" == 'q' ] || [ "${delhosts}" == 'exit' ] || [ "${delhosts}" == 'quit' ]; then
            clean_up_battlefields
            echo 'Wish you a happy life. bye~'
            exit 0
        elif [ "${delhosts}" == 'OP' ]; then
            operational_options
        else
            host_num=$(grep -w "${delhosts}" ${Host_file_ts} 2>/dev/null|wc -l)
            if [ ${host_num} -eq 1 ]; then
                unlock_host_file
                grep -vw "${delhosts}" ${Host_file} >${Empty_dir}/temporary_file.txt
                cat ${Empty_dir}/temporary_file.txt | column -t >${Host_file}
                rm -f ${Empty_dir}/temporary_file.txt
                lock_host_file
            fi
            operational_options_delhost
        fi
    done
    
}

function options_output_files(){
local OOF=
OOF="${Empty_dir}/options_output_files_${RANDOM}.txt"
clear
cat > ${OOF} <<EOF
$(
echo
echo -e "Host List Options Operation:"
echo -e '-------------------------------------------------------------'
echo -e " \033[36m1\033[0m - delete appoint hosts."
echo -e '-------------------------------------------------------------'
echo
)
EOF
cat ${OOF} 2>/dev/null
rm -f ${OOF} 2>/dev/null
}

function operational_options(){
    options_output_files
    while true
    do
        read -t60 -p  "Operational options: " Option
        if [ -z ${Option} ];then
            options_output_files
        else
            if [ "${Option}" == 'delhost' ] || [ "${Option}" == '1' ]; then
                operational_options_delhost
            elif [ "${Option}" == 'q' ] ||  [ "${Option}" == 'exit' ] || \
                [ "${Option}" == 'quit' ] || [ "${Option}" == '99' ]; then
                clean_up_battlefields
                echo 'Wish you a happy life. bye~'
                exit 0
            else
                operational_options
            fi
        fi
    done
}

function two_screening_ip_output(){
two2_menu="${Empty_dir}/two_level_menu_${RANDOM}.txt"
echo -e 'ID  登陆IP  登陆用户  SSH端口  备注' >${Empty_dir}/two1_screening_ip_output.txt
for i in "${IP_input[@]}"
do
    egrep '^[0-9]' ${Host_file_ts} 2>/dev/null|awk '{print $2,$3,$4,$6}'|grep -w "${i}"
done|awk 'BEGIN{num=000}{num++}{print num,$1,$2,$3,$4}' >> ${Empty_dir}/two1_screening_ip_output.txt
column -t ${Empty_dir}/two1_screening_ip_output.txt 2>/dev/null >${Host_file_ts}_tmp


link=$(cat ${Host_file_ts}_tmp|wc -l)
link=$(( ${link} - 1 ))
clear
Y=$(( $(stty size|awk '{print $2}') - $(head -1 ${Host_file_ts}_tmp|wc -c) + 11 ))
echo -e "\033[42;30m$(head -1 ${Host_file_ts}_tmp)$(printf "%${Y}s" ' ')\033[0m"
echo -e "\033[37m$(tail -${link}  ${Host_file_ts}_tmp)\033[0m"
echo '----------------------------------------------------------------'
echo -e "\033[46;30m功能选项:OP, 机器数量:${link} \033[0m"
}

function ip_tag_input_verification(){
    local id_bit= id_num=

    if [ -s ${Host_file_ts}_tmp ]; then
        filter_display_file="${Host_file_ts}_tmp"
    else
        filter_display_file="${Host_file_ts}"
    fi

    id_bit=($(awk '{print $1}' ${filter_display_file} 2>/dev/null | grep -E '[0-9]'))
    id_num=$(printf '%s\n' ${id_bit[*]}|grep -w "${Number}"|wc -l)
    if [ "${id_num}" -eq 1 ]; then
        IP="$(grep -w "^${Number}" ${filter_display_file}|awk '{print $2}')"
        New_host=1
        IP_1=${New_host}
        sum=${nnum}
    else
        IP_input_num=$(awk '{print $2,$6}' ${filter_display_file} 2>/dev/null|grep "${Number}"|egrep '[a-z,A-Z,0-9]'|wc -l)
        if [ ${IP_input_num} -eq 0 ];then
            IP_input=(`awk '{print $2,$6}' ${Host_file_ts} 2>/dev/null|grep "${Number}"|awk '{print $1}'`)
            IP_input_num=$(awk '{print $2,$6}' ${Host_file_ts} 2>/dev/null|grep "${Number}"|egrep '[a-z,A-Z,0-9]'|wc -l)
            if [ ${IP_input_num} -eq 0 ]; then
                New_host=2
                IP_1=${New_host}
                sum=${nnum}
                sum=${nnum}
            else
                two_screening_ip_output
            fi
        elif [ ${IP_input_num} -ge 2 ]; then
            IP_input=(`awk '{print $2,$6}' ${Host_file_ts} 2>/dev/null|grep "${Number}"|awk '{print $1}'`)
            two_screening_ip_output
            num=$(( ${#IP_input[*]} - 1 ))
            for i in $(seq 0 ${num})
            do
                if [ "${IP_input[$i]}" = "${Number}" ]; then
                    IP_1=1
                    IP=${Number}
                    sum=${nnum}
                else
                    New_host=2    # user input ip or tag False
                    IP_1=${New_host}
                    continue
                fi
                sum=$((sum+=1))
            done
        elif [ ${IP_input_num} -eq 1 ]; then
            IP_1=${IP_input_num}
            IP=$(egrep '^[0-9]' ${filter_display_file} | awk '{print $2,$6}' |grep "${Number}"|awk '{print $1}')
            sum=${nnum}
        fi
    fi
}

function input_ip(){
    local OP
    sum=0
    nnum=3
    while [ ${sum} -lt ${nnum} ]
    do
        read -t60 -p  "login server [ ID/IP/TAG ]: " Number
        if [ -z ${Number} ];then
            /bin/cp -a ${Host_file_ts} ${Host_file_ts}_tmp 2>/dev/null
            ip_tag_input_verification
        else
            if [ "${Number}" == 'q' ] || [ "${Number}" == 'quit' ] || [ "${Number}" == 'exit' ]; then
                clean_up_battlefields
                exit 0
            elif [ "${Number}" == 'OP' ]; then
                operational_options
            else
                ip_tag_input_verification
            fi
        fi
    done

    if [ $IP_1 -eq 1 ];then
      info
    elif [ $IP_1 -eq 2 ];then
      Host=$Number
      echo "Host=${Host}" >${Login_temporary_file_new}
      input_tag
      info_2
    elif [ $IP_1 -eq 3 ]; then
      echo "bye~!"
      exit 9
    fi
}

function login_server(){
    rm -f ${Empty_dir}/add_host.txt
    clean_up_battlefields
    output_login_info

    add_hosts
    exec /bin/sh ${login_shell_scripts} 2>/dev/null
}

function clean_up_battlefields(){
lock_host_file
cat > ${Crsf} << EOF
#!/bin/bash
rm -f /tmp/.dloginhostinfofile.txt
rm -f /tmp/.dloginhostinfofile_t1.txt
rm -f ${Empty_dir}/two_screening_ip_output.txt
find ${Empty_dir}/ -type f -amin +10 -cmin +10 -name ".*" -delete
find ${Empty_dir}/ -type f -amin +10 -cmin +10 -delete
find /tmp/ -type f -mtime +1 -name ".login_*.exp" -delete
rm -f $two2_menu

sleep 2
rm -f ${Host_file_ts}
rm -f ${Host_file_ts}_tmp
rm -f ${Login_temporary_file_new}
rm -f ${Login_temporary_file_new}
rm -f ${login_shell_scripts} 2>/dev/null
rm -f ${Crsf} 2>/dev/null
rm -f ${Empty_dir}/add_host.txt
EOF
nohup /bin/sh ${Crsf} &>/dev/null &
}

function signout_clear(){
    clean_up_battlefields
    echo -e "\nWish you a happy life, bay~"
    exit 0
}

function output_login_info(){
    clear 2>/dev/null
    output_h=$(awk -F= '/Host=/{print $NF}' ${Login_temporary_file_new})
    output_u=$(awk -F= '/User=/{print $NF}' ${Login_temporary_file_new})
    output_t="$(date +%F_%H:%M)"
    output_ht="$(grep -v '^#|^$' ${Host_file_ts} 2>/dev/null|grep -w "${output_h}"|awk '{print $6}')"
    [ -z "${output_ht}" ] && output_ht="${Server_tag:-none}"
    echo 'login information: '
    echo '  +-----------------------------------'
    echo -e "  | Login server: \033[32m${output_h}\033[0m"
    echo -e "  |   Login user: \033[32m${output_u}\033[0m"
    echo -e "  |   Login date: \033[32m${output_t}\033[0m"
    echo -e "  |    Login tag: \033[32m${output_ht}\033[0m"
    echo '  +-----------------------------------'
    echo -e "\033[44;37m Y(^_^)Y \033[0m Login server, please wait...... "
}


function main(){
    if [ ${Parameter_num} -eq 0 ]; then
        init
        service_info
        input_ip
        login_server
    elif [ ${Parameter_num} -eq 1 ] && [ "${Parameter1}" == '--unlock-file' ]; then
        host_lock=True
        unlock_host_file
        exit
    else
        echo -e "\033[31mParameter error, bay~\033[0m"
        exit 1
    fi
}
main
exit 0

服务器信息内容填写

# 在脚本的目录下面创建所需文件

mkdir -p  .login/.data

# 创建IP地址文件
touch .login/.data/.UserLoginhost.file

# 模版如下
123.66.xxx.xxx root 22 Password 国内云主机