Archive for the ‘Linux’ Category

The Universally Unique Identifier can be used to identify a device independent form its mount point or device name.
This is more and more important as many devices today support hot-plugging or are external anyway.
Therefore it makes sense to access a device not by device name but by the UUID, especially in /etc/fstab.

There are a lot of way to get a device UUID, the first one is to check in your /dev/ directory, but not all Linux Distribution have it implemented (i.e. Debian),
to do so, execute the following commands :

ls -l /dev/disk/by-uuid

and you should have something similar :

lrwxrwxrwx 1 root root 10 Aug 29 04:20 015770bc-2c9b-4654-9087-af8bc9f163f3 -> ../../sdd1
lrwxrwxrwx 1 root root 10 Aug 29 04:20 1cf18ff6-2f6e-4eb3-97a5-fb6d3b4e346b -> ../../sde7
lrwxrwxrwx 1 root root 10 Aug 29 04:20 41c9b6af-86d9-4893-8502-4c0edb556986 -> ../../sde1
lrwxrwxrwx 1 root root 10 Aug 29 04:20 60a56eaa-8a3c-4d1f-8017-24af2905aebf -> ../../sde5
lrwxrwxrwx 1 root root 10 Aug 29 04:20 8ee89635-33cb-4a42-b488-26fdd2f293ad -> ../../sde2
lrwxrwxrwx 1 root root 10 Aug 29 04:20 91967f22-c1f3-436c-b769-ccd6fcc29e4e -> ../../sde8
lrwxrwxrwx 1 root root 10 Aug 29 04:20 c693372b-7147-4fb3-aaf7-edbf5f79f7f7 -> ../../sde6

Another way is to use the “blkid” command.
To do so, and to gain the UUID for your /dev/sda1, execute :

blkid /dev/sda1

and you should have something similar :

/dev/sda1: LABEL="root" UUID="41c9b6af-86d9-4893-8502-4c0edb556986" SEC_TYPE="ext2" TYPE="ext3"

Another tool (maybe a little more hidden) will be “vol_id”
On certain systems (i.e. Fedora or VMware ESX) you should find it in “/lib/udev/vol_id”, to use it execute the following :

/lib/udev/vol_id /dev/sda1

and you should have something similar :

ID_FS_USAGE=filesystem
ID_FS_TYPE=ext3
ID_FS_VERSION=1.0
ID_FS_UUID=41c9b6af-86d9-4893-8502-4c0edb556986
ID_FS_LABEL=root
ID_FS_LABEL_SAFE=root

When you got your devices UUID you should fill the /etf/fstab like the following, to avoid that other operations, for examples adding disks or volume, cause problems to your mounting process :

UUID=41c9b6af-86d9-4893-8502-4c0edb556986 /                       ext3    defaults        1 1

if you wonder how “unique” this UUID is, the following is a Wikipedia quote:

1 trillion UUIDs would have to be created every nanosecond for 10 billion years to exhaust the number of UUIDs.

Hope this help

Bye
Riccardo

Print This Post Print This Post

To reset the sys password of a database, do the following:

1) rename/delete ${ORACLE_HOME}/database/PWD{ORACLE_SID}.ora
2) enter the command: orapwd file=${ORACLE_HOME}/database/PWD{ORACLE_SID}.ora password=newpassword
3) start “sqlplus /nolog”
4) login with sys and the newpassword (SQL> connect sys/newpassword as sysdba)
5) now reset other passwords or just be done

Hope this help (especially me)
Bye
Riccardo

Print This Post Print This Post

This is a list of handy MySQL commands that I use frequently.

Below when you see # it means from the unix shell. When you see mysql> it means from a MySQL prompt after logging into MySQL.

To login (from unix shell) use -h only if needed.
# [mysql dir]/bin/mysql -h hostname -u root -p

Create a database on the sql server.
mysql> create database [databasename];

List all databases on the sql server.
mysql> show databases;

Switch to a database.
mysql> use [db name];

To see all the tables in the db.
mysql> show tables;

To see database’s field formats.
mysql> describe [table name];

To delete a db.
mysql> drop database [database name];

To delete a table.
mysql> drop table [table name];

Show all data in a table.
mysql> SELECT * FROM [table name];

Returns the columns and column information pertaining to the designated table.
mysql> show columns from [table name];

Show certain selected rows with the value “whatever”.
mysql> SELECT * FROM [table name] WHERE [field name] = "whatever";

Show all records containing the name “Bob” AND the phone number ‘3444444′.
mysql> SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

Show all records not containing the name “Bob” AND the phone number ‘3444444′ order by the phone_number field.
mysql> SELECT * FROM [table name] WHERE name != "Bob" AND phone_number = '3444444' order by phone_number;

Show all records starting with the letters ‘bob’ AND the phone number ‘3444444′.
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444';

Show all records starting with the letters ‘bob’ AND the phone number ‘3444444′ limit to records 1 through 5.
mysql> SELECT * FROM [table name] WHERE name like "Bob%" AND phone_number = '3444444' limit 1,5;

Use a regular expression to find records. Use “REGEXP BINARY” to force case-sensitivity. This finds any record beginning with a.
mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

Show unique records.
mysql> SELECT DISTINCT [column name] FROM [table name];

Show selected records sorted in an ascending (asc) or descending (desc).
mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

Return number of rows.
mysql> SELECT COUNT(*) FROM [table name];

Sum column.
mysql> SELECT SUM(*) FROM [table name];

Join tables on common columns.
mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;

Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;

Change a users password from unix shell.
# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'

Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');
mysql> flush privileges;

Recover a MySQL root password. Stop the MySQL server process.
Start again with no grant tables.
Login to MySQL as root.
Set new password.
Exit MySQL and restart MySQL server.
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables &
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newrootpassword") where User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

Set a root password if there is on root password.
# mysqladmin -u root password newpassword

Update a root password.
# mysqladmin -u root -p oldpassword newpassword

Allow the user “bob” to connect to the server from localhost using the password “passwd”. Login as root. Switch to the MySQL db. Give privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to bob@localhost identified by 'passwd';
mysql> flush privileges;

Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N');
mysql> flush privileges;

or

mysql> grant all privileges on databasename.* to username@localhost;
mysql> flush privileges;

To update info already in a table.
mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';

Delete a row(s) from a table.
mysql> DELETE from [table name] where [field name] = 'whatever';

Update database permissions/privilages.
mysql> flush privileges;

Delete a column.
mysql> alter table [table name] drop column [column name];

Add a new column to db.
mysql> alter table [table name] add column [new column name] varchar (20);

Change column name.
mysql> alter table [table name] change [old column name] [new column name] varchar (50);

Make a unique column so you get no dupes.
mysql> alter table [table name] add unique ([column name]);

Make a column bigger.
mysql> alter table [table name] modify [column name] VARCHAR(3);

Delete unique from table.
mysql> alter table [table name] drop index [colmn name];

Load a CSV file into a table.
mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);

Dump all databases for backup. Backup file is sql commands to recreate all db’s.
# [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql

Dump one database for backup.
# [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql

Dump a table from a database.
# [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

Restore database (or database table) from backup.
# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

Create Table Example 1.
mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

Create Table Example 2.
mysql> create table [table name] (personid int(50) not null auto_increment primary key,firstname varchar(35),middlename varchar(50),lastnamevarchar(50) default 'bato');

Read the rest of this entry »

The following is a simple (and very row) init script for Symantec Backup Exec Agent for Linux.

I wrote it because I really hate to execute script not registered with chkconfig on a production systems.

I’ve added the “status” function by checking if an executable called “beremote” is listening on 10000/tcp on IPv4 on the localhost.

If you are planning to run ralus on a different TCP port, please fix the script.

The following is the script :


#!/bin/sh
#
# RALUS Control Script
# chkconfig: 2345 99 01
# Description: Here is a little startup/shutdown script for RedHat/CentOS systems
#
# Author : Riccardo Riva
#
# description: Symantec Backup Exec Linux Agent Init Script
#
# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

# See how we were called.

if [ ! -d /opt/VRTSralus ]
then
 echo "Symantec Backup Exec Remote Agent missing /opt/VRTSralus             [FAILED]"
 exit 1
fi

if [ ! -d /etc/VRTSralus ]
then
 echo "Symantec Backup Exec Remote Agent missing /etc/VRTSralus             [FAILED]"
 exit 1
fi

if [ ! -d /var/VRTSralus ]
then
 echo "Symantec Backup Exec Remote Agent missing /var/VRTSralus             [FAILED]"
 exit 1
fi

CMD="$1"

case "$CMD" in
'start')
 if [ -x /opt/VRTSralus/bin/beremote ]
 then
 echo -n "Starting Symantec Backup Exec Remote Agent "
 rm -f /var/VRTSralus/ralus.pid
 rm -f /var/VRTSralus/ralus.errpid
 /opt/VRTSralus/bin/beremote >/var/VRTSralus/beremote.service.log 2>/var/VRTSralus/beremote.service.log &
 PIDWAIT=30
 while [ "$PIDWAIT" != "0" ]
 do
 if [ -f /var/VRTSralus/ralus.pid ]
 then
 PIDWAIT=0
 else
 PIDWAIT=$(($PIDWAIT-1))
 echo -n "."
 sleep 1;
 fi
 if [ -f /var/VRTSralus/ralus.errpid ]
 then
 PIDWAIT=0
 fi
 done
 if [ -f /var/VRTSralus/ralus.pid ]
 then
 RETVAL=0
 else
 RETVAL=1
 fi
 echo
 else
 RETVAL=1
 fi
 if [ "$RETVAL" = "0" ]
 then
 echo "Starting Symantec Backup Exec Remote Agent:                              [  OK  ]"
 else
 echo "Starting Symantec Backup Exec Remote Agent:                              [FAILED]"
 fi
 ;;
'stop')
 if [ -f /bin/grep ]
 then
 PID=`/bin/ps -e | /bin/grep beremote | /bin/sed -e 's/^  *//' -e 's/ .*//'`
 else
 PID=`/usr/bin/ps -e | /usr/bin/grep beremote | /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
 fi

 if [ "${PID}" != "" ]
 then
 echo -n "Stopping Symantec Backup Exec Remote Agent "
 if [ -f /bin/pkill ]
 then
 /bin/pkill -15 beremote
 else
 /usr/bin/pkill -15 beremote
 fi
 RETVAL=$?

 PIDWAIT=15
 while [ "$PIDWAIT" != "0" ]
 do
 if [ -f /var/VRTSralus/ralus.pid ]
 then
 PIDWAIT=0
 RETVAL=0
 else
 PIDWAIT=$(($PIDWAIT-1))
 echo -n "."
 sleep 1;
 fi
 done
 echo
 rm -f /var/VRTSralus/ralus.pid
 rm -f /var/VRTSralus/ralus.errpid
 else
 RETVAL=1
 fi

 if [ "$RETVAL" = "0" ]
 then
 echo "Stopping Symantec Backup Exec Remote Agent:                              [  OK  ]"
 else
 echo "Stopping Symantec Backup Exec Remote Agent:                              [FAILED]"
 fi
 ;;
'restart')
 $0 stop
 $0 start
 RETVAL=1
 ;;

'status')
 if [ $(netstat -tulpan | grep beremote |grep 0.0.0.0:10000 | wc -l) -eq 1 ];
 then
 echo "Symantec Backup Exec Remote Agent running and listening on tcp port 10000"
 else
 echo "Symantec Backup Exec Remote Agent not running"
 fi
 ;;

*)
 echo "Symantec Backup Exec Remote Agent for Linux/Unix Servers"
 echo "Usage: VRTSralus.init { start | stop | restart | status}"
 RETVAL=1
 ;;
esac

exit $RETVAL

#
# -- End of file

You should download the file here : http://www.riccardoriva.com/shared-files/ralus_init_script.sh

Hope this help

Bye
Riccardo

Print This Post Print This Post

If you use Postgrey (and you should do it) on your mail servers and you want to have some statistics on the amount of greylisted messages and other information, you should find useful the following script.

The scripts assume that you have installed Postgrey (http://postgrey.schweikert.ch/) on RHEL (http://www.redhat.com) or CentOS (http://www.centos.org).
If you are using other distros you may fix some path problems.

Create a file called “greylisting_statistics.sh” in your “/usr/local/bin” folder with the following content :

#!/bin/sh

LOGFILE=/tmp/greylist-statistics
YOURMAIL=you@yourdomain.com

echo -n "Total amount of GreyListed messages" > $LOGFILE
cat /var/log/maillog | /usr/sbin/postgreyreport --delay=300 >> $LOGFILE
echo -ne  "\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -n "Get only the top 20 sources getting greylisted out" >> $LOGFILE
cat /var/log/maillog | postgreyreport | awk '{print $1}' | sort | uniq -c | sort -nr | head -n20 >> $LOGFILE
echo -ne  "\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -n "Get a list of the top 20 email address that the greylisted sources are sending email to"  >> $LOGFILE
cat /var/log/maillog | postgreyreport | awk '{print $4}'  | sort  | uniq -c | sort -nr | head -n20
echo -ne  "\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE
echo -ne  "-------------------------------------\n" >> $LOGFILE

cat $LOGFILE | mail -s "Greylisting Statistics of `hostname` for `date +%Y-%m-%d`" $YOURMAIL
#--- End of file ---

Assign to it the correct ownership and permission by running :

chmod 700 /usr/local/bin/greylisting_statistics.sh
chown root:root /usr/local/bin/greylisting_statistics.sh

Schedule the execution using “crontab -e” and adding the following line to have a daily based statistics :

59 23 * * * /usr/local/bin/greylisting_statistics.sh

Hope this help

Bye
Riccardo

Print This Post Print This Post

This simple How-To will explain a raw and dirt method to daemonize to http manager for ARECA Raid Controller.

If you have this controller on your system you should find in the bundled CD-ROM the following folder :

root
PACKAGES -> Linux -> CLI -> version -> i386 -> cli32

PACKAGES -> Linux -> CLI -> version -> x86-64 -> cli32

PACKAGES -> Linux -> HTTP -> version.x.y.x -> i386 -> archttp32

PACKAGES -> Linux -> HTTP -> version.x.y.x -> x86-64 -> archttp32

Create a folder in your /usr/local folder called “areca” e and create into it two folders called “http” and “cli”
Copy the “cli32″ and “archttp32″ executable file that match your architecture (i386 or x86-64) in the specified folder, so you should have something similar :

[root@relay ~]# ls -lahR /usr/local/areca/
/usr/local/areca/:
total 20K
drwxr-xr-x  4 root root 4.0K Feb 12 10:28 .
drwxr-xr-x 13 root root 4.0K Feb 12 10:26 ..
dr-xr-xr-x  2 root root 4.0K Feb 12 10:27 cli
dr-xr-xr-x  2 root root 4.0K Feb 12 10:42 http

/usr/local/areca/cli:
total 1.6M
dr-xr-xr-x 2 root root 4.0K Feb 12 10:27 .
drwxr-xr-x 4 root root 4.0K Feb 12 10:28 ..
-r-xr-xr-x 1 root root 1.6M Dec 26  2008 cli32

/usr/local/areca/http:
total 1.6M
dr-xr-xr-x 2 root root 4.0K Feb 12 10:42 .
drwxr-xr-x 4 root root 4.0K Feb 12 10:28 ..
-r-xr-xr-x 1 root root 1.6M Dec 26  2008 archttp32
-rw-r--r-- 1 root root   91 Feb 12 10:42 archttpsrv.conf

Create a new file called “areca” in your /etc/init.d folder with the following content :

#!/bin/sh
#
# Startup script for the ARECA RAID CONTROLLER HTTP Monitor
#
# chkconfig: 2345 62 38
# description: HTTP Tools to monitor and manage ARECA RAID Controller
#
# processname: archttp32
# config: none
# lockfile: /var/lock/subsys/archttp32
#
# Author: Riccardo Riva
# WebSite: http://www.riccardoriva.com
#
# This script is realeased under the terms of the GPL.
#====================================================================

# Source function library
. /etc/init.d/functions

RETVAL=0

start() {
echo -n $"Starting ARECA HTTP Manager: "
daemon /usr/local/areca/http/archttp32 2>&1>/dev/null &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/archttp32
return $RETVAL
}

stop() {
echo -n $"Stopping ARECA HTTP Manager: "
killproc archttp32
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/archttp32
return $RETVAL
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading ARECA HTTP Manager: "
killproc archttp32 -ALRM
RETVAL=$?
echo
return $RETVAL
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status archttp32
;;
restart)
restart
;;
condrestart)
[ -f /var/lock/subsys/archttp32 ] && restart || :
;;
reload)
reload
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
exit 1
esac

exit $?
#---End of file---

Assing to it the right permission and ownership

chmod 755 /etc/init.d/areca
chown root:root /etc/init.d/areca

You should also place in the executable file folder the configuration file, called “archttp32srv.conf” with the following content :

[GENERAL]
BindingIp=0.0.0.0
HTTPPort=81
SMTPPort=25
ScanPci=YES
ScanRs232=NO
ScanInband=NO
ConnInfo=NO

[MAIL]
Server=MAIL.SERVER.IP.ADDRESS # Please Change ME
Sender=SENDERNAME # Please Change ME
SenderMail=SENDERMAIL # Please Change ME
Account=
Password=
MailToName1=RECEIVERNAME # Please Change ME
MailToName2=
MailToName3=
MailToName4=
MailAddr1=RECEIVERMAIL # Please Change ME
MailAddr2=
MailAddr3=
MailAddr4=
EvtLevel=3
NotifyForNoEvent=NO

[SNMP]
TrapIp1=SNMP.SERVER.IP.ADDRESS # Please Change ME
TrapIp2=0.0.0.0
TrapIp3=0.0.0.0
TrapPort1=162
TrapPort2=162
TrapPort3=162
Community=public
EvtLevel=3

And configure it for startup at boot time.

chkconfig areca on

I know it very dirt, but I had no time left to search a better way, maybe in the future.

Hope this help

Bye
Riccardo

Print This Post Print This Post

In this tutorial I will describe how setup a complete mail server for relaying mail to another server, for example an IMAP server or an Exchange, tipically this machine will be placed in a DMZ network and will be reachable from internet.
I will explain how to install and configure :

- Postgrey : to do GreyListing on incoming mails and avoid SPAM.
- Postfix : to receive mails for local or remote mailboxes and do some checks for trash mail using RBL and internal options.
- Amavisd-New : to scan incoming mails form Viruses and SPAM.
- Clamd : to avoid Amavisd-new virus scan.
- Spamassassin : to avoid amavisd-new SPAM scan with Razor, Pyzor and DCC.
- Altermime : to add a disclaimer (both text and html) for the outgoing mail.
- Cyrus-Sasl : to enable sasl authentication for road warriors.
- Fetchmail : to eventually fetch the mail from another mail server.
- Apache : to create a reverse proxy to reach a webmail service on the internal web server.

I’ll describe all steps needed to install it on RHEL or CentOS because the steps are identical for both distributions and because are my favourites ones.
I’m assuming you’re going to install a 64bit operating system, but if you choose to install the 32bit version pay only attention for some packages to fit your architecture.

First of all you have to install you operating systems. I’ll not explain this process because it’s very simple, but I’ll suggest you to NOT customize the system during the installation procedure, it will be done later.

At the first reboot, disable both Selinux and Firewall, then reboot again and login to your system as user root.

Read the rest of this entry »

Virtualization Technology (VT) is a set of enhancements to CPU that improve performance for running a virtual machine by offloading some of the work to the new cpu extensions. Both AMD and Intel have CPU that support this technology.

In some cases it will be very usefule to know if your system can handle it.

It will be very simple from a Linux system because you should only take a look to /proc/cpuino looking for one of the following value :

  • vmx – (intel)
  • svm – (amd)

You can use grep to quickly find if either value exists in the file by running the following command:

egrep ‘(vmx|svm)’ /proc/cpuinfo

If your system supports VT, then you’ll see vmx or svm in the list of flags.

My system has four quad core AMD processors, so I’ll find the following repeated from 16 times :

My system has two processors, so there are two separate sections:

flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt

VT technology can still be disabled in your computer’s BIOS, however, so you’ll want to check there to make sure that it hasn’t been disabled. The flags in cpuinfo simply mean that your processor supports it.

VT is required to run 64-bit guests using Hypervisor like VMware Server or others similar products.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This post will explain how to create users and mailboxes for Zarafa.

I’ve tested it with CentOS 5.4 X86_64 and Zarafa 6.30.9 but it will fit with nearly all installation.

First of all you have to create a test file with all the users you want to create, providing the following information :

  • username
  • password
  • email address
  • name
  • surname

For example :

johndoe   password    johndoe@example.com    John    Doe
janedoe    password   janedoe@example.com     Jane     Doe

Save the text file whatever you want.

The following is the shell script that read your text file and create all Zarafa users.

If you use the script, all the users will not be administrator, so you have to create manually and administrator.


#!/bin/bash
# Written by Matteo Predieri - m.predieri_AT_damsistemi_DOT_it
# Written by Riccardo Riva - r.riva_AT_damsistemi_DOT_it
#
# Simple and raw script to create zarafa users and mailboxes from a test file
#
# The full fill-in of the text file is mandatory
# So fill in both "Name" and "Surname" with the following syntax
#
# username    password    email    name    surname
#

USERS_LIST=/tmp/userlists.txt
ZARAFA_CMD=/usr/bin/zarafa-admin
LOGFILE=/tmp/zara_user_creation.log

echo "Zarafa User Creation Log" > $LOGFILE
what="user";

for item in $( cat $USERS_LIST ); do

 if [ $what = "user" ]; then
 user=$item;
 what="passwd";
 else
 if [ $what = "passwd" ]; then
 passwd=$item;
 what="email";
 else
 if [ $what = "email" ]; then
 email=$item;
 what="name";
 else
 if [ $what = "name" ]; then
 name=$item;
 what="surname";
 else
 surname=$item;

 echo "Result in creating User: $user, with password: $passwd, with email address: $email with Full Name: $name $surname :" >> $LOGFILE
 FULLNAME="'$name $surname'"
 $ZARAFA_CMD -c $user -p $passwd -e $email -f "$FULLNAME" -a0 >> $LOGFILE
 echo -ne  "-------------------------------\n" >> $LOGFILE
 what="user";
 fi

 fi
 fi

 fi
done

# --- End of file ---

You’ve done.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This is a simple shell script that count all files in a local directory and all subdir. It doesn’t care what extention the file has, it will just recursively go through each directory and count any files within it, plus recurse into any more directories it finds. Give it as a parameter the directory you want to count.


#!/bin/sh
NUMBER=0
count ()
{
for temporary in $1/* ; do
if [ -d "$temporary" ] ; then
count "$temporary"
elif [ -f "$temporary" ] ; then
NUMBER=$(($NUMBER+1))
fi
done
}
count $1
echo "I found $NUMBER files, in the specified folder and all subfolders"

I know you should also did it with

ls -R | wc -l

but it was a shell exercise.

Hope this help

Bye
Riccardo

Print This Post Print This Post

This wuold be a simple reminder for the most common tips for VI or VIM.
It would not be a deep dive on it, simply a memo for the most common feature used by me.

Search and replace a string in a document :
:.,$s/search_string/replacement_string/g

Disabling highlighted patterns :
Type “:nohl” to disable highlighting

Comment out several lines at once (I like that one) :
1. Ctrl + V (visual block)
2. Select the lines you want to be commented out
3. Shift + I (insert mode)
4. Add the “#” character on the first line
5. Press escape

Copy and paste several lines :
1. Shift + V (visual line)
2. Select the lines
3. Press “yyN” (N lines yanked), for example to copy 3 lines use “yy3″
4. Go where you want the block to be pasted and press “p”

Cut and paste several lines :
1. Let’s say you want to cut 3 lines, put your cursor on the first of the 3 lines
2. Type : 3dd
3. Locate your cursor where you want the block to be pasted
4. Type “p”

Some key combinations :
dd : Delete current line
D : Delete from cursor to end of line
d$ : Delete from cursor to end of line
d0 : Delete from cursor to beginning of line
dw : Delete from cursor to end of current word
db : Delete from cursor to beginning of current word
yy : Copy current line (use “p” to paste)

Here’s a great cheatsheet from http://www.worldtimzone.com/res/vi.html

Cursor movement

* h – move left
* j – move down
* k – move up
* l – move right
* w – jump by start of words (punctuation considered words)
* W – jump by words (spaces separate words)
* e – jump to end of words (punctuation considered words)
* E – jump to end of words (no punctuation)
* b – jump backward by words (punctuation considered words)
* B – jump backward by words (no punctuation)
* 0 – (zero) start of line
* ^ – first non-blank character of line
* $ – end of line
* G – Go To command (prefix with number – 5G goes to line 5)

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
Insert Mode – Inserting/Appending text

* i – start insert mode at cursor
* I – insert at the beginning of the line
* a – append after the cursor
* A – append at the end of the line
* o – open (append) blank line below current line (no need to press return)
* O – open blank line above current line
* ea – append at end of word
* Esc – exit insert mode

Editing

* r – replace a single character (does not use insert mode)
* J – join line below to the current one
* cc – change (replace) an entire line
* cw – change (replace) to the end of word
* c$ – change (replace) to the end of line
* s – delete character at cursor and subsitute text
* S – delete line at cursor and substitute text (same as cc)
* xp – transpose two letters (delete and paste, technically)
* u – undo
* . – repeat last command

Marking text (visual mode)

* v – start visual mode, mark lines, then do command (such as y-yank)
* V – start Linewise visual mode
* o – move to other end of marked area
* Ctrl+v – start visual block mode
* O – move to Other corner of block
* aw – mark a word
* ab – a () block (with braces)
* aB – a {} block (with brackets)
* ib – inner () block
* iB – inner {} block
* Esc – exit visual mode

Visual commands

* > – shift right
* < – shift left
* y – yank (copy) marked text
* d – delete marked text
* ~ – switch case

Cut and Paste

* yy – yank (copy) a line
* 2yy – yank 2 lines
* yw – yank word
* y$ – yank to end of line
* p – put (paste) the clipboard after cursor
* P – put (paste) before cursor
* dd – delete (cut) a line
* dw – delete (cut) the current word
* x – delete (cut) current character

Exiting

* :w – write (save) the file, but don’t exit
* :wq – write (save) and quit
* :q – quit (fails if anything has changed)
* :q! – quit and throw away changes

Search/Replace

* /pattern – search for pattern
* ?pattern – search backward for pattern
* n – repeat search in same direction
* N – repeat search in opposite direction
* :%s/old/new/g – replace all old with new throughout file
* :%s/old/new/gc – replace all old with new throughout file with confirmations

Working with multiple files

* :e filename – Edit a file in a new buffer
* :bnext (or :bn) – go to next buffer
* :bprev (of :bp) – go to previous buffer
* :bd – delete a buffer (close a file)
* :sp filename – Open a file in a new buffer and split window
* ctrl+ws – Split windows
* ctrl+ww – switch between windows
* ctrl+wq – Quit a window
* ctrl+wv – Split windows vertically

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following is a great memo for a lot of bash shortcuts.

Original link : HowToGeek

CTRL Key Bound

Ctrl + a – Jump to the start of the line
Ctrl + b – Move back a char
Ctrl + c – Terminate the command
Ctrl + d – Exit the current shell
Ctrl + e – Jump to the end of the line
Ctrl + f – Move forward a char
Ctrl + h – Same as backspace
Ctrl + k – Delete to EOL
Ctrl + l – Clear the screen
Ctrl + r – Search the history backwards
Ctrl + R – Search the history backwards with multi occurrence
Ctrl + t : Swap the last two characters before the cursor
Ctrl + u – Delete backward from cursor
Ctrl + w : Delete the word before the cursor
Ctrl + xx – Move between EOL and current cursor position
Ctrl + x @ – Show possible hostname completions
Ctrl + z – Suspend/ Stop the command. “fg” restores the suspended command.

ALT Key Bound

Alt + < – Move to the first line in the history
Alt + > – Move to the last line in the history
Alt + ? – Show current completion list
Alt + * – Insert all possible completions
Alt + / – Attempt to complete filename
Alt + . – Yank last argument to previous command
Alt + b – Move backward
Alt + c – Capitalize the word
Alt + d – Delete word
Alt + f – Move forward
Alt + l – Make word lowercase
Alt + n – Search the history forwards non-incremental
Alt + p – Search the history backwards non-incremental
Alt + r – Recall command
Alt + t – Move words around
Alt + u – Make word uppercase
Alt + back-space – Delete backward from cursor

More Special Keybindings

$ 2T – All available commands(common) (2T means press the TAB twice)
$ (string)2T – All available commands starting with (string)
$ /2T – Entire directory structure including Hidden one
$ 2T – Only Sub Dirs inside including Hidden one
$ *2T – Only Sub Dirs inside without Hidden one
$ ~2T – All Present Users on system from “/etc/passwd”
$ $2T – All Sys variables
$ @2T – Entries from “/etc/hosts”
$ =2T – Output like ls or dir

Hope this help

Bye
Riccardo

Print This Post Print This Post

The following is a simple memo on how to use the three powerful tools above.
It will not be a complete guide, and you should find only a small part of the possible use, but this would be a review only for the most common use (for me) and would not go a deep dive.

Grep

grep : print lines matching a pattern (equals “grep -G” which is the default)
egrep : equals grep -E (interpret extended regexp)

grep -n : line numbered
grep -i : ignore case
grep -c : count matches
grep -v : print non-matching lines
grep -r : recursivity, read all files under each directory

grep pattern file.txt

Will display the lines containing the pattern

grep -c pattern file.txt

Will display how many lines contain the pattern

grep -i pattern file.txt

Will display the lines containing the pattern regardless of the case

grep -A 1 -B 1 pattern output.txt

Will print the one line before (-B) and one line after (-A) the matching pattern

Awk

awk '{ print $0 }' file

Output the content of the file

awk '{ print $2 }' file

Output the second field of data of the file, space is the default separator

awk -F ':' '{ print $2 }' file

Same but separator is “:”

Sed

cat file | sed -e 's/old_pattern/new_pattern/g'

sed would replace old_pattern by new_pattern in the output

cat file | sed -e '4,10s/old_pattern/new_pattern/g'

sed would replace old_pattern by new_pattern in the output between line 4 and 10

cat file | sed '/pattern/d'

Delete a pattern

cat file | sed '/pattern/!d'

Delete everything but the pattern (this equals grep “string_to_remove”)

Hope this help

Bye
Riccardo

Print This Post Print This Post

This would be another little memo of all the most used (by me) Input/output redirections and pipes.
As other times this would not be a complete guide, and would not go into the deep.

The base :

Standard input (stdin) : 0
Standard output (stdout) : 1
Error output (stderr) : 2

> : output redirection
< : input redirection
>> : append output
| : pass the output to the next utility
|| : execute the next command if the previous failed
&& : execute the next command only if the previous has succeded

Examples :

cat file.txt > /tmp/file.txt

If file.txt exists, the output will be written in /tmp/file.txt
If file.txt doesn’t exist, the error message will be output to the screen while nothing will be written to /tmp/file.txt (but the file will be created if not existing)

cat file.txt 2> /tmp/error_file.txt > /tmp/file.txt

In this case the error message would be written to /tmp/error_file.txt if the file doesn’t exist

cat file.txt 2> /tmp/error_file.txt >> /tmp/file.txt

The error output would still go in error_file.txt while the standard output would be APPENDED to file.txt

cat file1 > file2

Will copy the content of file1 to file2

 cat &gt; file3

Hello world

Will write “Hello world” to file3

[/bash]wc < file.txt > count_file.txt[/bash]

This would count the number of lines, words and bytes in file.txt (”wc < file.txt”) and output the result to count_file.txt

Several ways of counting lines etc. :
$ wc file.txt
7 2 17 file.txt
$ wc < file.txt
7 2 17
$ cat file.txt | wc
7 2 17

ls -l test10 > file.txt 2>&1

This would output anything under file.txt

<code>ls -l test10 2>&1 | mail -s “output in a mail” address@domain.com
Any output in a mail

Hope this help

Bye
Riccardo

Print This Post Print This Post

Which Linux distribution is more suitable for you regarding your experience and preferences ?

find it at :

http://www.zegeniestudios.net/ldc/index.php?lang=en

Mine was 100% Debian / 100% Ubuntu

No surprises on that !

Bye
Riccardo

This post will explain how to disable IPv6 in a system running Debian GNU/Linux or Ubuntu Linux.

You should want to disable IPv6 for compatibility reason or if you not plan to use it for speed up your system and/or to avoid loading of unuseful modules on system start up.

For disable the protocol you have to edit

/etc/modprobe.d/aliases

file and change two lines as follow :

#alias net-pf-10 ipv6
net-pf-10 off

You should also tell to your kernel to not load IPv6 module by blacklisting it at the boot, for doing so edit

/etc/modprobe.d/blacklist 

file adding the following line :

blacklist ipv6

Reboot your system and check with the following command if the module ipv6 is not present :

lsmod |grep ipv6

You’ve done

Hope this help

Bye
Riccardo

Print This Post Print This Post

This pot will show some useful example on how to use the “grep” command to find an occurency in a Linux system.
Grep searches the input file (or files) for lines containing a match to a given pattern. Whe an occurency math it copies to standard input the line with the occurrency or you should rediret the output whatever you want.

You should simpy invoke grep with the following :

grep 'STRING' filename

The above is a very simple use f grep, infact it check only in a single file searching all the occurency for ‘STRING’.
You should also use it to find ‘STRING1 STRING2′ in all files in your current location, running ::

grep 'STRING1 STRING2' *

or if you want to make the same research on a given path (i.e. all files in /etc) you should use :

grep 'STRING1 STRING2' /etc/*

Notice the use of single quotes; This are not essential but in this example it was required since the name contains a space. Double quotes could also have been used in this example.

In case of too much occurency you should redirect the output o a file to make more comfortable the research, for example using :

grep 'STRING 1 STRING2" /etc/* > /tmp/grepresults.txt

The following is a simple list for Grep Regular Expression

grep can search for complicated pattern to find what you need using some special characters used to create a regular expression:

`.’ The period `.’ matches any single character.

`?’ The preceding item is optional and will be matched at most once.

`*’ The preceding item will be matched zero or more times.

`+’ The preceding item will be matched one or more times.

for example, a regular expression search would be :

grep "\<[A-Za-z].*" file

The search above will search for any word which begins with a letter upper or lower case.

For more details check :

man grep

Hope this help

Bye
Riccardo

Print This Post Print This Post

Very often it happens that I must give support to a colleague or a customer on a Linux machine.

It’s very difficult to spell all bash command I will use to check which could be the problem, especially by phone.

When I discover “screen” it was a revelation.

With screen http://www.gnu.org/software/screen/screen.html you should share a linux session with other people.

You should use screen by simply ask to the user you want to assist to type on the console the following command :

screen

So if you can connect to the machine (even with ssh) you should run the following command :

screen -x

to share the same bash session.

Hope this help

Bye

Riccardo

Print This Post Print This Post

The following is a simple script to monitor a Software Raid configuration on a Linux System.
It execute a /proc/mdstat check to search a ‘blocks_’ occurency which indicates problems on the Raid system and in case of match it notify the system administrator with a mail message.

It should be very useful for non-presidiated system, even if I always prefer Hardware Raid.


#!/bin/bash
#
# Script created by Riccardo Riva
# http://www.riccardoriva.com
#
# It check a Software Raid subsystem and notify by mail any occurency problem

# Define variable
LOG_FILE=/tmp/raid-check.log
SYSTEM=`uname --nodename`
MAILTO='systemadmin@mail.exp'

# Checking /proc/mdstat
cat /proc/mdstat | grep 'blocks.*_' > $LOG_FILE

# Define function in case of problems detected
if [ $? -eq 0 ]
then
echo "The $SYSTEM system has RAID failures on it." >> $LOG_FILE
echo "Below is the output from /proc/mdstat" >> $LOG_FILE
echo "===========================================" >> $LOG_FILE
cat /proc/mdstat >> $LOG_FILE
echo "===========================================" >> $LOG_FILE
cat $LOG_FILE | mail -s 'URGENT: RAID disk failure detected' $MAILTO
fi

# Deleting log file
rm -f >> $LOG_FILE

# Exit
exit 0

Save the file above as /usr/local/bin/raidcheck.sh  and assign to it correct permission and ownership with the folowing :

chmod 700 /usr/local/bin/raidcheck.sh
chown root:nobody /usr/local/bin/raidcheck.sh

Change the email address variable with a real email address who will receive the notification.

You should now schedule the script execution at every time interval you want (i.e. twice a day, or hourly if you are paranoic).

Remember that is better to be paranoic that have a degraded system with unrecoverable data.

Hope this help

Bye
Riccardo

Print This Post Print This Post

Hi all, the following will be a quick and dirt list of commands for use “dd” in a linux system for various task.

First of all you should create an Hard Disk Image, for example if you want to create an image of your /dev/sda device, you should have another disk (with a writable partition) (i.e. /dev/sdb) with a directory (i.e. /home mounted on it) and simply type :

dd if=/dev/sda of=/home/sda.bin

Or even a partition backup using the same disk device for source and destination like the following (if for example you have /home mounted on /dev/sda2) :

dd if=/dev/sda1 of=/home/sda1.bin

You should even create a compress image of the same disk above, using GZIP, simply type the following :

dd if=/dev/sda1 | gzip > /home/sda1.bin.gz

One of the most useful use of dd (it’s saved my life a lot of time) will be :

dd if=/dev/sda of=sda.boot.mbr bs=512 count=1

With the above command, you have backuped up your MBR (Master Boot Record) of your /dev/sda device and in case of disaster you should restore it, for example booting with a live CD using the following :

dd if=sda.boot.mbr of=/dev/sda bs=512 count=1

You should also create a manual RAID1 between two disk by executing the followinf script with a cron job :


#!/bin/sh
#

LOG="/var/log/mirror.log"
ADMIN="yourmail@yourprovider.ext"
ERROR=0

echo `/bin/date` >$LOG 2>&1
/bin/dd if=/dev/sda of=/dev/sdb bs=1M >>$LOG 2>&1
ERROR=$?
echo `/bin/date` >>$LOG 2>&1

if [ $ERROR -ne 0 ]; then
cat $LOG | /bin/mail -s "Report mirror `uname -n`" $ADMIN
fi

The above sript will copy the entire /dev/sda to /dev/sdb logging the process and send an email to your email address at the end for debug and monitoring purpose.
If the first hard disk (/dev/sda) fail, you should phisicaly umount it, and subsitute it with /dev/sdb then reboot the system.

Hope this help

Bye
Riccardo

Print This Post Print This Post
Contacts
Look at me at Linkedin Follow me on Twitter
My Flickr Albums My Facebook profile My YouTube Videos
SkypeMe My Linux Counter GMail me
Search
Google Search
Categories
Tag Cloud 3D
FeedBurner RSS

Visitors
Locations of visitors to this page
VMware related Blogs
The following are Blog sites with feeds I personally follow. When I'll have some spare time I will complete all Feed and Twitter links.