Skip to content

Instantly share code, notes, and snippets.

@moregeek
Last active December 29, 2015 02:18
Show Gist options
  • Save moregeek/7599055 to your computer and use it in GitHub Desktop.
Save moregeek/7599055 to your computer and use it in GitHub Desktop.
## INSTALL
curl -o ~/bin/knockp https://gist.github.com/moregeek/7599055/raw/knockp
chmod +x ~/bin/knockp
## USAGE
### With ssh config (~/.ssh/config)
host example.org
ProxyCommand sh -c "~/bin/knockp -s example.org 2000 3000 4000; nc %h %p"
### Without ssh config
~/bin/knockp example.org 2000 3000 4000
#!/usr/bin/env bash
################################################################################
#
# knockp / simple portknocking helper script
#
# Copyright 2013 by Stefan Morgenthaler <stefan_at_morgenthaler_dot_at>
#
# Licensed under GNU General Public License 3.0 or later.
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
#
################################################################################
function _help () {
echo "usage: pknock [arguments] {HOST} {port: n n-1}";
echo "";
echo "arguments:";
echo " -s|silent just knock, do not show output on screen";
echo " -h|help show this help text";
exit 0;
}
function _knock () {
if ! [ -o ${_silent} ]; then
exec > /dev/null 2>&1
fi
host="$1"; shift;
echo "knocking host: ${host}..."
for port in "${@}"; do
echo "=> ${port}... "
nmap -Pn --host_timeout 100 --max-retries 0 -p ${port} ${host} > /dev/null 2&>1
done
exit 0;
}
function _check_dependency () {
which nmap > /dev/null 2>&1
if [[ $? -gt 0 ]]; then
echo "Please install dependency: 'nmap'!";
fi
exit 1;
}
function main () {
_check_dependency;
while [[ "$1" =~ ^[-]{1,2}[a-z] ]]; do
case ${1/*[-]} in
s|silent) shift; _silent=0; ;;
h|help) shift; _help ;;
*) echo "Unknown option! See 'knockp --help'";
exit 1;
esac
done;
if [ $# -lt 3 ]; then
echo "Missing argument! See 'knockp --help'";
exit 1;
fi
_knock "$@"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment