Advertisement
Guest User

iptables-strace

a guest
Jun 12th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.67 KB | None | 0 0
  1. #!/bin/bash
  2. # $Id: iptables-trace,v 1.6 2003/03/27 00:51:38 apc Exp $
  3. # Author: Tony Clayton <tony-netfilter@enfusion-group.com>
  4.  
  5. # You may use and edit this code freely.  If you make changes to
  6. # it that are generally useful, please email them to me and/or
  7. # post them on the netfilter mailing list.
  8.  
  9. LOGPREFIX='${table:0:1}:${chain:0:14}:$rulenum:${target:0:14}'
  10.  
  11. log_entry() {
  12.   local action=$1
  13.   local table=$3 chain=$5
  14.   shift 5
  15.   if [ "$last_chain" != "$chain" ]; then
  16.     rulenum=1
  17.   fi
  18.   case $action in
  19.   (skip) ;;
  20.   (add)
  21.     local rulespec
  22.     while [ "$1" != "-j" ]; do
  23.       rulespec="$rulespec $1"
  24.       shift;
  25.     done
  26.     shift;
  27.     target=$*
  28.     eval prefix="${LOGPREFIX}"
  29.     iptables -t $table -I $chain $rulenum $rulespec -j LOG \
  30.        --log-level debug --log-prefix "*${prefix:0:27}:"
  31.     let rulenum=$rulenum+1
  32.     ;;
  33.   (delete)
  34.     iptables -t $table -D $chain $rulenum
  35.     let rulenum=$rulenum-1
  36.     ;;
  37.   esac
  38.   last_chain=$chain
  39. }
  40.  
  41. start() {
  42.   for table in $(cat /proc/net/ip_tables_names); do
  43.     rulenum=1
  44.     iptables-save -t $table | grep '^-' | \
  45.       while read cmd; do
  46.     log_entry add -t $table $cmd
  47.         let rulenum=$rulenum+1
  48.       done
  49.   done
  50. }
  51.  
  52. stop() {
  53.   for table in $(cat /proc/net/ip_tables_names); do
  54.     iptables-save -t $table | grep '^-' | \
  55.       while read cmd; do
  56.     echo $cmd | grep -q -e '--log-prefix "*'
  57.     if [ $? -eq 0 ]; then
  58.           log_entry delete -t $table $cmd
  59.         else
  60.       log_entry skip -t $table $cmd
  61.     fi
  62.         let rulenum=$rulenum+1
  63.       done
  64.   done
  65. }
  66.  
  67. case "$1" in
  68.   start) start
  69.     ;;
  70.   stop) stop
  71.     ;;
  72.   *) echo $"Usage: $0 {start|stop}"
  73.      exit 1
  74. esac
  75.  
  76. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement