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 可优化

 

posted @   托马斯布莱克  阅读(573)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
历史上的今天:
2016-09-22 深入解析Java中volatile关键字的作用
点击右上角即可分享
微信分享提示