-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh-tui
executable file
·206 lines (190 loc) · 5.67 KB
/
ssh-tui
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash
export CONF_DIR="$HOME/.ssh-tui"
export PUBKEY="$HOME/.ssh/*.pub"
export BACKTITLE="SSH-TUI(TSHELL) by lofyer, v1.2"
export TMP_ANS="/tmp/ssh-tui-tmp-$$"
init(){
if [ -e "$CONF_DIR" ]
then
return
fi
mkdir "$CONF_DIR"
if [ $? != 0 ]
then
dialog --clear --backtitle "$BACKTITLE" --title "ERROR" --ok-label "OK" --infobox "Make sure that you have write permission in ~/" 14 60
exit
fi
if [ -e "~/.ssh/config" ]
then
return
else
echo -e "Host *\nServerAliveInterval 60\nServerAliveCountMax 5" > ~/.ssh/config
chmod 644 ~/.ssh/config
fi
if [ -e "$PUBKEY" ]
then
return
else
dialog --clear --backtitle "$BACKTITLE" --title "ERROR" --ok-label "OK" --infobox "No pub key found. Press OK to create." 14 60
./key-generate.exp $HOME
fi
}
show_list(){
while true
do
local SESSION_FILES=($(ls $CONF_DIR/*.session))
local MENU=
local COUNT=0
for i in ${SESSION_FILES[@]}
do
COUNT=$[COUNT+1]
MENU="${MENU} ${COUNT} $(basename $i)"
done
local OPTIONS=(${MENU})
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "Select" --cancel-label "Back" \
--title "Connect" \
--menu "Select session profile" 14 60 6 \
"${OPTIONS[@]}" \
2>$TMP_ANS
if [ $? != 0 ]
then
break
fi
clear
local ANSWER=$(cat $TMP_ANS)
local SESSION=($(cat ${SESSION_FILES[$(($ANSWER-1))]}))
echo "ssh ${SESSION[1]}@${SESSION[2]} -p${SESSION[3]}"
echo ${SESSION[@]}
if [[ ${SESSION[@]} == *"PROXY_FLAG"* ]]; then
ssh -v -o ProxyCommand="nc -x ${SESSION[5]} %h %p" -o ConnectTimeout=10 -o ConnectionAttempts=1 ${SESSION[1]}@${SESSION[2]} -p${SESSION[3]}
else
ssh -v -o ConnectTimeout=10 -o ConnectionAttempts=1 ${SESSION[1]}@${SESSION[2]} -p${SESSION[3]}
fi
done
}
add_list(){
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "OK" --cancel-label "Back" --title "PROFILE INFOMATION" \
--inputbox "Input profile name" 14 60 \
2>$TMP_ANS
if [ $? != 0 ]
then
return 1
fi
local PROFILE_NAME=$(cat $TMP_ANS)
CMD=$(touch $CONF_DIR/$PROFILE_NAME.session)
echo $PROFILE_NAME > $CONF_DIR/$PROFILE_NAME.session
# username
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "OK" --cancel-label "Back" --title "PROFILE INFOMATION" \
--inputbox "Input username" 14 60 "root" \
2>$TMP_ANS
if [ $? != 0 ]
then
rm -fr $CONF_DIR/$PROFILE_NAME.session
return 1
fi
local USERNAME=$(cat $TMP_ANS)
echo $USERNAME >> $CONF_DIR/$PROFILE_NAME.session
# host
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "OK" --cancel-label "Back" --title "PROFILE INFOMATION" \
--inputbox "Input hostname or IP" 14 60 \
2>$TMP_ANS
if [ $? != 0 ]
then
rm -fr $CONF_DIR/$PROFILE_NAME.session
return 1
fi
local HOSTNAME=$(cat $TMP_ANS)
echo $HOSTNAME >> $CONF_DIR/$PROFILE_NAME.session
# port
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "OK" --cancel-label "Back" --title "PROFILE INFOMATION" \
--inputbox "Input ssh port" 14 60 "22" \
2>$TMP_ANS
if [ $? != 0 ]
then
rm -fr $CONF_DIR/$PROFILE_NAME.session
return 1
fi
local PORT=$(cat $TMP_ANS)
echo $PORT >> $CONF_DIR/$PROFILE_NAME.session
sync
# proxy
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "Yes" --cancel-label "No" --title "PROFILE INFOMATION" \
--yesno "Use proxy?" 14 60 \
2>&1 >/dev/tty
if [[ $? == 0 ]]
then
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "OK" --cancel-label "Back" --title "PROFILE INFOMATION" \
--inputbox "Input proxy" 14 60 "127.0.0.1:1086" \
2>$TMP_ANS
if [ $? != 0 ]
then
local PROXY=$(cat $TMP_ANS)
echo "PROXY_FLAG" >> $CONF_DIR/$PROFILE_NAME.session
echo $PROXY >> $CONF_DIR/$PROFILE_NAME.session
fi
sync
fi
# ssh-copy-id
dialog --clear --backtitle "$BACKTITLE" \
--ok-label "Yes" --cancel-label "No" --title "PROFILE INFOMATION" \
--yesno "Try copy id to the host?" 14 60 \
2>&1 >/dev/tty
if [[ $? == 0 ]]
then
echo $USERNAME@$HOSTNAME
ssh-copy-id $USERNAME@$HOSTNAME -p $PORT
fi
}
delete_list(){
local MENU=
local COUNT=0
local SESSION_FILES=($(ls $CONF_DIR/*.session))
if [[ $SESSION_FILES == "" ]]
then
return
fi
for i in ${SESSION_FILES[@]}
do
COUNT=$[COUNT+1]
MENU="${MENU} ${COUNT} $i off"
done
local OPTIONS=(${MENU})
local CHOICES=$(dialog --separate-output --checklist "Select options:" 22 76 16 "${OPTIONS[@]}" 2>&1 >/dev/tty)
if [ $? != 0 ]
then
break
fi
for CHOICE in $CHOICES
do
rm -fr ${SESSION_FILES[$(($CHOICE-1))]}
done
}
main(){
while true
do
dialog --clear --backtitle "$BACKTITLE" --ok-label "Select" --cancel-label "Exit" --title "Menu" --menu "Select action" 14 60 6 \
"List" "Show all the session" \
"Add" "Add new session" \
"Delete" "Delete session from list" \
2>$TMP_ANS
if [[ $? == 1 ]]
then
break
fi
advanced_answer=$(cat $TMP_ANS)
case $advanced_answer in
"List") show_list;;
"Add") add_list;;
"Delete") delete_list;;
esac
done
}
init
main