diff options
author | Mike Crute <mike@crute.us> | 2017-08-16 16:54:18 +0000 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2017-08-16 16:54:18 +0000 |
commit | f8bdc2eedd1c7b028c900b0af99ee5e68190c082 (patch) | |
tree | 2e1dfbd1f69fde1352764877cec9f4931dc60873 | |
parent | 232681e150432aa962ad8544407a6cf91d149de3 (diff) | |
download | server_bin-f8bdc2eedd1c7b028c900b0af99ee5e68190c082.tar.bz2 server_bin-f8bdc2eedd1c7b028c900b0af99ee5e68190c082.tar.xz server_bin-f8bdc2eedd1c7b028c900b0af99ee5e68190c082.zip |
Add phonehome script
-rwxr-xr-x | phonehome.sh | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/phonehome.sh b/phonehome.sh new file mode 100755 index 0000000..f469d6c --- /dev/null +++ b/phonehome.sh | |||
@@ -0,0 +1,52 @@ | |||
1 | #!/bin/bash | ||
2 | # | ||
3 | # Alternate Access Tunnel for Remote Hosts | ||
4 | # | ||
5 | # Opens an SSH connection to a central aggregation host and forwards the local | ||
6 | # SSH port over that connection. This allows remote connections to the server | ||
7 | # from the central aggregation host. In the absence of true OOB recovery | ||
8 | # systems this mechanism provides some OOB recovery capability provided the | ||
9 | # entire internet connection is not down. | ||
10 | # | ||
11 | |||
12 | KEY=/root/.ssh/phonehome.id_rsa | ||
13 | CRONTAB=/var/spool/cron/crontabs/root | ||
14 | CRON_ENTRY="*/5 * * * * /root/phonehome.sh" | ||
15 | |||
16 | # Alt host hedges against DNS being down if the default DNS server is on the | ||
17 | # wrong side of a collapsed ssh tunnel | ||
18 | # | ||
19 | # Grab only answers starting with numbers since we're trying to resolve the IP | ||
20 | # itself not the hostname (in the absence of normal DNS). Grab the first numeric | ||
21 | # answer in the case of multiple records | ||
22 | HOST_IP=$(dig +short @8.8.8.8 phonehome.crute.me | awk '/^[0-9]+/ { print $0; exit; }') | ||
23 | |||
24 | function start_autossh { | ||
25 | autossh -f \ | ||
26 | -gNxCT \ | ||
27 | -l phonehome -p 4321 $1 \ | ||
28 | -i $KEY \ | ||
29 | -R $REMOTE_PORT:localhost:22 | ||
30 | } | ||
31 | |||
32 | if [ ! -f /etc/default/phonehome ]; then | ||
33 | echo "echo 'REMOTE_PORT=1234' > /etc/default/phonehome" | ||
34 | exit 1 | ||
35 | else | ||
36 | source /etc/default/phonehome | ||
37 | fi | ||
38 | |||
39 | if ! grep 'phonehome.sh' $CRONTAB 2>&1 > /dev/null; then | ||
40 | echo "Not in crontab, adding" | ||
41 | echo "$CRON_ENTRY" >> $CRONTAB | ||
42 | else | ||
43 | if ! grep -F "$CRON_ENTRY" $CRONTAB 2>&1 > /dev/null; then | ||
44 | echo "In crontab but not correct, fixing" | ||
45 | sed -i '/phonehome.sh/d' $CRONTAB | ||
46 | echo "$CRON_ENTRY" >> $CRONTAB | ||
47 | fi | ||
48 | fi | ||
49 | |||
50 | if ! pgrep autossh > /dev/null; then | ||
51 | start_autossh $HOST_IP | ||
52 | fi | ||