-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
62 lines (49 loc) · 2.02 KB
/
bootstrap.sh
File metadata and controls
62 lines (49 loc) · 2.02 KB
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
#!/bin/bash
# Run on the Target (management) server to push all autoSSH scripts to a new
# remote host and kick off the setup.
#
# Usage: bootstrap.sh [--systemd] <user> <new-host>
#
# --systemd Pass --systemd through to reverseSSH.sh on the remote host.
# user - username on both this server and the new host
# new-host - hostname or IP of the new remote host (must be reachable now,
# e.g. on the local network before it goes behind the firewall)
#
# The scripts are pushed to /home/<user>/autoSSH-setup/ on the new host.
# findopenport.sh is also installed to /usr/local/bin on the Target (this machine)
# if it is not already present.
USE_SYSTEMD=""
if [[ "$1" == "--systemd" ]]; then
USE_SYSTEMD="--systemd"
shift
fi
if [[ $# -lt 2 ]]; then
echo "Usage: $0 [--systemd] <user> <new-host>"
exit 1
fi
USER="$1"
NEW_HOST="$2"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REMOTE_DIR="/home/$USER/autoSSH-setup"
TARGET=$(hostname -f)
## Install findopenport.sh on this Target if not already on PATH
if ! command -v findopenport.sh &>/dev/null; then
echo "Installing findopenport.sh to /usr/local/bin on this server..."
cp "$SCRIPT_DIR/findopenport.sh" /usr/local/bin/findopenport.sh
chmod +x /usr/local/bin/findopenport.sh
fi
## Push all scripts to the new host
echo "Copying scripts to $NEW_HOST:$REMOTE_DIR ..."
ssh $USER@$NEW_HOST "mkdir -p $REMOTE_DIR"
scp "$SCRIPT_DIR"/{reverseSSH.sh,autossh-tunnel.service,report.sh,teardown.sh} \
$USER@$NEW_HOST:$REMOTE_DIR/
## Copy this Target's .pem and .pub to the new host's install directory
ssh $USER@$NEW_HOST "mkdir -p /home/$USER/install"
scp /home/$USER/.ssh/id_rsa \
$USER@$NEW_HOST:/home/$USER/install/$USER.pem
scp /home/$USER/.ssh/id_rsa.pub \
$USER@$NEW_HOST:/home/$USER/install/$USER.pub
## Run reverseSSH.sh on the new host
echo "Running setup on $NEW_HOST..."
ssh -t $USER@$NEW_HOST "sudo bash $REMOTE_DIR/reverseSSH.sh $USE_SYSTEMD $USER $TARGET"
echo "Bootstrap complete. Run tunnel-status.sh on this server to verify."