Advertisement
stubborn_d0nkey

Shell functions to close/open ports

Feb 3rd, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.01 KB | None | 0 0
  1. ################
  2. ################
  3. ### usage: openport [-p] port
  4. ### usage: closeport [-p PROTOCOL] port
  5. ### options:
  6. ### -p, --protocol : protocol (default=tcp)
  7. ################
  8. #
  9. #
  10. function openport () {
  11.    
  12.     protocol=tcp
  13.     OPTS=`getopt -o p: -l protocol: -- "$@"`
  14.    
  15.     if [ $? -ne 0 ];
  16.         then
  17.                 return 1
  18.         fi
  19.    
  20.     eval set -- "$OPTS"
  21.    
  22.     while true ; do
  23.         case "$1" in
  24.             -p|--protocol) protocol=$2; shift 2;;
  25.             --) shift; break;;
  26.             *) break;;
  27.         esac
  28.     done
  29.        
  30.     port=$1
  31.     sudo iptables -A INPUT -p $protocol --dport $port -j ACCEPT; }
  32.  
  33.     function closeport () {
  34.     protocol=tcp
  35.     OPTS=`getopt -o p: -l protocol: -- "$@"`
  36.    
  37.     if [ $? -ne 0 ];
  38.         then
  39.                 return 1
  40.         fi
  41.    
  42.     eval set -- "$OPTS"
  43.    
  44.     while true ; do
  45.         case "$1" in
  46.             -p|--protocol) protocol=$2; shift 2;;
  47.             --) shift; break;;
  48.             *) break;;
  49.         esac
  50.     done
  51.        
  52.     port=$1
  53.     sudo iptables -A INPUT -p $protocol --dport $port -j REJECT; }
  54. #############################################3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement