Archive

Archive for the ‘Shell’ Category

Spamhaus droplist

April 29th, 2009

 Powered by Max Banner Ads 
#!/bin/bash
 
#Script to add firewall rules to a linux system to completely block
#all traffic to and from networks in the spamhaus drop list.
 
#Copyright 2009, William Stearns, wstearns@pobox.com
#Released under the GPL.  This and other tools can be found at
#http://www.stearns.org/
 
#Sole (optional) command line parameter is the file location of the
#drop list, such as:
 
#cd /var/lib/
#wget http://www.spamhaus.org/drop/drop.lasso
# ./spamhaus-drop /var/lib/drop.lasso
 
#While the DROP file should be regularly updated, this should
#probably be about once per day or less frequently; do _not_
#download DROP more than once an hour.
 
if [ -n "$1" ]; then
	DropList="$1"
else
	DropList="./drop.lasso"
fi
if [ ! -s "$DropList" ]; then
	echo "Unable to find drop list file $DropList .  Perhaps do:" >&2
	echo "wget http://www.spamhaus.org/drop/drop.lasso -O $DropList"
	echo "exiting." >&2
	exit 1
fi
 
if [ ! -x /sbin/iptables ]; then
	echo "Missing iptables command line tool, exiting." >&2
	exit 1
fi
 
cat "$DropList" \
 | sed -e 's/;.*//' \
 | grep -v '^ *$' \
 | while read OneNetBlock ; do
	/sbin/iptables -I INPUT -s "$OneNetBlock" -j DROP
	/sbin/iptables -I OUTPUT -d "$OneNetBlock" -j DROP
	/sbin/iptables -I FORWARD -s "$OneNetBlock" -j DROP
	/sbin/iptables -I FORWARD -d "$OneNetBlock" -j DROP
done

Fabio Laganà Debian, Linux, Shell, Uncategorized, bash, droplist, sed, spam, spamhaus

Eseguire il backup delle directory di un server

April 28th, 2009

nome script: /usr/local/sbin/backup_dir.sh

descrizione: Con questo script viene fatto il backup delle directory importanti server

Inserendo una riga simile nel cron scheduliamo il backup tutte le notti

cat /etc/cron.d/backup
MAILTO="postmaster@yourdomain.com"
00 21 * * * root test -x /usr/local/sbin/backup_dir.sh && /usr/local/sbin/backup_dir.sh
#!/bin/bash
#
# backup_dir.sh
# Con questo script viene fatto il backup delle directory importanti  server
#
hosts_internal=( your_host )
 
for host in ${hosts_internal[@]}
do
  echo "Download of /etc/ /usr/local/sbin/ /root/ /home/ directory from "$host" "
  echo -n "Starting ------>"
  date
  # bakup file for hosts
  tar -zcvf /var/backups/backup_files_"$host"_`date +"%d%m%Y"`.tar.gz /etc/ /usr/local/sbin/ /root/ /home/ ;
  mv /var/backups/backup_files_"$host"_`date +"%d%m%Y"`.tar.gz /backups/"$host"/;
  rm -f /var/backups/backup_files_"$host"_`date +"%d%m%Y"`.tar.gz
  echo -n "Stopping ------>"
  date
  echo ""
done

Fabio Laganà Debian, Shell, backup, bash , , ,

Count domain in mail.log (Qmail)

February 20th, 2009
# cat /usr/local/sbin/count_mail_per_domain.sh
#!/bin/bash
logfile=$2
domain=$3
 
case "$1" in
        -t)
	cat $logfile |grep "to remote" | awk '{print $14}'
					| awk -F@ '{ field = $2 }; {print "domain: " field }'
					| sort |uniq -c |sort -nr
					| awk -v totale=0 '{totale=totale+$1; print ;} END {print "Totale giorno domini: "totale;}'
            ;;
 
        -su)
	cat $logfile |grep "to remote"  |grep -i "$domain"
					|awk ' { field = $14 }; {print "to remote: " field }'
					| sort| uniq -c| sort -nr
					| awk -v totale=0 '{totale=totale+$1; print ;} END {print "Totale giorno domini per utente: "totale;}'
            ;;
	-s)
        cat $logfile |grep "to remote"  |grep -i "$domain" |awk '{print $14}'
                                        | awk -F@ '{ field = $2 }; {print "to remote domain: " field }'
                                        | sort| uniq -c| sort -nr
                                        | awk -v totale=0 '{totale=totale+$1; print ;} END {print "Totale giorno domini: "totale;}'
 
         ;;
        *)
            echo $"Usage: $0 { -t /var/log/mail.log.0 }  for print total mail for domain "
            echo $"Usage: $0 { -su /var/log/mail.log.0 domain.com } for print total mail for single domain per user"
            echo $"Usage: $0 { -s /var/log/mail.log.0 domain.com } for print total mail for single domain"
            exit 1
 
esac

for more tips visit qmail relay to smarthost

Fabio Laganà Debian, Mailserver, Qmail, Shell, Uncategorized, awk, bash , , , , ,

Add and Remove ip alias into OpenBSD System

December 5th, 2008

Set an IP Alias

To put a second IP address to the same interface, just enter on the commandline as root:

 OpenBSD# ifconfig re0 inet alias 192.168.10.199 netmask 255.255.255.0

This sets an IP address 192.168.10.199 to vr0. Check again with ifconfig:

 OpenBSD# ifconfig -A
 lo0: flags=8049 mtu 33208
         groups: lo
         inet 127.0.0.1 netmask 0xff000000
         inet6 ::1 prefixlen 128
         inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
 re0: flags=8843 mtu 1500
         lladdr 00:40:45:28:89:37
         groups: egress
         media: Ethernet autoselect (100baseTX full-duplex)
         status: active
         inet 192.168.1.199 netmask 0xffffff00 broadcast 192.168.1.255
         inet6 fe80::240:45ff:fe28:8937%vr0 prefixlen 64 scopeid 0x2
         inet 192.168.10.199 netmask 0xffffff00 broadcast 192.168.100.255

Now we see two IP addresses on vr0. If you want to set an IP address within the same network you would have to set a fake netmask of 255.255.255.255.

Make the IP Alias Permanent

If you use ifconfig to set an IP alias, the alias won’t be present after the next reboot. To make the setting permanent, add a line to /etc/hostname.<INTERFACE>:

 OpenBSD# vi /etc/hostname.vr0
 inet 192.168.1.199 255.255.255.0 NONE
         inet alias 192.168.10.199 255.255.255.0

Remove an IP Alias

You can remove an IP alias with a comand like this:

 OpenBSD# ifconfig re0 192.168.10.199 delete

This deletes the second IP address from the interface keeping the first.

Fabio Laganà OpenBSD, Shell , , , ,

Da immagine a disco via rete con netcat

September 4th, 2008

sul target host partire con una LIVE linux e mettere netcat in ascolto in questo modo…

  netcat -l -p 1234 | dd of=/dev/sda bs=16065b

sul master (dove risiede l’immagine gzippata)

  zcat immagine.gz | netcat 192.168.1.1 1234

Fabio Laganà Debian, Shell, netcat , ,

Bash script for checking mysql problem by reading daemon.log

August 6th, 2008
#!/bin/bash
 
### Checking del daemon.log per eventuali problemi sul DB MySQL
# rel. 0.3 - 01 Agosto 2008
 
# time_to_check_min=`date +"%b %d %k:%M" -d "-1 min"`
# time_to_check_hour=`date +"%b %d %k:%M" -d "-1 hour"`
file_to_check=/var/log/daemon.log
tmp_file=/tmp/info.tmp.01.$$
tmp_file1=/tmp/info.tmp.02.$$
host=`cat /etc/hostname`
semaphore=/tmp/.flag_checking_daemon_log
 
daemon=mysql
 
if [[ -e $semaphore ]];
  then
    echo "checking for mysqld....Nothing to do ....another cron is still working"
  else
    touch $semaphore
if [[ `date +"%d"` &lt; '10' ]]; then
    time_to_check_min=`date +"%b %d %k:%M" -d "-1 min"| sed -r 's/[[:space:]](0)([1-9])[[:space:]]/  2 /'`
    time_to_check_hour=`date +"%b %d %k:%M" -d "-1 hour"| sed -r 's/[[:space:]](0)([1-9])[[:space:]]/  2 /'`
else
    time_to_check_min=`date +"%b %d %k:%M" -d "-1 min"`
    time_to_check_hour=`date +"%b %d %k:%M" -d "-1 hour"`
