I love Free Software!

Pages

Funny Quotes to Think

When you say "I wrote a program that crashed Windows", people just stare at You blankly and say "Hey, I got those with the system, for free"

-Torvalds, Linus(1995-03-08)-

Sunday, September 4, 2011

Install Kernel 3.0.4

It is always good to say that you have latest kernel running in your machine. I have upgraded kernel from debian experimental and testing repositories . That was simple and straight forward. But you get real happy when you build a kernel from the source.
This was my first experience in upgrading kernel to 3.0.4 from source and even I don't know whether I can successfully complete or not. But I did this and love to share my experience with all you guys!

Before dive in to this just walk-through this document to learn about kernel.

Download the latest kernel from Linux Kernel Archive 

# wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.0.4.tar.bz2

Extract the kernel
#  bzip2 -dc linux-3.X.tar.bz2 | tar xvf -

Move to the directory where kernel source code is extracted
# mv linux-3.0.4/

Configure your kernel
Use make menuconfig or make config for configuring your kernel. I tried make menuconfig this will give you text based menus
# make menuconfig

Make the kernel 
# make bzImage 

Making kernel modules 
# make modules

This will compile all the modules for kernel 3.0.1


Install the modules compiled
# make modules_install
This will copy all kernel modules to /lib/modules/3.0.4.

Install the new kernel 
# make install
This will copy files vmlinuz-3.0.4, initrd.img-3.0.4 and config-3.0.4 to your /boot directory.
Execute below command in your shell, if initrd.img-3.0.4 is not created.
# update-initramfs -u -k 3.0.4

Configure your Grub
This document explains how to configure new kernel in grub2. grub users please excuse. Check below link to learn more about grub2

Edit /etc/grub.d/40_custom file and add below configuration for new menu item. Please make sure that you are not removing anything which already present in this file

menuentry 'Ubuntu, with Linux 3.0.4' --class ubuntu --class gnu-linux --class gnu --class os {
        recordfail
        insmod ext2
        set root='(hd0,1)'
        search --no-floppy --fs-uuid --set c933488a-c517-465e-9ecf-4744806b3a7a
        linux   /boot/vmlinuz-3.0.4 root=UUID=c933488a-c517-465e-9ecf-4744806b3a7a ro   quiet splash
        initrd  /boot/initrd.img-3.0.4
}

c933488a-c517-465e-9ecf-4744806b3a7a is the UUID. replace this ID with yours. For more information about UUID, wikipedia is the good source.
Use below command to find the UUID
# blkid

Update your grub configuration
# update-grub

Reboot with new kernel
Now reboot your machine. Once you boot with new kernel, execute uname -a in your shell to find the kernel version.

Linux pranavam 3.0.4 #1 SMP Sun Sep 4 12:28:48 IST 2011 i686 GNU/Linux

Wednesday, April 27, 2011

Quota report using LDAP and Dict

This script will export all LDAP information including current quota usage. You should use quota dict for quota storage

LDAP attributes
  1. givenName
  2. sn
  3. uid
  4. mail
  5. quota

This is the way how script will work :

#!/usr/bin/perl
# This script will export LDAP information including current quota usage.
# Install NET::LDAP, DBI, MIME::Lite for running this script.
# You have full freedom to modify this script. This will help me to improve the usage.
# Please feel free to reach me on praveen.velu84@yahoo.com

use Net::LDAP;
use DBI;
use MIME::Lite;

# Create a file for storing data
open (MYFILE, '> /tmp/quota_report.csv');
# Modify connection settings of dovecotdict database
my $dbh = DBI->connect("DBI:Pg:dbname=dovecot;host=localhost", "dovecot", "paassword", {'RaiseError' => 1});
my $sth = $dbh->prepare('SELECT * from quota where username = ?') or die "Couldn't prepare statement: " . $dbh->errstr;
$ldap = Net::LDAP->new("localhost");

# Modify your LDAP connectivity.
$ldap->bind("cn=admin,dc=example,dc=com", password=>"secret");
$mesg = $ldap->search(filter=>"(&(mail=*)(objectclass=posixAccount))", base=>"dc=example,dc=com");
print MYFILE "User_Name;First_Name;Last_Name;Email;LDAP_quota(MB);Quota_Usage(MB);No_of_messages\n";
@entries = $mesg->entries;
foreach $entry (@entries){
                $name = $entry->get_value("uid");
                $fname = $entry->get_value("givenName");
                $lname = $entry->get_value("sn");
                $mail = $entry->get_value("mail");
                $quota = $entry->get_value("quota");
$sth->execute($mail);
@data = $sth->fetchrow_array();
$usedquotamb = ($data[1]/1048576);
$quotausage = int $usedquotamb;
$messages = $data[2];
                print MYFILE $name . ";" . $fname . ";" . $lname . ";" .$mail . ";" . $quota . ";" . $quotausage . ";" . $messages ."\n"
}
close MYFILE;

