If you use Linux as your work environment, this article is a good news! Because I often use SSH to log on to other hosts during my study/work (especially recently Playing Raspberry Pi), and every time I use SSH to log on, I need to enter a long string, which makes me very annoying, so I wrote this script to simplify my SSH process.
Use this script. If you want to log on to a machine, you only need to use $ ~ /Easy_ssh.sh and select one option to log on. Even if there is no machine option for logon, I only need to enter and save it once, and then I can log on to this computer quickly.
#!/bin/bash# (C) 2014 Yunlong Zhou <[email protected]> # Under licence GPLv2# File : easy_ssh.sh # Introduction: # This script is using for ssh Linux hosts quickly# Advises:#Copy this script to a easy use place, such as ~, so that you can use it very easily.#Besides, this script will create a ".hostlist" local file to temp the hosts, so if you want to move the script, remembermove the temp file too.# Last Modify:#2014-7-2# Useage :#$ /bin/bash easy_ssh.sh (chmod +x easy_ssh.sh ; ./easy_ssh.sh)#Now you have hosts on your config:# 0 : Inupt other host manually# 1 : [email protected]# 2 : [email protected]#======================================#Please input your choice: 1#You choose 1, and Now I will try to connect [email protected]#[email protected]'s password: # ...function copy_array #from_name #to_name{from_name=$1from_len=${#from_name[@]}to_name=$2to_len=${#to_name[@]}for((i=1; i<$from_len; i++))do$to_name[$to_len]=${$from_name[$i]}((to_len++))done}function get_current_hosts # array_name{copy_array $1 array_nameecho ${array_name[*]}ls .hostlist &> /dev/nullif [ $? -ne 0 ]; thentouch .hostlistelsewhile read READLINEdoif [ $READLINE != ' ' ];thenlen=${#array_name[@]}#echo -e "\t $len $READLINE"array_name[$len]=$READLINEfidone < .hostlistfi}function store_hostmessage #user_input{if read -t 30 -p "Do you want to add this host message to config file?[Y/n]"then store_flag=$REPLYcase "$store_flag" in"Y"|"YES"|"YEs"|"Yes"|"yes"|"yeS"|"yES"|"ye")echo $1 >>.hostlistecho "Yep, Store complete";;"N"|"NO"|"No"|"n"|"no"|"nO")echo "Ok, do not store, see you later" ;;*)echo "Can't recognize, won't store, please input right options next time";;esacelse echo -e "\nOh, you are too slow or away" exit -1 fi}function get_user_manual_input{if read -t 180 -p "Please input the host manually(in [email protected]_addr format): " then user_input=$REPLY user_name=${user_input%@*}user_ip=${user_input##*@}ping -c 1 $user_ip &> /dev/nullif [ $? -eq 0 ]; thenssh $user_inputstore_hostmessage $user_inputelseecho "The IP address is not accessable, please check again"fi else echo -e "\nOh, you are too slow or away" exit -1 fi}function get_user_input #array_name{copy_array $1 array_namearray_len=${#array_name[@]}if read -t 40 -p "Please input your choice: "then user_input=$REPLY if [ $user_input == "0" ] ;thenecho "You choose 0"get_user_manual_inputelif [ $(($user_input-0)) -gt 0 -a $(($user_input-$array_len)) -le 0 ]; thenecho "You choose $user_input, and Now I will try to connect ${array_name[$user_input-1]}"ssh ${array_name[$(($user_input-1))]}elseecho "Please input right choice"fielse echo -e "\nYou are too slow!" exit -1fi}function echo_hosts_get_input #array_name{copy_array $1 array_namearray_len=${#array_name[@]}if [ $array_len -ne 0 ]; thenecho "Now you have $arrar_len hosts on your config:"cur=1echo -e "\t 0 : Inupt other host manually"for i in ${array_name[*]}doecho -e "\t $cur : $i"((cur++))doneecho "======================================"get_user_input ${array_name[*]}elseecho -e "Sorry that have no hosts message"get_user_manual_inputfiecho ""}host=()get_current_hosts ${host[*]}echo_hosts_get_input ${host[*]}
Current shortcomings:
Currently, only common SSH logon is supported.-P port logon is not required for input. This will be solved next time! In addition, I personally feel that using shell scripts for implementation is not very complex, but using python for implementation should be more convenient and convenient.
Hope you have a pleasant experience.