-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautossh.sh
192 lines (176 loc) · 3.51 KB
/
autossh.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
#!/bin/bash
# ------------------------------------------------------------------------------
# FILE: autossh
# DESCRIPTION: This is an SSH-D proxy with auto-reconnect on disconnect
# AUTHOR: Hector Nguyen (hectornguyen at octopius dot com)
# VERSION: 1.0.0
# ------------------------------------------------------------------------------
VERSION="1.0.0"
GITHUB="https://github.com/hectornguyen/autossh"
AUTHOR="Hector Nguyen"
SCRIPT=${0##*/}
IFS=$'\n'
ALIVE=0
HISTFILE="$HOME/.autossh.history"
# Use colors, but only if connected to a terminal, and that terminal supports them.
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
fi
# Progress or something
start_progress()
{
while true
do
echo -ne "#"
sleep 1
done
}
quick_progress()
{
while true
do
echo -ne "#"
sleep .033
done
}
long_progress()
{
while true
do
echo -ne "#"
sleep 3
done
}
dot_progress()
{
for i in {1..100}; do
printf "." $i -1 $i
sleep .033
done
echo_c green " 100%{$NORMAL}"
sleep 1
}
stop_progress()
{
kill $1
wait $1 2>/dev/null
echo -en "\n"
}
# Case-insensitive for regex matching
shopt -s nocasematch
# Prepare history mode
set -i
history -c
history -r
# Input method
get_input()
{
read -e -p "${BLUE}$1${NORMAL}" "$2"
history -s "${!2}"
}
# Echo in bold
echo_b()
{
if [ "$1" = "-e" ]; then
echo -e "${BOLD}$2${NORMAL}"
else
echo "${BOLD}$1${NORMAL}"
fi
}
# Echo in colour
echo_c()
{
case "$1" in
red | r | -red | -r | --red | --r ) echo "${RED}$2${NORMAL}" ;;
green | g | -green | -g | --green | --g ) echo "${GREEN}$2${NORMAL}" ;;
blue | b | -blue | -b | --blue | --b ) echo "${BLUE}$2${NORMAL}" ;;
yellow | y | -yellow | -y | --yellow | --y ) echo "${YELLOW}$2${NORMAL}" ;;
* ) echo "$(BOLD)$2$(RESET)" ;;
esac
}
# Get data from parameters
if [[ ! -n "$remote_param" && -n "$1" ]]; then
remote_param="$1"
remote_user="${remote_param%%@*}"
remote_ip="${remote_param##*@}"
fi
# Get input data and save to history
save_input()
{
if [[ ! -n "$remote_user" && ! -n "$1" ]]; then
while get_input "SSH Username > " remote_user; do
case ${remote_user%% *} in
* )
if [ -n "$remote_user" ]; then
break
else
continue
fi
;;
esac
done
fi
if [[ ! -n "$remote_ip" && ! -n "$1" ]]; then
while get_input "SSH Alias/IP-address > " remote_ip; do
case ${remote_ip%% *} in
* )
if [ -n "$remote_ip" ]; then
break
else
continue
fi
;;
esac
done
fi
}
# Infinitie loop to keep connecting
auto_connect()
{
while true; do
exist=`ps aux | grep "$remote_user@$remote_ip" | grep 22`
if test -n "$exist"
then
if test $ALIVE -eq 0
then
echo_c yellow "I'm alive since $(date)"
fi
ALIVE=1
else
ALIVE=0
echo_c red "I'm dead... God is bringing me back..."
clear
printf "${GREEN}Connecting: "
for i in {1..100}; do
printf "." $i -1 $i
sleep .033
done
echo_c green " 100%${NORMAL}"
sleep 1
clear
ssh $remote_user@$remote_ip
fi
sleep 1
done
}
main()
{
save_input
auto_connect
}
main