创建并进入文件夹
mkdir ~/doc/tools/testbin
cd ~/doc/tools/testbin
一、创建账户密码文件
- 创建文件:
vim password.lst
- 输入以下内容
1:172.16.6.1:22:root:123456:(root)XX服务器1
2:172.16.6.2:22:root2:123456:(root)XX测试服务器2
3:172.16.6.3:22:root3:123456:(csapp)XX测试服务器3
4:172.16.6.4:22:root4:123456:(tgweb)XX 测试服务器4
注意:ip、端口、用户名、密码等自行替换
-
ESC
退出编辑模式, 输入:x
退出
二、创建连接文件
- 创建文件:
vim 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
-
ESC
退出编辑模式, 输入:x
退出
三、创建连接界面
- 创建文件:
vim so.sh
- 输入以下内容
#!/bin/bash
direc=`dirname $0`
function color(){
none="\033[0m"
black="\033[0;30m"
dark_gray="\033[1;30m"
blue="\033[0;36m"
light_blue="\033[1;34m"
green="\033[0;32m"
light_green="\033[1;32m"
cyan="\033[0;36m"
light_cyan="\033[1;36m"
red="\033[0;31m"
light_red="\033[1;31m"
purple="\033[0;35m"
light_purple="\033[1;35m"
brown="\033[0;33m"
yellow="\033[1;33m"
light_gray="\033[0;37m"
white="\033[1;37m"
close="\033[m"
case $1 in
# none)
# echo -e "$none $2 $close"
# ;;
blue)
echo "$blue $2 $close"
;;
green)
echo "$green $2 $close"
;;
red)
echo "$red $2 $close"
;;
purple)
echo "$purple $2 $close"
;;
yellow)
echo "$yellow $2 $close"
;;
white)
echo "$white $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 | \033[1;33m%15s | \033[1;37m%s\n",$1,$2,$6)}' $direc/password.lst
underline
read -p '[*] 选择主机(按q退出): ' 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
-
ESC
退出编辑模式, 输入:x
退出
网友评论