# Senting mail to administrator. Replace postmaster email id with your email id
my $msg = MIME::Lite->new(
    From    => 'postmaster@example.com',
    To      => 'admin@example.com',
    Subject => 'LDAP quota report',
    Type    => 'multipart/mixed',
);
$msg->attach(
    Type     => 'TEXT',
    Data     => "Dear Sir, \n\nThis is an automated message. Please find attached User Quota Report.\n\nEmail Administrator ",
);

$msg->attach(
    Type     => 'text/csv',
    Path     => '/tmp/to_report.csv',
    Filename => 'quota_report.csv',
);

$msg->send;
$dbh->disconnect;

Wednesday, September 29, 2010

LDAP to CSV using perl



#!/usr/bin/perl
# This script will export LDAP entry in to a csv file. 
# Install Net:LDAP for running this script. 
# Feel free to contact me on praveen.velu84@yahoo.com

use Net::LDAP;

$ldap = Net::LDAP->new("localhost");
$ldap->bind("cn=admin,dc=example,dc=com", password=>"secret");

# Create a new file "ldapcsv.csv and pipe your query output to this file.  
$outputfile = "ldapcsv.csv";

# Create a new File Handler.
open (FH, ">$outputfile") or die "$!";

# Modify your search query and ldap connection details.
$mesg = $ldap->search(filter=>"(&(mail=*)(objectclass=*))", base=>"dc=example,dc=com");

@entries = $mesg->entries;
foreach $entry (@entries){
        @myuid = $entry->get( 'uid' );
        @givenName = $entry->get( 'givenName' );
        @sn = $entry->get( 'sn' );
        @userPassword = $entry->get( 'userPassword' );
        print "Exporting  @myuid[0] to csv file \n";
print FH "@myuid[0],@givenName[0],@sn[0],@userPassword[0]\n"
}

# Close the file handler.
close(FH);

Tuesday, September 7, 2010

Creating OpenVPN client certificates using perl script

How this perl scrip will work

1. Accept certificate name through stdin
2. Create vpn certificate
3. Copy certificate and client.ovpn to a temp directory
4. Modify certificate name in client.ovpn configuration
5. Create a zip file of VPN certificate file
6. Remove temp file after creating archive

How can I run this script

1. Configure your openvpn server. You can check below link for configure openvpn
2. Copy the script to a file create_vpn_cert.pl
3. Modify script based on your open vpn configuration
4. Modify the permission to execute script
# chmod +x  create_vpn_cert.pl
5. Execute the script
# perl create_vpn_cert.pl



#!/usr/bin/perl -w 
# This perl script is used to create open vpn certificates.
# Script prompt for certificate name. based on the input script will create certificates /tmp directory
 
use warnings;
use File::Copy;
use Archive::Zip;
use File::Path;
my $zip = Archive::Zip->new(); 

# OUTPUT DIRECTORY FOR ZIP FILE
$outputdir='/tmp/';

# DIRECTORY WHERE client.ovpn is available
$ovpn = '/usr/share/doc/openvpn/examples/sample-config-files/';
#DIRECTORY WHERE KEYS ARE STORED
$keys = '/etc/openvpn/easy-rsa/2.0/keys/'; 

#DIRECTORY WHERE build-key SCRIPT AVAILABLE
$scriptdir = '/etc/openvpn/easy-rsa/2.0/';

#Creating Certificate
print "Enter VPN Certificate name:";
$certname = <STDIN>;
chomp $certname;
print "Creating Certificate $certname";
system ("$scriptdir/build-key $certname");
 

# Copy certificate files to temp folder
mkdir("/tmp/$certname", 0775) || print $!;
copy("$ovpn/client.ovpn","/tmp/$certname") or die "Copy failed: $!\n";
copy("$keys/ca.crt","/tmp/$certname") or die "Copy failed ca.crt: $!\n";
copy("$keys/$certname.crt","/tmp/$certname") or die "Copy failed $certname.crt: $!\n";
copy("$keys/$certname.key","/tmp/$certname") or die "Copy failed $certname.key: $!\n"; 

# Modifying client.ovpn file
my $filein = "/tmp/$certname/client.ovpn";
my $filetemp = $filein.'_'.$$;
open (my $fh_in, "<", $filein) or die;
open (my $fh_out, ">$filetemp") or die;
while (<$fh_in>) { 
    my $x = $_;
    $x =~ s/cert client.crt/cert $certname.crt/g;
    $x =~ s/key client.key/key $certname.key/g;
    print $fh_out $x;
}
close ($filein);
close ($filetemp);
move("$filetemp","$filein");
# Creating archive
$zip->addTree( "/tmp/$certname" );
$zip->writeToFileNamed("/$outputdir/$certname.zip");
print "Created certficte for user in /$outputdir/$certname.zip \n";
 

# Removing tmp directory
rmtree("/tmp/$certname");
exit;

 

Thursday, August 12, 2010

Shell script to export roundcube address book to csv

Below shell script will convert roundcube address book to csv. I am using postgresql for roundcube database. ouput csv file can found in /tmp folder

#!/bin/bash
# Roundcube Address book must be stored in PostgreSQL database.
# This script is created by Praveen C. please feel free to contact me on praveen.velu84@yahoo.com-
# for any query. You can modify this script to suit your requirement

# Enter roundcube user_name. Script will query this user in DB and export Address book
USER="user_name"
 
# Export your roundcube database passwor. This password will be used for `psql` command
# This will help us to avoid psql prompt for password
export PGPASSWORD=password

#  Find the `user_id` of the user
echo "COPY(SELECT user_id from users where username='$USER') \
                   TO STDOUT" | psql -h localhost -U roundcube  \
                   -o /tmp/rc_query_id roundcube

# Read user_id stored in file rc_query_id file and store to variable _USER_ID
user_list=`cat /tmp/rc_query_id`
for id in $user_list; do
_USER_ID=$id
done

# Query user contacts in and sent output to csv.
echo "COPY (SELECT name,email,firstname,surname from contacts where user_id=$_USER_ID) \
                     TO STDOUT with CSV HEADER" | psql -h localhost -U roundcube \
                     -o /tmp/$USER.csv roundcube

Tuesday, August 3, 2010

PHP script to monitor your multiple ISP link connected to firewall


I have configured multiple ISP links in a linux firewall. But it is very difficult for users to understand which like is up or down. I decided to create a simple webpage which gives the status of each link in a  webpage. This will avoid the hassle of login to linux machine as a normal user and running commands with root privilage


You can use php and fping to check your ISP links status. Make sure that fping is installed in your firewall.
For a better appearance I am using images to show status of link
Image Courtesy:  Open ClipArt

Image which I used to show when a link is Active. Image name is  up.png

Image which I used to show when a link is down.  Image name is down.gif

I have copied both images in to folder img. Below is the php script. For checking the ping response I am using OpenDNS IP address as my destination server


<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Link Monitor</title></head><body>
<?
// This is the remote host used for fping. Do not change. I am using OpenDNS IP
$remote_host = "208.67.222.222";
// Add you links in to array. Chang these IP address according to IP addres configured in your firewall. You can  one or more IP address in to array
$isps = array('111.222.333.444','555.666.777.888');
$status = array();

foreach ($isps as $isp) 
{
   $fping = `/usr/sbin/fping -u -S$isp $remote_host`;
   if ($fping == "")
   {
      array_push ($status , "up.png");
   }
   else
   {
      array_push ($status , "down.gif");
   }
}
?>
</br>
<table style="text-align: left; width: 1062px; height: 75px;" border="0" cellpadding="2" cellspacing="2">

<?
$count = 1;
foreach ($isps as $isp) {?>
<tr><td  width=20%><FONT COLOR=black FACE="Geneva, Arial" SIZE=5>Link  <?echo $count?></font></td><td  width=30%><FONT COLOR=Blue FACE="Geneva, Arial" SIZE=5><?echo array_pop($isps);?></font></td><td width=30%><IMG SRC="img/<?echo array_pop($status);?>" height="60" width="61"></td></tr>
<?$count ++;}?>
</table>
</body></html>

Saturday, July 10, 2010

Create a virtual HDD using qemu-img

When you create a virtual machine using KVM and virt-manager, virtual HDD will create in /var/lib/libvirt/images/ directory. Unfortunately I dont have enough  space in my / partition.
I faced difficulty in creating virtual HDD in other partion using virt-manager. To work around this issue, I used qemu-image. Using qemu-img you can create a new vitual HDD with any size on any of your partition

 # qemu-img create -f qcow debian.qcow 5G

This will create a Virtual HDD debain.qcow with size 5GB, later then you can select this HDD for installation using virt-manager