-
Notifications
You must be signed in to change notification settings - Fork 18
/
run.sh
95 lines (76 loc) · 2.63 KB
/
run.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
#!/usr/bin/with-contenv bashio
set -e
CONFIG_PATH=/data/options.json
KEY_PATH=/data/ssh_keys
HOSTNAME=$(jq --raw-output ".hostname" $CONFIG_PATH)
SSH_PORT=$(jq --raw-output ".ssh_port" $CONFIG_PATH)
USERNAME=$(jq --raw-output ".username" $CONFIG_PATH)
REMOTE_FORWARDING=$(jq --raw-output ".remote_forwarding[]" $CONFIG_PATH)
OTHER_SSH_OPTIONS=$(jq --raw-output ".other_ssh_options" $CONFIG_PATH)
FORCE_GENERATION=$(jq --raw-output ".force_keygen" $CONFIG_PATH)
#
if [ "$FORCE_GENERATION" != "false" ]; then
bashio::log.info "Deleting existing key pair due to set 'force_keygen'"
bashio::log.warning "Do not forget to unset 'force_keygen' in your add-on configuration"
rm -rf "$KEY_PATH"
fi
if [ ! -d "$KEY_PATH" ]; then
bashio::log.info "No previous key pair found"
mkdir -p "$KEY_PATH"
ssh-keygen -b 4096 -t rsa -N "" -C "hassio-setup-via-autossh" -f "${KEY_PATH}/autossh_rsa_key"
bashio::log.info "The public key is:"
cat "${KEY_PATH}/autossh_rsa_key.pub"
bashio::log.warning "Add this key to '~/.ssh/authorize_keys' on your remote server now!"
bashio::log.warning "Please restart add-on when done. Exiting..."
exit 1
else
bashio::log.info "Authentication key pair restored"
fi
bashio::log.info "The public key is:"
cat "${KEY_PATH}/autossh_rsa_key.pub"
bashio::log.info "Add to '~/.ssh/authorize_keys' on your remote server"
#
if [ -z "$HOSTNAME" ]; then
bashio::log.error "Please set 'hostname' in your config to the address of your remote server"
exit 1
fi
TEST_COMMAND="/usr/bin/ssh "\
"-o BatchMode=yes "\
"-o ConnectTimeout=5 "\
"-o PubkeyAuthentication=no "\
"-o PasswordAuthentication=no "\
"-o KbdInteractiveAuthentication=no "\
"-o ChallengeResponseAuthentication=no "\
"-o StrictHostKeyChecking=no "\
"-p ${SSH_PORT} -t -t "\
"test@${HOSTNAME} "\
"2>&1 || true"
if eval "${TEST_COMMAND}" | grep -q "Permission denied"; then
bashio::log.info "Testing SSH connection... SSH service reachable on remote server"
else
eval "${TEST_COMMAND}"
bashio::log.error "SSH service can't be reached on remote server"
exit 1
fi
bashio::log.info "Remote server host keys:"
ssh-keyscan -p $SSH_PORT $HOSTNAME || true
#
COMMAND="/usr/bin/autossh "\
" -M 0 -N "\
"-o ServerAliveInterval=30 "\
"-o ServerAliveCountMax=3 "\
"-o StrictHostKeyChecking=no "\
"-o ExitOnForwardFailure=yes "\
"-p ${SSH_PORT} -t -t "\
"-i ${KEY_PATH}/autossh_rsa_key "\
"${USERNAME}@${HOSTNAME}"
if [ ! -z "${REMOTE_FORWARDING}" ]; then
while read -r LINE; do
COMMAND="${COMMAND} -R ${LINE}"
done <<< "${REMOTE_FORWARDING}"
fi
COMMAND="${COMMAND} ${OTHER_SSH_OPTIONS}"
bashio::log.info "Executing command: ${COMMAND}"
/usr/bin/autossh -V
# Execute
exec ${COMMAND}