-
Notifications
You must be signed in to change notification settings - Fork 33
/
ssh-tools.sh
executable file
·113 lines (112 loc) · 3.02 KB
/
ssh-tools.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#/bin/bash
echo 请选择你的机器列表
# define BASE PATH
BASE_PATH=$(dirname "$0")
# 定义数据相关的文件
DATA_FILE=$BASE_PATH/data/ssh.dat
# 定义执行ssh的文件
DIRECT_FILE=$BASE_PATH/exp/direct.exp
STEP_FILE=$BASE_PATH/exp/step.exp
TYPE_ARR=($(awk '{print $1}' $DATA_FILE))
HOST_ARR=($(awk '{print $2}' $DATA_FILE))
PASS_ARR=($(awk '{print $3}' $DATA_FILE))
COMMENT_ARR=($(awk '{print $4}' $DATA_FILE))
FORWARD_ARR=($(awk 'BEGIN {i=0}{if($1 !~ /^#/) i+=1} END {print $3}' $DATA_FILE))
FORWARD_ARR=($(awk 'BEGIN {i=0}{if($1 !~ /^#/) i+=1} END {print $4}' $DATA_FILE))
DEFAULT_TYPE=$1
DEFAULT_SOURCE=$2
DEFAULT_TARGET=$3
TEMP_ADDR="roo@127.0.0.1 22"
# 指定当前 session 的LC_CTYPE,用来解决 sz、rz 失效的问题
export LC_CTYPE=en_US
# def for host
function splitAddr(){
address=$1
arr=(${address//:/ })
if [[ ${#arr[@]} -eq 2 ]]; then
TEMP_ADDR="${arr[0]} ${arr[1]}"
else
TEMP_ADDR="${arr[0]} 22"
fi
}
# 用于打印服务器列表
function printList(){
for (( i = 0; i < $arr_len; i++ )); do
if [[ ${TYPE_ARR[$i]} -eq $1 ]]; then
echo "$i ${HOST_ARR[$i]} ${COMMENT_ARR[$i]}"
fi
done
echo 请输入你需要直接进入的服务器
}
# 直接连接的函数
function direct()
{
if [[ -n $DEFAULT_SOURCE ]]; then
select=$DEFAULT_SOURCE
# using split port
splitAddr ${HOST_ARR[$select]};
expect $DIRECT_FILE $TEMP_ADDR ${PASS_ARR[$select]}
else
printList 0
read select
if [[ ${arr_len} -gt $select ]]; then
# 直接连接的机器
echo 正在连接服务器 ${HOST_ARR[$select]}
splitAddr ${HOST_ARR[$select]}
expect $DIRECT_FILE $TEMP_ADDR ${PASS_ARR[$select]}
else
echo 输入的数字不正确
fi
fi
}
# 跳板机
function step(){
if [[ -n $DEFAULT_SOURCE ]]; then
select=$DEFAULT_SOURCE
splitAddr ${HOST_ARR[$select]}
SOURCE_ADDR=$TEMP_ADDR
target=$DEFAULT_TARGET
splitAddr ${HOST_ARR[$target]}
TARGET_ADDR=$TEMP_ADDR
expect $STEP_FILE $SOURCE_ADDR ${PASS_ARR[$select]} ${TARGET_ADDR} ${PASS_ARR[$target]}
else
echo '选择跳板机'
printList 1
read select
if [[ ${arr_len} -gt $select ]]; then
printList 2
read -p "选择目标机:" target
# 直接连接的机器
if [[ ${TYPE_ARR[$target]} -eq 2 ]]; then
splitAddr ${HOST_ARR[$select]}
SOURCE_ADDR=$TEMP_ADDR
splitAddr ${HOST_ARR[$target]}
TARGET_ADDR=$TEMP_ADDR
echo 正在连接服务器 ${HOST_ARR[$select]}
expect $STEP_FILE $SOURCE_ADDR ${PASS_ARR[$select]} $TARGET_ADDR ${PASS_ARR[$target]}
fi
else
echo 输入的数字不正确
fi
fi
}
# fefault type
arr_len=${#HOST_ARR[@]}
if [[ -n $DEFAULT_TYPE ]]; then
type=$DEFAULT_TYPE
else
echo "请选择连接的类型:1直接连接,2 跳板"
read type
fi
case "$type" in
"1")
direct
;;
"2")
step
;;
*)
#其它输入
echo "output error,please input 1/2"
;;
esac