-
Notifications
You must be signed in to change notification settings - Fork 2
/
tunnel
executable file
·77 lines (62 loc) · 2.58 KB
/
tunnel
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
#!/bin/bash
BASTION_USERNAME=
BASTION_HOST=
BASTION_PRIVATE_KEY_FILE=
LOCAL_PORT_MAP=
REMOTE_HOST=
REMOTE_PORT=
print_usage() {
printf "Usage:\ttunnel [OPTIONS]\n"
printf "\nCreate an SSH tunnel quickly and efficiently\n\n"
printf "Options:\n"
printf " -u, --bastion-username string\t\tThe username of the server you have SSH access to\n"
printf " -h, --bastion-host string\t\tThe host of the server you have SSH access to\n"
printf " -k, --bastion-private-key string\tThe private key file path for the user of the server you have SSH access to\n"
printf " -p, --local-port int\t\t\tThe port on your computer that you want to use to access the remote resource through\n"
printf " -r, --remote-host string\t\tThe host of the resource that is accessible from within the network of the bastion server\n"
printf " -P, --remote-port int\t\t\tThe port of the resource that is accessible from within the network of the bastion server\n\n"
}
while true; do
case "$1" in
-u | --bastion-username ) BASTION_USERNAME="$2"; shift 2 ;;
-h | --bastion-host ) BASTION_HOST="$2"; shift 2 ;;
-k | --bastion-private-key ) BASTION_PRIVATE_KEY_FILE="$2"; shift 2 ;;
-p | --local-port ) LOCAL_PORT_MAP="$2"; shift 2 ;;
-r | --remote-host ) REMOTE_HOST="$2"; shift 2 ;;
-P | --remote-port ) REMOTE_PORT="$2"; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ -z "$BASTION_USERNAME" ]; then
print_usage
exit 2;
fi
if [ -z "$BASTION_HOST" ]; then
print_usage
exit 2;
fi
if [ -z "$BASTION_PRIVATE_KEY_FILE" ]; then
print_usage
exit 2;
fi
if [ -z "$LOCAL_PORT_MAP" ]; then
print_usage
exit 2;
fi
if [ -z "$REMOTE_HOST" ]; then
print_usage
exit 2;
fi
if [ -z "$REMOTE_PORT" ]; then
print_usage
exit 2;
fi
IMAGE_TAG="noahjahn-utils-tunnel"
CONTAINER_NAME="$IMAGE_TAG-$(date +%s)"
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
docker build -f "$SCRIPT_DIR/Dockerfile.tunnel" -t $IMAGE_TAG . || exit 1;
docker run -p $LOCAL_PORT_MAP:$LOCAL_PORT_MAP --name $CONTAINER_NAME --rm --entrypoint=tail -d $IMAGE_TAG -f /dev/null || exit 1;
docker cp $BASTION_PRIVATE_KEY_FILE $CONTAINER_NAME:/bastion-private-key || { docker stop -t 1 $CONTAINER_NAME; exit 1; }
docker exec -i $CONTAINER_NAME chmod 600 /bastion-private-key || { docker stop -t 1 $CONTAINER_NAME; exit 1; }
docker exec -it $CONTAINER_NAME ssh -o StrictHostKeyChecking=no -g -N -i /bastion-private-key -L 0.0.0.0:$LOCAL_PORT_MAP:$REMOTE_HOST:$REMOTE_PORT $BASTION_USERNAME@$BASTION_HOST || { docker stop -t 1 $CONTAINER_NAME; }