Batch unzip 7z archives

Posted by marian on June 06, 2010

  1. #!/bin/bash
  2. for i in $(find $(pwd) -name '*.7z')
  3. do
  4.        cd $(dirname $i)
  5.        7z x  $i
  6. done

Batch unrar archives

Posted by marian on June 06, 2010

  1. #!/bin/bash
  2. for i in $(find $(pwd) -name '*.rar')
  3. do
  4.        cd $(dirname $i)
  5.        unrar e  $i
  6. done

Batch unzip archives

Posted by marian on June 06, 2010

  1. #!/bin/bash
  2. for i in $(find $(pwd) -name '*.zip')
  3. do
  4.        cd $(dirname $i)
  5.        unzip  $i
  6. done

Convert all *.ppt files in a directory to *.pdf (or any format suported by OpenOffice to PDF)

Posted by marian on January 20, 2010

I believe that everybody came across the need to convert some Microsoft Office document into PDF format. Under GNU/Linux this can be accomplished very simply.
If you are a student and all the courses are in PowerPoint and you want to read them as PDF under GNU/Linux using the awesome evince, then you will appreciate this tutorial. ( Question to the reader: Why does Evince look so good, while the GNU/Linux version of Acrobat Reader is so sluggish ?)
Steps to follow:
1) Install OpenOffice
2) Install Cups-PDF:

  1. sudo apt-get install cups-pdf (Ubuntu)
  2. sudo yum install cups-pdf (Fedora)

3) Create a PDF directory in your home directory if it doesn’t exist:

  1. mkdir ~/PDF

4) For converting a single *.ppt file to *.pdf you should use:

  1. soffice -norestore -nofirststartwizard -nologo -headless -pt PDF your_pdf_file.ppt

But this can be achieved easily using OpenOffice GUI.
Now I will show you how easy is to convert all the files in a directory to PDF files and then merge them in a single PDF file. You just have to go to the directory containing your files and then execute :

  1. find . -type f -name "*.ppt" -exec soffice -norestore -nofirststartwizard -nologo -headless -pt PDF {} \;

Find will search in the current directory for all the files (due to “-type f”) and having the extension ppt (due to -name “*.ppt”) and convert them into PDF files.
You might also use *.doc (Microsoft Word), and i think (not tested) that also *.pptx and *.docx (Microsoft Office 2007) could be used. The output files will be stored in the ~/PDF directory.

Now the PDF merging stage. There are many tools that can do this under Linux, but I prefer pdftk which can be installed using:

  1. sudo apt-get install pdftk (Ubuntu)
  2. sudo yum install pdftk (Fedora)

And here is the easiest way to merge all the PDF files in a directory:

  1. pdftk *.pdf output merged.pdf

where merged.pdf is the name of the merged file :) ).

A sample IPtables configuration script

Posted by marian on November 01, 2009

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# flush all chains
iptables -F
# set the default policy for each of the pre-defined chains
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
#qemu, kvm
iptables  -A POSTROUTING -s 192.168.122.0/24 -d ! 192.168.122.0/24 -j MASQUERADE
#create a new chain for reporting droped packets
iptables -N LOGDROP
iptables -A LOGDROP -j LOG
iptables -A LOGDROP -j DROP
# allow establishment of connections initialised by my outgoing packets
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
#qemu, kvm
iptables -A INPUT -i virbr0 -p udp -m udp --dport 53 -j ACCEPT
iptables -A INPUT -i virbr0 -p tcp -m tcp --dport 53 -j ACCEPT
iptables -A INPUT -i virbr0 -p udp -m udp --dport 67 -j ACCEPT
iptables -A INPUT -i virbr0 -p tcp -m tcp --dport 67 -j ACCEPT
iptables -A INPUT -p tcp --dport 111 -j DROP
# accept anything on localhost
iptables -A INPUT -i lo -j ACCEPT
#!!!!#drop everything else
iptables -A INPUT -j LOGDROP
#qemu, kvm
iptables -A FORWARD -d 192.168.122.0/24 -o virbr0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 192.168.122.0/24 -i virbr0 -j ACCEPT
iptables -A FORWARD -i virbr0 -o virbr0 -j ACCEPT
iptables -A FORWARD -o virbr0 -j REJECT --reject-with icmp-port-unreachable
iptables -A FORWARD -i virbr0 -j REJECT --reject-with icmp-port-unreachable
#!!!!#drop everything else
iptables -A OUTPUT -p tcp -m tcp --dport 5432 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 8443 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 5432 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 8080 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 1900 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 135 -j  DROP
iptables -A OUTPUT -p tcp -m tcp --dport 435 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 631 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 111 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 25 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 4444 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 8099 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 6000 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 2049 -j DROP
iptables -A OUTPUT -p tcp -m tcp --dport 5901 -j ACCEPT
iptables -A OUTPUT -j ACCEPT
#iptables -A LOGDROP -m limit --limit 1/sec -j LOG --log-prefix "iptables denied: " --log-level 7
#iptables -A LOGDROP -j DROP

Finding which process has opened a specific help

Posted by marian on October 18, 2009

There are a number of situations when one has to know the PID of the process that opened a port; this situations include, but are not limited to:

  • you design Web Applications, someting has when terribly wrong, and you need to kill the webserver; you can either search for its PID or you can use the fuser command:
    1. fuser -n tcp 80

    As a matter of fact, I have to kill Tomcat quite often.
    Another situation when you need to know the PID is when you think that your computer is being gacked :)

Genrating random password

Posted by marian on October 11, 2009

For generating a random password containing hex  digits one can use:

hexdump -n32 -e '"%x"' /dev/random

where 32 is the length of the string, and the format is ‘”%x”.