
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 1.33 KB | hits: 8 | expires: Never
#!/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