Register now and start sharing your code snippets.
-->

Flushing iptables

Shell Script (Bash) posted about 1 year ago by marko

Reset all rules of iptables. Handy to put in the root’s crontab while configuring a remote firewall.

   1  #!/bin/sh
   2  # 
   3  # rc.flush-iptables - Resets iptables to default values. 
   4  # 
   5  # Copyright (C) 2001  Oskar Andreasson <bluefluxATkoffeinDOTnet>
   6  #
   7  # This program is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; version 2 of the License.
  10  #
  11  # This program is distributed in the hope that it will be useful,
  12  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  # GNU General Public License for more details.
  15  #
  16  # You should have received a copy of the GNU General Public License
  17  # along with this program or from the site that you downloaded it
  18  # from; if not, write to the Free Software Foundation, Inc., 59 Temple
  19  # Place, Suite 330, Boston, MA  02111-1307   USA
  20  
  21  #
  22  # Configurations
  23  #
  24  IPTABLES="/sbin/iptables"
  25  
  26  #
  27  # reset the default policies in the filter table.
  28  #
  29  $IPTABLES -P INPUT ACCEPT
  30  $IPTABLES -P FORWARD ACCEPT
  31  $IPTABLES -P OUTPUT ACCEPT
  32  
  33  #
  34  # reset the default policies in the nat table.
  35  #
  36  $IPTABLES -t nat -P PREROUTING ACCEPT
  37  $IPTABLES -t nat -P POSTROUTING ACCEPT
  38  $IPTABLES -t nat -P OUTPUT ACCEPT
  39  
  40  #
  41  # reset the default policies in the mangle table.
  42  #
  43  $IPTABLES -t mangle -P PREROUTING ACCEPT
  44  $IPTABLES -t mangle -P POSTROUTING ACCEPT
  45  $IPTABLES -t mangle -P INPUT ACCEPT
  46  $IPTABLES -t mangle -P OUTPUT ACCEPT
  47  $IPTABLES -t mangle -P FORWARD ACCEPT
  48  
  49  #
  50  # flush all the rules in the filter and nat tables.
  51  #
  52  $IPTABLES -F
  53  $IPTABLES -t nat -F
  54  $IPTABLES -t mangle -F
  55  #
  56  # erase all chains that's not default in filter and nat table.
  57  #
  58  $IPTABLES -X
  59  $IPTABLES -t nat -X
  60  $IPTABLES -t mangle -X
  61  

Tagged iptables, firewall, linux

Log file analysis with AWK - Calculating the sum and average

Shell Script (Bash) posted about 1 year ago by christian

This AWK script is useful when you want to calculate the average and sum for a set of values found in a log file.

   1  awk '{ s += $1 } END { print "sum: ", s, " average: ", s/NR, " samples: ", NR }' rails_production_log_or_whatever.log

Note that $1 means that column one contains the values you want to use. NR is the total number of rows in the file. As an example, let’s say you have this log file:

   1  1
   2  2
   3  3
   4  4
   5  5

The output would then be:

   1  sum:  15  average:  3  samples:  5

Combine it with grep or sed to do more advanced log file analysis —you can for example calculate the average time it took to render action xyz in Rails on the 21th of July at 21:00 PM.

Note that by default the column values should be space separated—use the following switch to parse CSV (comma separated) files: -F,

Tagged awk, shell, performance, average, sum

Find a text pattern in jar files

Shell Script (Bash) posted about 1 year ago by marko

Helpful when you need to find a class or package in some jar file recursively below the current directory. Still needs a test to see if the file found was a file or directory. Works case insensitively. Uses the unzip command because of it’s performance superiority in comparison to jar.

   1  #!/bin/sh
   2  for f in `find . -type f -name '*\.jar'`
   3  do
   4          unzip -l $f | grep -i $1 && echo "was found in $f"
   5  done

Tagged jar, find, search, recursive, linux, unzip

Screenshot in wmii

Shell Script (Bash) posted about 1 year ago by marko

Method 1

A simple script to create a screenshot in wmii. It probably works in other window managers too. I call it ‘scrot’. Put it in the path and run it as you’d run any program in wmii. The “import” program comes with imagemagick.

   1  #!/bin/bash
   2  import -window root /tmp/screenshot.png

Method 2

If you want timestamped screenshots then

   1  apt-get install scrot
   2  mkdir -p ~/screenshots

And create a script with the following content for making the screenshot.

   1  #!/bin/bash
   2  scrot '%Y-%m-%d-%H-%M-%S_$wx$h_scrot.png' -e 'mv $f ~/screenshots'

Tagged wmii, screenshot, linux, imagemagick

SSH tunneling

Shell Script (Bash) posted about 1 year ago by marko

SSH tunneling to bypass overly strict firewalls for services you need. The first port is that of the remote service, and the latter is the port you want to use on the local computer. For clarity I usually use the same port on both computers if possible. Here we initiate a tunnel into the vnc server of a remote host.

   1  ssh -L 5905:localhost:5905 marko@remotehost

Next we open the vnc viewer into localhost, which in fact is the tunnel into the remote host.

   1  vncviewer localhost:5

Tagged ssh, tunneling, port, secure