mac 利用 sshpass 自动登录
mac 利用 sshpass + 配置文件 实现自动登录
使用方式 https://github.com/vipzhicheng/go 参见此项目
其实原理也就是 脚本 读取配置文件 匹配 参数或者 ip 调用 ssh 命令 简单 mark下
按照链接方式配置好之后
shell 中 执行 go (参数)
例如 我配置
# IP USER:PASS Label
120.27.96.210(你的 ip) 你的用户名:你的密码 label:210
执行 go 210
直接 ssh 登录到 目标 ip
#!/bin/bash
# Define config directory and config path
GO_HOME=~
AUTHFILE=$GO_HOME/.ssh/pass/.go.conf
# Parse options
while getopts gh ARGS
do
case $ARGS in
g)
extra_options="-D7070"
shift
;;
h)
echo "usage: go [-gh] foo bar"
exit 0;
;;
*)
echo "Unknow option: $ARGS"
echo "usage: go [-gh] foo bar"
exit 1;
;;
esac
done
# Config search function
# Support AND logic, separated by blank char
GREP()
{
if [ -z "$*" ]; then
var="^[^#].*"
tmp_var=`cat $AUTHFILE | grep -i $var`
echo "$tmp_var"
return
fi
index=1;
for var in $@
do
if [[ "$var" =~ ^- ]];then
index=$((index+1));
continue
fi
if [ "$var" = "${!index}" ];then
var="^[^#].*$var.*"
tmp_var=`grep -i $var $AUTHFILE`
else
var="^[^#].*$var.*"
tmp_var=`echo "$tmp_var" | grep -i $var`
fi
done
echo "$tmp_var"
}
# Get choice
GET_CHAR()
{
read choice
echo $choice;
}
# Support input keywords by arguments or read from command line input
if [ -z $1 ];then
echo -n "Please input the server IP or Label: "
read target
else
target=$@
fi
# Parse config search result
count=`GREP $target | wc -l`
targetfullname=`GREP $target | awk '{print $1}' | awk -F ':' '{print $1}'`
port=`GREP $target | awk '{print $1}' | awk -F ':' '{if ($2 > "") {print $2} else {print 22}}'`
passwd=`GREP $target | awk '{print $2}' | awk -F ':' '{if ($2 > "") {print $2} else {print "-"}}'`
sshkey=`GREP $target | awk '{print $2}' | awk -F ':' '{if ($3 > "") {print $3} else {print "-"}}'`
user=`GREP $target | awk '{print $2}' | awk -F ':' '{print $1}'`
label=`GREP $target | awk '{print $3}'`
# Process the case of more than one items in search results.
if [ $count -gt 1 ];then
echo -e 'Found follow servers: (\033[0;31mWhich one do you want to connect?\033[0m)'
arrtarget=($targetfullname)
arruser=($user)
arrpasswd=($passwd)
arrlabel=($label)
arrport=($port)
arrsshkey=($sshkey)
length=${#arrtarget[@]}
for ((i=0; i<$length; i++))
do
echo -e '[\033[4;34m'$(($i+1))'\033[0m]' "${arruser[$i]}@${arrtarget[$i]}:${arrport[$i]} ${arrlabel[$i]}"
done
# Choose one from search results
echo -n "Please choose by ID: "
choice=`GET_CHAR`
echo ""
echo $choice;
if [[ "$choice" =~ ^[0-9]+$ ]]; then
echo '';
else
exit 1;
fi
targetfullname=${arrtarget[$(($choice-1))]}
passwd=${arrpasswd[$(($choice-1))]}
user=${arruser[$(($choice-1))]}
label=${arrencoding[$(($choice-1))]}
port=${arrport[$(($choice-1))]}
sshkey=${arrsshkey[$(($choice-1))]}
fi
# Bad cases
if [ -z $targetfullname ] || [ -z $user ];then
echo "No matching server~";
exit 1;
fi
target=$targetfullname
# Any key value should not be empty
if [ -z $port ]; then
port=22
fi
if [ -z $passwd ]; then
passwd=-
fi
if [ -z $extra_options ]; then
extra_options=-
fi
if [ -z $sshkey ]; then
sshkey=-
fi
echo "Logging into ${user}@${target} ${label}..."
sshpass -p $passwd -P $port ssh $user@$target //利用 sshpass 不会出现第一个命令 会卡住现象 expect命令会出现 原因不详
#ssh-expect $user $target $passwd $port $extra_options $sshkey
参考 https://github.com/vipzhicheng/go (主要采用)
https://github.com/whorusq/ssh-autologin/blob/master/goto.sh 可参考
https://github.com/Kuchenm0nster/ssh-config-manager/blob/master/ssh-manager.sh 可优化