Shell 指令碼小試牛刀(5) -- 超便捷指令碼之快速ssh 登入其他主機

來源:互聯網
上載者:User

標籤:shell 指令碼   快速   ssh 登入主機   

如果你也是以Linux 為工作環境的童鞋,那麼此文真是捷報!因為我的學習/工作中(特別是最近玩耍樹莓派)經常會使用到ssh 登入其他主機,而每次使用ssh 登入都需要輸入老長一大串讓我很煩,所以我寫了這個指令碼來簡化我的ssh 過程。

使用這個指令碼,如果我想登入一台機器,我只要使用 $~/easy_ssh.sh 然後選擇一項就可登入,即使當前沒有我要登入的機器選項,我只要輸入一次並儲存,以後我也可以很快的登入到這台電腦。



#!/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[*]}

目前的不足:

當前只支援普通的ssh 登入,對於輸入需要 -p 連接埠登入部分還是沒有採取比較好的處理,下次會解決!此外,個人感覺使用shell 指令碼實現雖然不是很複雜,但是使用python 實現應該會更加方便快捷。


希望你使用的愉快。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.