-
Notifications
You must be signed in to change notification settings - Fork 5
/
darknet_duck.sh
executable file
·52 lines (46 loc) · 1.26 KB
/
darknet_duck.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
#!/bin/bash
# Check for the requesite number of arguments
if [[ $# -lt 2 || $# -gt 2 ]];
then
echo "";
echo "Invalid number of arguments: $0 $@";
echo "usage: ${0} <host> <port>";
exit -1;
fi
HOST=${1};
PORT=${2};
TCP_TIMEOUT=1;
# Create a subshell for the commands
# Using () instead of $() or `` is important because the script continues without waiting for the output
# http://stackoverflow.com/questions/19462291/bash-subshell-parenthese-vs-dollar-parenthese
(
# Sleep for the timeout and kill the process if it still exists
(
sleep ${TCP_TIMEOUT};
if [ -d /proc/$$ ];
then
logger "****** Darknet unavailable *******";
logger "Killing any instances of tincd";
pkill tincd;
sleep 2;
logger "Starting tincd*";
/sbin/tincd -n hades;
logger "${0} is done";
kill $$;
fi
) &
# Nifty tcp check bash builtin
# http://stackoverflow.com/questions/23421917/bash-script-telnet-to-test-multiple-addresses-and-ports
exec 3<> /dev/tcp/${HOST}/${PORT};
) 2>/dev/null
# Evaluate the return code of the subshell
case $? in
0) # tcp connection succeeded
logger "Darknet is functional";
exit 0;
;;
1) # tcp connection was refused
logger "Darknet is up, ${HOST} refused connection";
exit 1;
;;
esac