1.安装必要软件expect
sudo apt install expect
2.书写配置文件password.lst
该文件用来保存用户的ssh链接信息
新建文件password.lst,然后添加如下内容
序号:IP:端口:用户:密码:说明
1:123.56.10.38:22:username:password:阿里云测试服务器
2:192.168.88.130:22:ca0gu0:toor:虚拟机mysql数据库服务器
3.新建入口脚本文件manager.sh
#!/bin/bash
direc=`dirname $0`
function color(){
blue="\033[0;36m"
red="\033[0;31m"
green="\033[0;32m"
close="\033[m"
case $1 in
blue)
echo -e "$blue $2 $close"
;;
red)
echo -e "$red $2 $close"
;;
green)
echo -e "$green $2 $close"
;;
*)
echo "Input color error!!"
;;
esac
}
function copyright(){
echo "#####################"
color blue " SSH Login Platform "
echo "#####################"
echo
}
function underline(){
echo "-----------------------------------------"
}
function main(){
while [ True ];do
echo "序号 | 主机 | 说明"
underline
awk 'BEGIN {FS=":"} {printf("\033[0;31m% 3s \033[m | %15s | %s\n",$1,$2,$6)}' $direc/password.lst
underline
read -p '[*] 选择主机: ' number
pw="$direc/password.lst"
ipaddr=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $2}}' $pw)
port=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $3}}' $pw)
username=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $4}}' $pw)
passwd=$(awk -v num=$number 'BEGIN {FS=":"} {if($1 == num) {print $5}}' $pw)
case $number in
[0-9]|[0-9][0-9]|[0-9][0-9][0-9])
echo $passwd | grep -q ".pem$"
RETURN=$?
if [[ $RETURN == 0 ]];then
ssh -i $direc/keys/$passwd $username@$ipaddr -p $port
echo "ssh -i $direc/$passwd $username@$ipaddr -p $port"
else
expect -f $direc/ssh_login.exp $ipaddr $username $passwd $port
fi
;;
"q"|"quit")
exit
;;
*)
echo "Input error!!"
;;
esac
done
}
copyright
main
4.新建ssh_login.exp脚本文件
#!/usr/bin/expect -f
set TARGET [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWD [lindex $argv 2]
set PORT [lindex $argv 3]
set timeout 10
spawn ssh $USER@$TARGET -p $PORT
expect {
"*yes/no" {send "yes\r"; exp_continue}
"*password:" {send "$PASSWD\r"}
}
interact
5.运行
./manager.sh
6.将该目录添加到环境变量
编辑~/.profile