-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosh-knock.sh
executable file
·166 lines (149 loc) · 4.31 KB
/
mosh-knock.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
#!/bin/sh
# mosh-knock: Script to automatically knock the ports encripted in a file and then connect through mosh.
# written by Diego Guida -- March - 2017
##########
#DEFAULTS#
##########
KNOCK_FILE="~/.ssh/mosh/knock_ports.gpg"
SSH_PORT=22
SSH_FORW_FILE="~/.ssh/mosh/ssh_forwards"
#######################
#The command line help#
#######################
display_help() {
echo "Usage: $0 [option...] SERVER" >&2
echo
echo " -c --connect Connect to mosh server"
echo " -s --ssh Connect to SSH server"
echo " -p --port <SSH_Port> Specify the SSH port"
echo " -k --knock Knock using the predetermined file ~/.ssh/mosh/knock_ports.gpg"
echo " -kf <file> Knock using the <file> ports"
echo " -f --forward <Local_Port> <Host_Port> Forward specified local port to host port"
echo " -fd Forward the ports specified in the file ssh_forwards"
echo " -ff <file> Forward the ports specified in <file>, SSH format is expected"
echo " -h --help Display this help and exit."
echo
echo "Example"
echo
echo "Connect to mosh server at port 43200, knock ports in file knocking_heaven.gpg and forward local port 8080 to 4300:"
echo '"mosh_knock.sh -c -p 43200 -kf "~/.ssh/mosh/knocking_heaven.gpg" -f 8080 4300"'
echo
exit 1
}
###################################################################
#Unencrypt the file containing the ports to knock and knock them. #
#Avoided the use of an intermediate file for security reasons. #
#Knock -d [delay] added to fix server-router filtering the packets#
###################################################################
knock() {
echo "Knocking'em ports..."
gpg2 -d $KNOCK_FILE 2> /dev/null | xargs -L3 knock -d 20 $SERVER
sleep 0.5 #Sleep in order to wait for port to be trully opened
echo "Knocked."
}
check_port(){
timeout 1 bash -c "</dev/tcp/${SERVER}/${SSH_PORT}" &> /dev/null
if [ ! $? -eq 0 ]; then
echo "Port is closed"
read -p "Want me to knock it open?" yn
case $yn in
[Yy]* ) knock;;
[Nn]* ) exit 2;;
esac
fi
}
###############################
#Forward the ports through SSH#
###############################
port_forw(){
#If SSH_FORW is set to 1 (-f), forward the specified port only.
#sleep 30 opens port forward for 30s and closes it if no connection is active.
if [ $SSH_FORW -eq 1 ]; then
echo "Forwarding port $PORT_LOCAL --> ${PORT_SERVER}"
ssh -f -o ExitOnForwardFailure=yes -L ${PORT_LOCAL}:${SERVER}:${PORT_SERVER} sleep 30 &> /dev/null
else
echo "Forwarding ports specified at ${SSH_FORW_FILE}"
ssh -f -o ExitOnForwardFailure=yes -F $SSH_FORW_FILE $SERVER sleep 30 &