-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdojump.sh
executable file
·98 lines (80 loc) · 2.31 KB
/
dojump.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
#!/bin/bash
# Fancy error message
show_error() {
echo -e "\e[01;31m$1\e[0m"
}
# Obtain remote IP
get_droplet_ip(){
DROPLET_IP=$(doctl compute droplet list | grep jumpbox | awk '{print $3}')
}
ssh_status(){
echo -ne "."
nc -w1 $DROPLET_IP 22
}
# Start sshuttle
sshuttle_start(){
if [ -z "$DROPLET_IP" ]; then
show_error "ERROR: Could not determine Droplet's IP address."
show_error "Perhaps not enough time to start remote SSH server, try again..."
exit 1
fi
echo "Tunneling all network traffic via $DROPLET_IP";
sshuttle --dns -e 'ssh -q -o CheckHostIP=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' -r root@$DROPLET_IP 0/0 -x $DROPLET_IP
}
# Check if requirements doctl and sshuttle are installed
if ! command -v doctl &> /dev/null
then
show_error "ERROR: doctl could not be found. Installation instructions:"
echo "https://www.digitalocean.com/docs/apis-clis/doctl/how-to/install"
exit
fi
if ! command -v sshuttle &> /dev/null
then
show_error "ERROR: sshuttle could not be found. Installation instructions:"
echo "https://github.com/sshuttle/sshuttle"
exit
fi
# Read config
PDIR=$(dirname $(readlink -f $0))
CONFIG="$PDIR/dojump.conf"
if [ -e $CONFIG -a -r $CONFIG ]; then
source $CONFIG
if [ "$VERBOSE" == "true" ]; then
echo "Parsing config file..."
fi
else
show_error "No configuration file found, creating it for you"
cp -v $CONFIG.default $CONFIG
echo "Please edit $CONFIG before running again."
exit 0
fi
# Remote SSH FingerPrint is a must
# Get it with: doctl compute ssh-key list
if [ -z "$DROPLET_SSH_KEYS" ]; then
show_error "ERROR: Set up DROPLET_SSH_KEYS in $CONFIG!"
exit 1
fi
# Check if we are using existing Droplet or creating new one
get_droplet_ip
if [ -z "$DROPLET_IP" ]; then
echo -n "Creating new Droplet, please wait..."
doctl compute droplet create \
--region $DROPLET_REGION \
--image $DROPLET_IMAGE \
--size $DROPLET_SIZE \
--ssh-keys $DROPLET_SSH_KEYS \
--wait jumpbox > /dev/null 2>&1
echo " done"
get_droplet_ip
echo ""
echo -ne "Checking remote SSH Server."
until ssh_status; do
sleep 2
done
sshuttle_start
else
echo "Found existing droplet named 'jumpbox', resuming..."
sshuttle_start
fi
# Upon exiting (ctrl+c) offer to destroy droplet to save that money!
doctl compute droplet delete jumpbox