forked from robinparisi/ssh-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-manager.sh
executable file
·225 lines (198 loc) · 4.36 KB
/
ssh-manager.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
#########################################
# Original script by Errol Byrd
# Copyright (c) 2010, Errol Byrd <[email protected]>
#########################################
# Modified by Robin Parisi
# Contact at [email protected]
# Github https://github.com/Fendtwick/ssh-manager
# Modified by Free 4 Funs
# Contact at [email protected]
# Github https://github.com/Owned67/ssh-manager
#================== Globals ==================================================
# Version
VERSION="0.7"
# Configuration
HOST_FILE="$HOME/.ssh_servers"
DATA_DELIM=":"
DATA_ALIAS=1
DATA_HUSER=2
DATA_HADDR=3
DATA_HPORT=4
if [ -f /etc/ssh/sshd_config ];then
SSH_DEFAULT_PORT="`cat /etc/ssh/sshd_config | grep Port | awk '{print $2}'`"
else
echo "Server SSH is not installed or sshd_config is not present"
exit 1
fi
AllowPing=1
#================== Functions ================================================
function exec_ping() {
case $(uname) in
MINGW*)
ping -n 1 -i 2 $@
;;
*)
ping -c1 -t 2 $@
;;
esac
}
function test_host() {
if [ $AllowPing -eq 1 ];then
exec_ping $* > /dev/null
if [ $? != 0 ] ; then
echo -n "["
cecho -n -red "KO"
echo -n "] "
else
echo -n "["
cecho -n -green "UP"
echo -n "] "
fi
else
cecho -n -red "This function is disabled, Set variable AllowPing to 1"
fi
}
function separator() {
echo "--------------------------------------------------------------------------------"
}
function list_commands() {
separator
echo -e "Availables commands"
separator
echo "cc <alias> [username] connect to server"
echo "add <alias>:<user>:<host>:[port] add new server"
echo "del <alias> delete server"
echo "export export config"
}
function probe ()
{
als=$1
grep -w -e $als $HOST_FILE > /dev/null
return $?
}
function get_raw ()
{
als=$1
grep -w -e $als $HOST_FILE 2> /dev/null
}
function get_addr ()
{
als=$1
get_raw "$als" | awk -F "$DATA_DELIM" '{ print $'$DATA_HADDR' }'
}
function get_port ()
{
als=$1
get_raw "$als" | awk -F "$DATA_DELIM" '{ print $'$DATA_HPORT'}'
}
function get_user ()
{
als=$1
get_raw "$als" | awk -F "$DATA_DELIM" '{ print $'$DATA_HUSER' }'
}
function cecho {
while [ "$1" ]; do
case "$1" in
-normal) color="\033[00m" ;;
-black) color="\033[30;01m" ;;
-red) color="\033[31;01m" ;;
-green) color="\033[32;01m" ;;
-yellow) color="\033[33;01m" ;;
-blue) color="\033[34;01m" ;;
-magenta) color="\033[35;01m" ;;
-cyan) color="\033[36;01m" ;;
-white) color="\033[37;01m" ;;
-n) one_line=1; shift ; continue ;;
*) echo -n "$1"; shift ; continue ;;
esac
shift
echo -en "$color"
echo -en "$1"
echo -en "\033[00m"
shift
done
if [ ! $one_line ]; then
echo
fi
}
#=============================================================================
cmd=$1
alias=$2
user=$3
# if config file doesn't exist
if [ ! -f $HOST_FILE ]; then touch "$HOST_FILE"; fi
# without args
if [ $# -eq 0 ]; then
separator
echo "List of availables servers for user $(whoami) "
separator
while IFS=: read label user ip port
do
test_host $ip
echo -ne " "
cecho -n -blue $label
echo -ne ' ==> '
cecho -n -red $user
cecho -n -yellow "@"
cecho -n -white $ip
echo -ne ' -> '
if [ "$port" == "" ]; then
port=$SSH_DEFAULT_PORT
fi
cecho -yellow $port
echo
done < $HOST_FILE
list_commands
exit 1;
fi
case "$cmd" in
# Connect to host
cc )
probe "$alias"
if [ $? -eq 0 ]; then
if [ "$user" == "" ]; then
user=$(get_user "$alias")
fi
addr=$(get_addr "$alias")
port=$(get_port "$alias")
# Use default port when parameter is missing
if [ "$port" == "" ]; then
port=$SSH_DEFAULT_PORT
fi
echo "connecting to '$alias' ($addr:$port)"
ssh $user@$addr -p $port
else
echo "$0: unknown alias '$alias'"
fi
;;
# Add new alias
add )
probe "$alias"
if [ $? -eq 0 ]; then
as echo "$0: alias '$alias' is in use"
else
echo "$alias$DATA_DELIM$user" >> $HOST_FILE
echo "new alias '$alias' added"
fi
;;
# Export config
export )
echo
cat $HOST_FILE
;;
# Delete ali
del )
probe "$alias"
if [ $? -eq 0 ]; then
cat $HOST_FILE | sed '/^'$alias$DATA_DELIM'/d' > /tmp/.tmp.$$
mv /tmp/.tmp.$$ $HOST_FILE
echo "alias '$alias' removed"
else
echo "$0: unknown alias '$alias'"
fi
;;
* )
echo "$0: unrecognised command '$cmd'"
;;
esac