diff options
Diffstat (limited to 'home/common/scripts/knock')
-rwxr-xr-x | home/common/scripts/knock | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/home/common/scripts/knock b/home/common/scripts/knock new file mode 100755 index 0000000..fdff4ca --- /dev/null +++ b/home/common/scripts/knock @@ -0,0 +1,50 @@ +#!/usr/bin/env zsh + +# load module to parse command line arguments +zmodload zsh/zutil +zparseopts -D -E -A opts -- h x + +# load module to avoid use of GNU sleep +zmodload zsh/zselect + +# enable XTRACE shell option for full debugging output of scripts +if (( ${+opts[-x]} )); then + set -x +fi + +if [[ -z "${2}" ]] || (( ${+opts[-h]} )); then + echo "usage: ${0:t} [ -h ] [ -x ] host port [ knock_port ] .." >&2 + echo -e '\n\t-h\tshow this help\n\t-x\tenable shell debugging' >&2 + echo -e '\thost\tdestination host name' >&2 + echo -e '\tport\tdestination service port\n' >&2 + echo -e 'Specifying no knock_port(s) will use 12345 23456 34567 45678 by default.\n' >&2 + exit 1 +fi + +host="${1}" +port="${2}" +shift 2 +knock_ports="${@:-12345 23456 34567 45678}" +attempts=1 + +function check_service_port { + if nc -w1 ${host} ${port} &> /dev/null <& -; then + exit 0 + fi +} + +#check_service_port + +while [[ ${attempts} -lt 9 ]]; do + + for knock_port in ${=knock_ports}; do + nc -w1 ${host} ${knock_port} &> /dev/null <& - & + zselect -t ${attempts}0 + done + + check_service_port + ((attempts+=1)) + +done + +exit 1 |