fi
#echo "checking for ""$file_to_check  $time_to_check_min"
grep "$time_to_check_min" "$file_to_check" &gt; $tmp_file
grep "$daemon" "$tmp_file" &gt; $tmp_file1
if [[ -e $tmp_file1 ]];
    then
        if [[ -s $tmp_file1 ]]; then
            mail -s "Something happening on [ $daemon ] for ""$host" tech-c@bakeca.it &lt; $tmp_file1
        fi
        #remove the tmp_file
        rm -f $tmp_file
        rm -f $tmp_file1
    fi
   #removing the semaphore
   rm -f $semaphore
fi

Fabio Laganà Debian, Linux, Shell, bash, mysql , , , , ,

Another trick for cloning

July 10th, 2008
   For a beowulf of full fledged Debian systems (local disk, no shared /usr
or anything like that) you'd need to use

prototype-node:
 dselect  (install and configure everything you want)
 dpkg --get-selections > selection.file

second node:

  dpkg --set-selections < selection.file
  apt-get install dselect-upgrade
  [log any questions and answers]
  [create an expect script with your responses to any questions]

rest-of-nodes:

   dpkg --set-selections < selection.file
  expect -c "apt-get install dselect-upgrade" -f expect-script

Fabio Laganà Debian, Linux, Shell, bash

checkinstall – trace your .tar.gz installations, and make/create your .deb files

June 9th, 2008

Debian / Ubuntu

sudo aptitude install checkinstall

Now as an example I will compile and install aterm which is a terminal emulator, I will write about it later.

Ok, get the code

wget ftp://ftp.afterstep.org/apps/aterm/aterm-1.0.1.tar.gz

Untar it

tar xvzf aterm-1.0.1.tar.gz

Change to its directory:

cd aterm-1.0.1

Now, usually at this point you have to enter three commands

./configure
make
make install

This time we will enter only the first two:

./configure

make

Now, here comes the magic, staying at the same directory change to root or use sudo

sudo checkinstall

You will see something like this, the first time:

checkinstall 1.6.1, Copyright 2002 Felipe Eduardo Sanchez Diaz Duran
           This software is released under the GNU GPL.

           The package documentation directory ./doc-pak does not exist.
           Should I create a default set of package docs?  [y]:
Preparing package documentation...OK
Please write a description for the package.
End your description with an empty line or EOF.
>> aterm 1.0.1
*****************************************
**** Debian package creation selected ***
*****************************************
This package will be built according to these values:

0 -  Maintainer: [ root@ggarron ]
1 -  Summary: [ aterm 1.0.1 ]
2 -  Name:    [ aterm ]
3 -  Version: [ 1.0.1 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ i386 ]
8 -  Source location: [ aterm-1.0.1 ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
Enter a number to change any of them or press ENTER to continue:
Copying files to the temporary directory...OK
Stripping ELF binaries and libraries...OK
Compressing man pages...OK
Building file list...OK
Building Debian package...OK
Installing Debian package...OK
Erasing temporary files...OK
Writing backup package...OK
Deleting temp dir...OK
**********************************************************************
 Done. The new package has been installed and saved to
 /home/ggarron/Desktop/borrar/aterm-1.0.1/aterm_1.0.1-1_i386.deb
 You can remove it from your system anytime using:
 dpkg -r aterm

Fabio Laganà Debian, Linux, Shell , , ,

cloning disk using dd

May 29th, 2008

from your machine to external usb disk:

dd if=/dev/&lt;your machine disk&gt; | gzip &gt; /path/nameofyourimg.gz

from your img saved to external usb disk:

zcat /path/nameofyourimg.gz | dd of=/dev/&lt;your machine disk&gt;

Fabio Laganà Debian, Linux, Shell , , , ,

Linux Variables

April 10th, 2008

When variables are used they are referred to with the $ symbol in front of them. There are several useful variables available in the shell program. Here are a few:

  • $$ = The PID number of the process executing the shell.
  • $? = Exit status variable.
  • $0 = The name of the command you used to call a program.
  • $1 = The first argument on the command line.
  • $2 = The second argument on the command line.
  • $n = The nth argument on the command line.
  • $* = All the arguments on the command line.
  • $# The number of command line arguments.

The “shift” command can be used to shift command line arguments to the left, ie $1 becomes the value of $2, $3 shifts into $2, etc. The command, “shift 2″ will shift 2 places meaning the new value of $1 will be the old value of $3 and so forth.

Fabio Laganà Linux, Shell ,