This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautotunnel
executable file
·59 lines (53 loc) · 1.76 KB
/
autotunnel
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
#!/usr/bin/env bash
# @Author: Thomas Foubert <tfoubert>
# @Date: 2019-04-25T17:50:59+02:00
# @Email: thomas@thomas-foubert.com
# @Filename: autotunnel
# @Last modified by: thomas
# @Last modified time: 2019-05-13T18:23:42+02:00
helper () {
echo "USAGE: ${0} [-h/--help] alias-name local-port remote-port"
echo "DESCRIPTION: "
echo -e "\t-h/--help\t\tDisplay this helper"
echo -e "\talias-name\t\tThe alias you want to give to your tunnel"
echo -e "\tlocal-port\t\tThe targeted local port for your tunnel"
echo -e "\tremote-port\t\tThe targeted remote port for your tunnel\n"
echo "The script must be run under root or sudo permisions"
exit $1
}
if [[ "$1" = "-h" ]] || [[ "$1" = "--help" ]]; then
helper 0
fi
if ! [ $(id -u) = 0 ]; then
echo "The script must be run under root or sudo permisions." >&2
exit 1
fi
if [[ -z $1 ]] || [[ -z $2 ]] || [[ -z $3 ]]; then
echo "Missing argument." >&2
helper 1
elif [[ -n $4 ]]; then
echo "Too much arguments." >&2
helper 1
fi
if ! [ -e /usr/bin/autossh ]; then
echo "Installing autossh"
apt install autossh
fi
echo "Preparing the service file"
cp basefile.txt autossh-tunnel.service
sed -i.bak s/alias/${1}/g autossh-tunnel.service
sed -i.bak s/lport/${2}/g autossh-tunnel.service
sed -i.bak s/rport/${3}/g autossh-tunnel.service
echo "Removing temp files"
rm *.bak
echo "Moving service file to system dir"
mv autossh-tunnel.service /etc/systemd/system/
echo "Preparing ssh connection"
mkdir -p ~/.ssh
touch ~/.ssh/known_hosts
ssh-keyscan serveo.net >> ~/.ssh/known_hosts 2>/dev/null
echo s"Installing the service"
systemctl daemon-reload
systemctl enable autossh-tunnel.service
systemctl start autossh-tunnel.service
echo "SSH Tunnel working, read the man to know how to access it"