-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathessh
More file actions
executable file
·187 lines (172 loc) · 4.63 KB
/
essh
File metadata and controls
executable file
·187 lines (172 loc) · 4.63 KB
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
#!/usr/bin/env bash
# TODO:
# improve help
# improve iterate through environments
# switch to use reasonable hosts format: user@hostname:port
# improve checking if entry is present in env file
# detect multiple entries with the same name
# scp functionality
# switch from group and name to "namespaces", like company:group:server
# essh would call first unique id
# add did you mean for example with edit distance
# override environment settings with command line parameters
# update .env.srv entries
PORT=22
PARAMS=""
PRINT=""
PING=""
GROUP=""
ENV_NAME=""
USER=`whoami`
HOST=""
PARAMS=""
ENV_FILE_NAME="${HOME}/bin/.env.srv"
function help {
echo "Another layer added on top of ssh/ping for faster operation on known servers"
echo "Usage:"
echo " $(basename $0) env_name [-p] [-f filename]"
echo " $(basename $0) print [env_name] [-f filename]"
echo " $(basename $0) ping env_name [-f filename]"
echo " $(basename $0) add -g group -n name -h hostname [-u username] [-p port] [-P params] [-f filename]"
echo;
echo "Parameters:"
echo " -f filename - common, use alternative configuration file"
echo " -p - connect, use of additional parameters specified in configuration file"
echo;
echo "Examples:"
echo " $(basename $0) print home - prints every entry for group 'home'"
echo " $(basename $0) router -p - connects to host router with use of additional parameters"
echo;
echo "Sample configuration file: env.srv"
echo "Double spaces in configuration file are not allowed!"
echo "Advantage over ~/.ssh/config or /etc/hosts - it's easier to store i.e. on Dropbox and use the same for every computer you use."
echo "Default port - 22"
echo "Default user - `whoami` - currently logged in"
echo "Config file location - ${ENV_FILE_NAME}"
exit 1
}
function print_envs {
if [ $# -gt 0 ]; then
grep "$1" ${ENV_FILE_NAME}
else
cat ${ENV_FILE_NAME}
fi
exit 0
}
function add_env_iterate {
while [ $# -gt 0 ]; do
if [ "$1" = "-g" ]; then
shift
GROUP="$1"
elif [ "$1" = "-n" ]; then
shift
ENV_NAME="$1"
elif [ "$1" = "-u" ]; then
shift
USER="$1"
elif [ "$1" = "-h" ]; then
shift
HOST="$1"
elif [ "$1" = "-p" ]; then
shift
PORT="$1"
elif [ "$1" = "-P" ]; then
shift
PARAMS="$1"
fi
shift
done
}
function add_env {
if [ -z "$GROUP" ]; then
echo "Group cannot be empty!"; help
fi
if [ -z "$ENV_NAME" ]; then
echo "Name cannot be empty!"; help
fi
if [ -z "$HOST" ]; then
echo "Host cannot be empty!"; help
fi
if [ ! -z "`egrep \"\s${ENV_NAME}\s\" ${ENV_FILE_NAME}`" ]; then
echo "Entry for ${ENV_NAME} already exists!"; exit -4
fi
echo "${GROUP} ${ENV_NAME} ${HOST} ${USER} ${PORT} ${PARAMS}" >> ${ENV_FILE_NAME}
exit
}
if [ $# = 0 ]; then
help
fi
# iterate through parameters
while [ $# -gt 0 ]; do
if [ "$1" = "add" ]; then
shift
add_env_iterate $@
add_env
elif [ "$1" = "-p" ]; then
PARAMS="true"
elif [ "$1" = "print" ]; then
if [ ! -z "${PING}" ]; then
echo "Can't use print and ping at the same time!"; help
fi
PRINT="true"
elif [ "$1" = "ping" ]; then
if [ ! -z "${PRINT}" ]; then
echo "Can't use print and ping at the same time!"; help
fi
PING="true"
elif [ "$1" = "-f" ]; then
shift
if [ ! -z "$1" ]; then
ENV_FILE_NAME="$1"
echo "Using configuration file: ${ENV_FILE_NAME}"
else
echo "Alternative configuration file not specified!"; help
fi
elif [ "${ENV_NAME}" = "" ]; then
ENV_NAME="$1"
else
echo "Check parameters!"; help
fi
shift
done
if [ ! -f "${ENV_FILE_NAME}" ]; then
echo "Can't find configuration file: ${ENV_FILE_NAME}"
exit -1
fi
if [ ! -z "${PRINT}" ]; then
print_envs ${ENV_NAME}
fi
ENV_FILE=`grep "^[^#;]" ${ENV_FILE_NAME}`
IFS=$'\n'
ENV_ENTRIES=(${ENV_FILE})
IFS=' '
# iterate through environments
for ENV in "${ENV_ENTRIES[@]}"
do
ENTRY=(${ENV})
if [ "${ENTRY[1]}" = "${ENV_NAME}" ]; then
SPACE=$(echo "${ENV}" | grep " ")
if [ ! -z "${SPACE}" ]; then
echo -e "Check entry:\n${ENV}"; exit -4
fi
if [ ! -z "${ENTRY[4]}" ]; then
PORT=${ENTRY[4]}
fi
if [ ! -z "${PARAMS}" ]; then
if [ -z "${ENTRY[5]}" ]; then
echo "Parameters for ${ENV} not specifiled in ${ENV_FILE_NAME}!"; exit -3
fi
PARAMS="${ENTRY[5]}"
fi
if [ -z "${PING}" ]; then
echo ssh "${ENTRY[3]}"@"${ENTRY[2]}" -p ${PORT} ${PARAMS/'&'/' '}
ssh "${ENTRY[3]}"@"${ENTRY[2]}" -p ${PORT} ${PARAMS/'&'/' '}
else
echo ping "${ENTRY[2]}"
ping "${ENTRY[2]}"
fi
exit 0
fi
done
echo Environment not found!
exit -2