Shell 脚本小试牛刀(5) -- 超便捷脚本之快速ssh 登录其他主机

来源:互联网 发布:网络连接受限制怎么办 编辑:程序博客网 时间:2024/06/05 02:26

如果你也是以Linux 为工作环境的童鞋,那么此文真是捷报!因为我的学习/工作中(特别是最近玩耍树莓派)经常会使用到ssh 登录其他主机,而每次使用ssh 登录都需要输入老长一大串让我很烦,所以我写了这个脚本来简化我的ssh 过程。

使用这个脚本,如果我想登录一台机器,我只要使用 $~/easy_ssh.sh 然后选择一项就可登录,即使当前没有我要登录的机器选项,我只要输入一次并保存,以后我也可以很快的登录到这台电脑。

#!/bin/bash# (C) 2014 Yunlong Zhou <reaper888@yeah.net>      # 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 : pi@192.168.2.226# 2 : long@192.168.2.222#======================================#Please input your choice: 1#You choose 1, and Now I will try to connect pi@192.168.2.226#pi@192.168.2.226'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 $host_file &> /dev/nullif [ $? -ne 0  ]; thentouch $host_fileelsewhile read READLINEdoif [ $READLINE != ' ' ];thenlen=${#array_name[@]}#echo -e "\t $len $READLINE"array_name[$len]=$READLINEfidone < $host_filefi}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"|"y"|"yes"|"yeS"|"yES"|"ye")echo $1 >>$host_fileecho "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 user@IP_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_input $host_fileelseecho "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_input elif [ $(($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[*]} $host_file elseecho -e "Sorry that have no hosts message"get_user_manual_inputfiecho ""}host=()path=${0%/*}host_file=$path/.hostlistecho $host_fileget_current_hosts ${host[*]} $host_fileecho_hosts_get_input ${host[*]} $host_file


目前的不足:

当前只支持普通的ssh 登录,对于输入需要 -p 端口登录部分还是没有采取比较好的处理,下次会解决!此外,个人感觉使用shell 脚本实现虽然不是很复杂,但是使用python 实现应该会更加方便快捷。


希望你使用的愉快。大笑


=================================

更新日志: 2014.7.12

之前发布的版本中,会直接在你使用脚本时所在的位置下查找.hostlist 文件,但是这就违背了我们随时随地登录的原则,所以做了相应修改。代码中已直接改正!

2 0
原创粉丝点击