Shell 脚本小试牛刀(5) -- 超便捷脚本之高速ssh 登录其它主机
假设你也是以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=$1
from_len=${#from_name[@]}
to_name=$2
to_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_name
echo ${array_name[*]}
ls $host_file &> /dev/null
if [ $? -ne 0 ]; then
touch $host_file
else
while read READLINE
do
if [ $READLINE != ' ' ];then
len=${#array_name[@]}
#echo -e "\t $len $READLINE"
array_name[$len]=$READLINE
fi
done < $host_file
fi
}
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=$REPLY
case "$store_flag" in
"Y"|"YES"|"YEs"|"Yes"|"y"|"yes"|"yeS"|"yES"|"ye")
echo $1 >>$host_file
echo "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";;
esac
else
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/null
if [ $? -eq 0 ]; then
ssh $user_input
store_hostmessage $user_input $host_file
else
echo "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_name
array_len=${#array_name[@]}
if read -t 40 -p "Please input your choice: "
then
user_input=$REPLY
if [ $user_input == "0" ] ;then
echo "You choose 0"
get_user_manual_input
elif [ $(($user_input-0)) -gt 0 -a $(($user_input-$array_len)) -le 0 ]; then
echo "You choose $user_input, and Now I will try to connect ${array_name[$user_input-1]}"
ssh ${array_name[$(($user_input-1))]}
else
echo "Please input right choice"
fi
else
echo -e "\nYou are too slow!"
exit -1
fi
}
function echo_hosts_get_input #array_name
{
copy_array $1 array_name
array_len=${#array_name[@]}
if [ $array_len -ne 0 ]; then
echo "Now you have $arrar_len hosts on your config:"
cur=1
echo -e "\t 0 : Inupt other host manually"
for i in ${array_name[*]}
do
echo -e "\t $cur : $i"
((cur++))
done
echo "======================================"
get_user_input ${array_name[*]} $host_file
else
echo -e "Sorry that have no hosts message"
get_user_manual_input
fi
echo ""
}
host=()
path=${0%/*}
host_file=$path/.hostlist
echo $host_file
get_current_hosts ${host[*]} $host_file
echo_hosts_get_input ${host[*]} $host_file眼下的不足:
当前仅仅支持普通的ssh 登录,对于输入须要 -p port登录部分还是没有採取比較好的处理。下次会解决!
此外,个人感觉使用shell 脚本实现尽管不是非常复杂,可是使用python 实现应该会更加方便快捷。
希望你使用的愉快。

=================================
更新日志: 2014.7.12
之前公布的版本号中。会直接在你使用脚本时所在的位置下查找.hostlist 文件。可是这就违背了我们随时随地登录的原则。所以做了对应改动。代码中已直接改正。

浙公网安备 33010602011771号