This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathssh-manager.sh
executable file
·209 lines (188 loc) · 3.93 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
#!/bin/bash
#########################################
# Original script by Errol Byrd
#########################################
# Modified by Robin Parisi
#================== Globals ==================================================
# Version
VERSION="0.6"
# Configuration
HOST_FILE="$HOME/.ssh_servers"
DATA_DELIM=":"
DATA_ALIAS=1
DATA_HUSER=2
DATA_HADDR=3
DATA_HPORT=4
PING_DEFAULT_TTL=20
SSH_DEFAULT_PORT=22
#================== Functions ================================================
function exec_ping() {
case $(uname) in
MINGW*)
ping -n 1 -i $PING_DEFAULT_TTL $@
;;
*)
ping -c1 -t$PING_DEFAULT_TTL $@
;;
esac
}
function test_host() {
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
}
function separator() {
echo -e "----\t----\t----\t----\t----\t----\t----\t----"
}
function list_commands() {
separator
echo -e "Availables commands"
separator
echo -e "$0 cc\t<alias> [username]\t\tconnect to server"
echo -e "$0 add\t<alias>:<user>:<host>:[port]\tadd new server"
echo -e "$0 del\t<alias>\t\t\t\tdelete server"
echo -e "$0 export\t\t\t\t\texport 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 server_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
}
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 "\t"
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 0
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'"
exit 1
fi
;;
# Add new alias
add )
server_add
;;
# 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'"
exit 1
;;
esac