Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ssh port knock script
!/bin/bash
#Purpose: Open/close SSH port via port knock
#Author: Mohan Balasundaram
#Date: 24/04/2015
NC="/bin/nc"
OPT="-z -w3"
PORT_OPEN_ORDER="10010 11020 12030"
PORT_CLOSE_ORDER="12030 11020 10010"
HOST="192.168.1.250" #jumpbox host
SSH_PORT="22"
if [ "$#" -ne 1 ]; then
echo "Usage: $0 open|close"
exit 2;
fi
if [[ $1 =~ ^([oO][pP][eE][nN])$ ]]; then
echo "Opening SSH Port"
for PORT in ${PORT_OPEN_ORDER}; do
${NC} ${OPT} ${HOST} ${PORT}
done
sleep 2;
echo "Checking SSH Port"
${NC} ${OPT} ${HOST} ${SSH_PORT}
elif [[ $1 =~ ^([cC][lL][oO][Ss][eE])$ ]]; then
echo "Closing SSH Port"
for PORT in ${PORT_CLOSE_ORDER}; do
${NC} ${OPT} ${HOST} ${PORT}
done
sleep 1;
echo "Checking SSH Port"
${NC} ${OPT} ${HOST} ${SSH_PORT} || echo "Connection to ${HOST} ${SSH_PORT} port [tcp/ssh] FAILED!"
else
echo "Invalid Input: $1"
echo "Usage: $0 open|close"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment