Skip to content

Instantly share code, notes, and snippets.

@agazso
Created August 1, 2011 23:36
Show Gist options
  • Save agazso/1119252 to your computer and use it in GitHub Desktop.
Save agazso/1119252 to your computer and use it in GitHub Desktop.
Accept IP-address on SSH port for a timeout interval
#!/bin/sh
DEFAULT_TIMEOUT=60
ACCEPTSCRIPT=$0
if [ "$1" = "" ]; then
echo
echo "Usage:"
echo " acceptsship.sh <ip-address> [timeout=${DEFAULT_TIMEOUT}s]"
echo
exit 1
fi
IPADDR=$1
TIMEOUT=$2
# check validity of IP-Address
IP_REGEX='^\(\([0-9]\|[1-9][0-9]\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\([0-9]\|[1-9][0-9]\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)$'
IPADDR_CHECK=$(echo $IPADDR | sed s/$IP_REGEX//)
if [ "$IPADDR_CHECK" = "$IPADDR" ]; then
echo "Invalid IP-Address!"
exit 1
fi
# check validity of timeout
if [ "$TIMEOUT" = "" ]; then
TIMEOUT=$DEFAULT_TIMEOUT
fi
if [ "$TIMEOUT" -gt "300" ]; then
TIMEOUT=$DEFAULT_TIMEOUT
fi
if [ "$TIMEOUT" -le "0" ]; then
TIMEOUT=$DEFAULT_TIMEOUT
fi
TIMEOUT_CHECK=$(expr $TIMEOUT + 0)
if [ $? != 0 -o "$TIMEOUT_CHECK" != "$TIMEOUT" ]; then
echo "Timeout must be a numeric value!"
exit 1
fi
# check if the script is already running
pidof -o %PPID -x $ACCEPTSCRIPT > /dev/null
if [ $? != 1 ]; then
echo "Already running!"
exit 1
fi
echo "Accepting SSH connections from $IPADDR for $TIMEOUT seconds..."
acceptaddress()
{
iptables -I INPUT -s $IPADDR -p tcp -m tcp --dport 22 -j ACCEPT && \
sleep $TIMEOUT && \
iptables -D INPUT -s $IPADDR -p tcp -m tcp --dport 22 -j ACCEPT
}
acceptaddress &
# close standard output and exit
>&-
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment