Created
December 18, 2018 09:03
-
-
Save Dragas/a7f2ed88ec16c5db76663abc691499cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### BEGIN INIT INFO | |
# Provides: zbw_connect | |
# Required-Start: $all | |
# Required-Stop: $all | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: zbw_connect | |
# Description: the script to connect to zbw server | |
### END INIT INFO | |
PIDFILE=/var/run/zbw_connect.pid | |
# test a writable of /tmp | |
if ! touch /tmp/.zbw_connect_rw_test; | |
then | |
echo "/tmp is not writable" >&2 | |
exit 1 | |
fi | |
rm -f /tmp/.zbw_connect_rw_test 2>/dev/null || true | |
# get a user password | |
PASSWORD=`cat /etc/zbw/passwd` | |
if [[ -z $PASSWORD ]]; | |
then | |
echo "Didn't find passwd file" >&2 | |
exit 1 | |
fi | |
# get a local port | |
LOCAL_PORT=`cat /etc/zbw/local_port` | |
if [[ -z $LOCAL_PORT ]]; | |
then | |
echo "Didn't find local_port file" >&2 | |
exit 1 | |
fi | |
# get a box type | |
BOXTYPE=`cat /etc/z-way/box_type` | |
[[ -r /lib/lsb/init-functions ]] && . /lib/lsb/init-functions | |
# If script is executed as an init script | |
case "$1" in | |
start) | |
log_daemon_msg "Starting zbw_connect" | |
PID=`cat $PIDFILE 2>/dev/null` | |
if [[ $PID ]]; | |
then | |
NAME=`ps -Ao pid,comm | awk -v PID=$PID '$1 == PID && $2 ~ /zbw_connect/ { print $2 }'` | |
if [[ $NAME ]]; | |
then | |
echo "already running" | |
exit | |
fi | |
fi | |
(nohup setsid $0 >/dev/null 2>&1 &) | |
log_action_msg "ok" | |
exit | |
;; | |
stop) | |
log_daemon_msg "Stoping zbw_connect" | |
PID=`cat $PIDFILE 2>/dev/null` | |
if [[ $PID ]]; | |
then | |
for pid in `ps -Ao pid,comm | awk '$2 ~ /zbw_connect/ { print $1 }'`; | |
do | |
[[ $pid -eq $PID ]] && kill -TERM -$pid && break | |
done | |
fi | |
rm -f $PIDFILE | |
rm -f /tmp/zbw_connect.priv | |
log_action_msg "ok" | |
exit 0 | |
;; | |
restart) | |
$0 stop | |
$0 start | |
exit | |
;; | |
restart_with_delay) | |
(nohup setsid $0 _restart_delayed $2 >/dev/null 2>&1 &) | |
exit | |
;; | |
_restart_delayed) | |
sleep $2 | |
$0 stop | |
$0 start | |
exit | |
;; | |
esac | |
# Can we run? | |
[[ -f /etc/zbw/flags/no_connection ]] && exit 0 | |
echo $$ > $PIDFILE | |
# Extract a private key | |
offset=`sed -e '/^START_OF_EMBEDDED_DATA$/ q' $0 | wc -c` | |
touch /tmp/zbw_connect.priv | |
chmod 0600 /tmp/zbw_connect.priv | |
dd if=$0 of=/tmp/zbw_connect.priv bs=$offset skip=1 >/dev/null 2>&1 | |
# Some constants | |
SERVER="find.z-wave.me" | |
SSH_USER="remote" | |
# Make forward opts string | |
FWD_OPTS="-R 0.0.0.0:10000:127.0.0.1:$LOCAL_PORT" | |
if [[ -f /etc/zbw/flags/forward_ssh ]]; | |
then | |
FWD_OPTS="$FWD_OPTS -R 0.0.0.0:10001:127.0.0.1:22" | |
fi | |
function get_local_ips() | |
{ | |
# Get local ips | |
if [[ -x `which ip` ]]; then | |
LOCAL_IPS=`ip a | sed -nre 's/^\s+inet ([0-9.]+).+$/\1/; T n; p; :n'` | |
elif [[ -x `which ifconfig` ]]; then | |
LOCAL_IPS=`ifconfig | sed -nre 's/^\s+inet addr:([0-9.]+).+$/\1/; T n; p; :n'` | |
else | |
echo Can\'t get local ip addresses >&2 | |
logger -t zbw_connect Can\'t get local ip addresses | |
exit 1 | |
fi | |
# i think filtering out only 127.0.0.1 address is sufficient | |
ZBW_INTERNAL_IP="" | |
for i in $LOCAL_IPS; do | |
if [[ $i != "127.0.0.1" ]]; then | |
if [[ $ZBW_INTERNAL_IP ]]; then | |
ZBW_INTERNAL_IP="$ZBW_INTERNAL_IP,$i"; | |
else | |
ZBW_INTERNAL_IP="$i"; | |
fi | |
fi | |
done | |
} | |
export ZBW_PASSWORD=$PASSWORD | |
export ZBW_INTERNAL_IP | |
export ZBW_INTERNAL_PORT=$LOCAL_PORT | |
export ZBW_BOXTYPE=$BOXTYPE | |
while true | |
do | |
get_local_ips | |
ssh -i /tmp/zbw_connect.priv -T -o 'StrictHostKeyChecking no' -o 'UserKnownHostsFile /dev/null' -o 'BatchMode yes' -o 'SendEnv ZBW_*' -o "ExitOnForwardFailure yes" -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" $FWD_OPTS $SSH_USER@$SERVER | |
sleep 1 | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment