Skip to content

Instantly share code, notes, and snippets.

@arcticlinux
Last active May 4, 2017 05:36
Show Gist options
  • Save arcticlinux/564bb5c2de8217adfe5c to your computer and use it in GitHub Desktop.
Save arcticlinux/564bb5c2de8217adfe5c to your computer and use it in GitHub Desktop.
Port Knocking SSH wrapper
#!/bin/bash
# Author: Michael Best <mbest@arcticlinux.com>
# Designed to work with the Shorewall Port Knocking Implementation
# http://shorewall.net/PortKnocking.html
COMMAND="ssh"
PORT=1600
TIMEOUT=2
function USAGE {
echo -e "Usage for $0\n"
echo -e "By default this program knocks port 1600 with tcping, and then calls ssh"
echo -e "Example: kssh username@example.com\n "
echo "Options:"
echo "-c Sets the default ssh command, default ssh (ssh, mosh, etc)"
echo "-p Sets the port knocking port, default 1600"
echo "-t Sets the timeout value on tcping, default 2"
echo "-h Displays usage"
exit 1
}
while getopts :c:p:t:h OPT; do
case $OPT in
c) # set ssh command
COMMAND=$OPTARG
;;
p) # set knocking port
PORT=$OPTARG
;;
t) # set timeout
TIMEOUT=$OPTARG
;;
h) #show usage
USAGE
;;
\?)
echo -e "\nInvalid Option -$OPTARG\n"
USAGE
;;
esac
done
shift $((OPTIND-1))
HOST=`echo $1 | sed 's#.*@##'`
echo "knocking $HOST $PORT, $COMMAND $*"
tcping -q -t $TIMEOUT $HOST $PORT &> /dev/null
$COMMAND $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment