Our highest priority is to satisfy the customer through early and continuous delivery of valuable and working software.

Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, January 3, 2012

Some quick tips

Find and replace text within all files within a directory

find PATH_TO_DIRECTORY -type f | xargs perl -pi -e 's/SEARCH_KEYWORD/REPLACE_KEYWORD/g'


Find a text in files in a directory and subdirectories

find PATH_TO_DIRECTORY -exec grep -l 'SEARCH_KEYWORD' {} \;


Find all modified file in last few days within a directory (exclude some sub-directory for e.g. CVS)
Create 2 files as below under DIRECTORY (e.g. find all modified files between 1st July 2011 and 31st July 2011)

touch -t 201107010000.01 FILE_1 /* 2011[year]07[month]01[day] */
touch -t 201107312359.59 FILE_2 /* 2011[year]07[month]31[day] */
find PATH_TO_DIRECTORY -type f -newer FILE_1 ! -newer FILE_2 -exec ls -ogh {} \; | grep -vw 'EXCLUDE_SUB_DIRECTORY' | cut -f4,5,6 -d' '


Batch resize image using ImageMagick

cd PATH_TO_IMAGE_DIRECTORY
mogrify -resize WIDTHxHEIGHT *
mogrify -resize WIDTHxHEIGHT -quality 80 * (to reduce quality)


Copy directory structure without the files

find * -type d -exec mkdir PATH_TO_DIRECTORY/\{\} \;

Wednesday, April 14, 2010

Webpage screen capturing using khtml2png

Recently, we were working on one PHP project, where we required to have "webpage screen capturing" functionality. I googled on net and found some tools... some window based, some paid... obviously I was looking for *FREE* tool :). As we are working on Lamp (Linux, Apache, MySQL & PHP), I was wondering if I get some Linux based tool.

One solution I found was, using html2ps and then ps2png/ps2jpg/ps2gif to convert it to image. Then ImageMagicK for image manipulation. Somehow I stuck with some weird memory related errors, some package conflicts, some formatting issues etc. So, after spending one day on nothing; I dropped this solution.

Then I tried khtml2png (http://khtml2png.sourceforge.net) and after some r&d, it worked for us.

Some points to remember...
- You need to have VPS/dedicated hosting to setup these tools. On shared hosting, its not possible to install due to various restrictions by hosting providers.

- This tool requires, some libraries and tools: g++, KDE 3.x, kdelibs for KDE 3.x, zlib (zlib1g-dev) and cmake

- This tool uses KDE (K Desktop Environment), that means whenever you use khtml2png tool, it will open one window for *a while* at time of capturing webpage screenshot. We can remove this by using "Xvfb". We will see how to install and configure it later.

- These links will be helpful, if you are planning to develop web application with webpage screen capturing using khtml2png
http://khtml2png.sourceforge.net/index.php?page=faq
http://www.mysql-apache-php.com/website_screenshot.htm

Here is step by step guide to install various dependencies and packages. (I installed these tools on Fedora7 & RHEL5 successfully)

I used "yum" command to install and auto-configure these tools. If "yum" is not available on your machine, get if from http://yum.baseurl.org/ and install it.

Step:1

yum install ImageMagick

yum install Xvfb

yum install gcc gcc-c++ automake autoconf nano zlib zlib-devel

yum groupinstall "X Window System" "KDE (K Desktop Environment)"

yum install kdelibs kdelibs-devel

yum install Xvfb xorg xorg-x11-font*

Step:2 Install *cmake*
Go to share directory by typing command
cd /usr/local/share/
or any preferred directory where you want to download package. (check http://www.cmake.org for latest "cmake" version)

wget http://www.cmake.org/files/v2.8/cmake-2.8.1.tar.gz

tar -xzvf cmake-2.8.1.tar.gz

cd cmake-2.8.1

./bootstrap

make

make install

Step:3 Download & Install *khtml2png* on your server as per instructions in this link.
http://khtml2png.sourceforge.net/index.php?page=download

Step:4 Check if *khtml2png* is working

/usr/local/bin/khtml2png2 'http://www.yahoo.com' yahoo.png

(this will capture yahoo homepage in yahoo.png)

Step:5 Install *khtmld* (a daemon which will be required to run khtml2png in background)
http://wiki.goatpr0n.de/projects/khtmld

I faced couple of problems while setting up *khtmld*, but it got solved by reading suggestions from above link.

I installed above all tools as *root* user.

Once you are done with above steps, lets play with *khtml2png*

How to start?
Run following command to run khtml2png without a visible X session

Xvfb :2 -screen 0 1024x768x24&
export DISPLAY=localhost:2.0
(you can put above 2 lines in rc.local so it will start automatically whenever server restarts)

Then start *khtmld* daemon as your webserver user (for me it is *apache*) so that PHP script can have permission to talk with this daemon. (run below command after login as *root* user)

khtmld -K /usr/local/bin/khtml2png2 -c /etc/khtmldrc --user apache&

"-K /usr/local/bin/khtml2png2" is path to khtml2png2 as by default "khtmld" will look for old "khtml2png" (khtml2png2 is latest version). Find khtml2png2 path using

whereis khtml2png2

"-c /etc/khtmldrc" is config file path for khtmld (you can create this config file if its not already there)
Sample content for khtmldrc

width=1024
height=768
display=:0.0

Capture image using *khtmld*

echo "http://www.yahoo.com /tmp/yahoo.png" >/tmp/khtmldspool
(for more details - http://wiki.goatpr0n.de/projects/khtmld)

We have also used ImageMagicK command "convert" (http://www.imagemagick.org/script/convert.php) to trim the image for removing whitespace.

convert /tmp/yahoo.png -fuzz 1% -trim /tmp/new.yahoo.png

Sample PHP code for capturing & displaying PNG image using "khtml2png"

<?php
ob_clean();
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-type: image/png");

$webpage_url= "http://www.yahoo.com";

$out_put_file = "/tmp/yahoo.png"; //captured screen
$new_out_put_file = "/tmp/new.yahoo.png"; //whitespace removed

$cmd = "echo '".$webpage_url." ".$out_put_file."' >/tmp/khtmldspool";
exec("$cmd");

// some delay till khtml2png capture screen
while(!file_exists($out_put_file)) { sleep(3); }

exec("convert $out_put_file -fuzz 1% -trim $new_out_put_file");

while(!file_exists($new_out_put_file)) { sleep(1); }

// display image on browser
echo file_get_contents($new_out_put_file);

unlink($out_put_file);
unlink($new_out_put_file);
exit;
?>


Hope this will be helpful.

That's all for now.

Wednesday, January 21, 2009

Add a user to a group under Linux operating system

One can use useradd or usermod commands to add a user to a group. useradd command creates a new user or update default new user information. usermod command modifies a user account i.e. it is useful to add user to existing group. There are two types of group. First is primary user group and other is secondary group. All user account related information is stored in /etc/passwd, /etc/shadow and /etc/group files to store user information.

useradd example - Add a new user to secondary group

Use useradd command to add new users to existing group (or create a new group and then add user). If group does not exist, create it.
Syntax:
useradd -G {group-name} username

Create a new user called lamptech and add it to group called developers. First login as a root user (make sure group developers exists), enter:
# grep developers /etc/group

Output:
developers:x:1124:

If you do not see any output then you need to add group developers using groupadd command:
# groupadd developers

Next, add a user called lamptech to group developers:
# useradd -G developers lamptech

Setup password for user lamptech:
# passwd lamptech

Ensure that user added properly to group developers:
# id lamptech

Output:
uid=1122(lamptech) gid=1125(lamptech) groups=1125(lamptech),1124(developers)

Please note that capital G (-G) option add user to a list of supplementary groups. Each group is separated from the next by a comma, with no intervening whitespace. For example, add user jerry to groups admins, ftp, www, and developers, enter:
# useradd -G admins,ftp,www,developers jerry

useradd example - Add a new user to primary group

To add a user tony to group developers use following command:
# useradd -g developers tony
# id tony

uid=1123(tony) gid=1124(developers) groups=1124(developers)

Please note that small -g option add user to initial login group (primary group). The group name must exist. A group number must refer to an already existing group.

usermod example - Add a existing user to existing group

Add existing user tony to ftp supplementary/secondary group with usermod command using -a option ~ i.e. add the user to the supplemental group(s). Use only with -G option :
# usermod -a -G ftp tonyChange existing user tony primary group to www:
# usermod -g www tony

Friday, April 18, 2008

How to mount external ntfs USB in RHEL?

Follow the steps to mount external ntfs USB in RHEL or in any linux system.

Get your kernel version: uname -r
2.6.9-42.ELsmp (For my RHEL)
.................
Install the package downloaded according to your kernel version
http://rpmfind.net/linux/sourceforge/l/li/linux-ntfs/
rpm -ivh kernel-module-ntfs-2.6.9-42.ELsmp-2.1.20-0.rr.10.0.i686.rpm
.................
/sbin/modprobe ntfs (as root)
.................
cat /proc/filesystems
.................
Check volume names of your USB Drive.
cat /etc/fstab
..................
Mount external ntfs USB
sudo mount /dev/sdb1 /mnt/usb/ -t ntfs -r -o umask=0222
..................
Unmount external ntfs USB
sudo umount /dev/sdb1 /mnt/usb/

You r done!