Showing posts with label how-to. Show all posts
Showing posts with label how-to. Show all posts

Screenshot Grabbing



Using imagemagick

imagemagick is a collection of tools and libraries for image manipulation. Most distribustions come with imagemagick .
To take a snapshot using Imagemagic:
import -window root screenshot.png
import manual page

Using xv

xv is another image manipulation program that can deal with almost all file formats. xv is not included in distributions installations, but packages are available for most. Example usage:
xv -grabdelay 2 myimage.jpg
xv manual page
using gimp

To grab an image of the screen or a program in Gimp:
File -> Acquire -> Screen shot
using Framebuffer Console

Use fbgrab to grab a screenshot in a framebuffer console.
fbgrab filename.png
using kde

KDE comes with a handy program called ksnapshot that lets you with ease grab a screenshot of your desktop or a single window.
scrot
scrot is a small (66 kB) screen shot grabber based on imlib2. It has lots of options for autogenerating filenames, and can do fun stuff like taking screenshots of multiple displays and glueing them together. thanks to miztic for mentioning it

using xwd

xwd (manual page) is a part of XFree, so chances are high you already have it installed. It can dump screenshots to .xwd files. This is not a common format. After taking a screenshot using
xwd -root -out test.xwd
the only programs I found capable of opening it were Gimp and xwud (manual page). thanks to lude for mentioning it

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Encrypt Filesystems



If you are using modules you need to load these first:
modprobe loop;modprobe cryptoloop;modprobe cipher-aes
Basically you:
losetup -e aes -k 256 /dev/loop0 /dev/partition
You will be asked for a password. Then
mount /dev/loop0 /path/to/mount


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Batch resize images using the command line in Linux



If you have a ton of images that need resizing, you can do it all with the imagemagick package:

cd /home/user/images
mkdir resized_images
cp /home/user/images/* /home/user/images/resized_images

Now that you have a copy of the files in resized_images, time to resize them all:

mogrify -resize 800 *.jpg

This will resize them all to a width of 800px while keeping the aspect ratio. If you want a fixed image size, you can specify it like this:

mogrify -resize 800×600! *.jpg



Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

How to Find duplicate files



Let’s say you have a folder with 5000 MP3 files you want to check for duplicates. Or a directory containing thousands of EPUB files, all with different names but you have a hunch some of them might be duplicates. You can cd your way in the console up to that particular folder and then do a

find -not -empty -type f -printf “%s\n” | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

This will output a list of files that are duplicates, according tot their HASH signature.
Another way is to install fdupes and do a

fdupes -r ./folder > duplicates_list.txt

The -r is for recursivity. Check the duplicates_list.txt afterwards in a text editor for a list of duplicate files.

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

block consecutive IP address using scripts



How to block long list of consecutive IP address?
How to call linux route command inside a script?
How to block consecutive IP address using bash script or perl script?
How to block local IP address permanently?

The are times that a server does not need to listen and process any TCP/UDP request for a long list of consecutive local IP addresses.

This blog entry provides a starting point of creating server scripts to block a long list of consecutive IP address from the server for permanent blocking.

To start, launch your fave editor and create a IPblock.sh bash script like with contents similar to the next few lines. This blog entry assumes that you have bash shell and perl currently installed from the machine.
From below example, we are permanently blocking IP address from
192.168.0.10 to 192.168.0.254.
Here's a simple sample script that does the job.
#!/bin/bash
echo Blocking started ...

for ((i=10;i<=254;i=i+1)); do /sbin/route add -host 192.168.0.$i reject done echo Done
This can also be accomplished using perl script which does the same function. Create a separate IPblock.pl perl script like so.
#!/usr/bin/perl -w

my $i;
for ($i=10; $i<=254; $i++ ) { system ("/sbin/route del -host 192.168.0.$i reject"); } }
Make sure these scripts are root executable like so
# chmod 700 IPblock.sh
# chmod 700 IPblock.pl
Now, to execute individually
# ./IPblock.sh
# ./IPblock.pl
Additionally, the above scripts can be scheduled for regular execution if you need them so by using crontab utility.




Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

du - the disk usage linux command



du stands for disk usage. This simple linux command provides a summary of harddisk or storage space disk usage. It has many parameter arguments that can provide results in many screen format. du command can also summarize files and directories in a recursive manner.

Here are several usage of to use the du (disk usage) command.
# cd /home/vertito
To list the files and directories from there
# ls -la
-rw-r--r-- 1 root root 29 2007-08-11 11:57 file.txt
drwxr-xr-x 2 root root 4096 2007-08-11 11:57 folder1
Show summary in bytes
# du -b
4096 ./folder1
8221 .
# du -a
4 ./file.txt
4 ./folder1
12 .
Now, let us get a more human readable results
# du -ah
4.0K ./file.txt
4.0K ./folder1
12K .
The above shows that my file.txt has about 4K of filesize rounded to nearest power of 1024K including . an ..

Now, let us it in bytes
# du -ab
29 ./file.txt
4096 ./folder1
8221 .
The above is the same results you get from issuing ls -la command. 8221 is . and ..

Now let us do it once again in human readable form
# du -abh
29 ./file.txt
4.0K ./folder1
8.1K .
You can also exclude file glob pattern or shell expression for files like so
# du -abh --exclude='file.txt'
# du -abh --exclude='*.txt'
4.0K ./folder1
8.0K .
Recursive directory disk usage summary can also be achieved by doing the default usage without any parameters
# cd /home
# du
You can also limit the recursive search dept like so
# du --max-depth=2
which search on the 2nd level of directory only and ignores any folder found above the 2nd level folders.

Getting the summarized return in a human readable form
# du -sh
Alternatively if you wish to get the last time modification
# du -ah --time
4.0K 2007-08-11 11:57 ./file.txt
4.0K 2007-08-11 11:57 ./folder1
12K 2007-08-11 11:57 .

If you are using mbox type of mail storage handling, these commands can be handy checking and reporting partition and/or folder disk usage when incorporated inside a shell scripts. Furthermore, you can create and generate your TOP 10 users with largest mails on monthly or weekly basis that could give you more detailed email report and alerts from it..



At regular interval and again using a script, you can also watch and monitor folder/partition usage changes and alerts you for certain specified thresholds like for /home or /var/ftp or /tmp.

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Insert a file at a specific line and column



How to insert the complete contents of a text file at a specific row and column of another text file.

If we are merely concerned with inserting after a specific line, it can be readily achieved with a number of Linux tools. For example, to insert file1.txt after the second line of file2.txt, any of the following commands will do:
$ sed -i '2r file1.txt' file2.txt
$ awk '{print} NR==2 {while (getline < "file1.txt") print}' file2.txt
Unlike the previous sed command which modifies file2 in-line, the above awk command writes the desired output to standard output only.
$ emacs -batch +3 file2.txt --insert file1.txt -f save-buffer -kill
This uses the batch capability of the emacs text editor. The command opens file2 at line 3, inserts file1, saves and then exits.
$ vi +2 file2.txt << DELIM > :r file1.txt
> :wq
> DELIM
Vim: Warning: Input is not from a terminal
Note that after you enter the first line ("vi +2 ..."), you will be prompted for more input. At that time, you enter the next three lines (:r, :wq, DELIM)
While Linux has many utilities to manipulate text (sed, perl, awk, cut, python), the easiest way I can think of to accomplish my objective to insert at a target line and column is using emacs.
$ emacs -batch -Q +2:3 file2.txt --insert file1.txt -f save-buffer -kill 2>/dev/null
The key is +2:3 which directs the editor to open the file at line 2 column 3.

Two other components in the above emacs command warrant some explanation. First, -Q means quick startup. Quick, because emacs won't load any init file, or any splash file. Second, the last part of the command pipes any standard error output from the emacs editor to the null device.

I don't doubt that sed, awk or perl can do the job. If you have a simple solution, please share with us via the comment feature of this web page. Many thanks.

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Run commands on logout



If a file named $HOME/.logout (a file named .logout in your home directory) exists, and the following trap statement is in your .profile, .logout is executed when you logout.

Add this to .profile:
trap "$HOME/.logout" 0


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Top for files



$ watch -d -n 1 'df; ls -FlAt /path'


This one-liner watches for file changes in directory /path. It uses the watch command that executes the given command periodically. The -d flag tells watch to display differences between the command calls (so you saw what files get added or removed in /path). The -n 1 flag tells it to execute the command every second.

The command to execute is df; ls -FlAt /path that is actually two commands, executed one after other. First, df outputs the filesystem disk space usage, and then ls -FlAt lists the files in /path. The -F argument to ls tells it to classify files, appending */=>@| to the filenames to indicate whether they are executables *, directories /, sockets =, doors >, symlinks @, or named pipes |. The -l argument lists all files, -A hides . and .., and -t sorts the files by time.

Special note about doors - they are Solaris thing that act like pipes, except they launch the program that is supposed to be the receiving party. A plain pipe would block until the other party opens it, but a door launches the other party itself.

Actually the output is nicer if you specify -h argument to df so it was human readable. You can also join the arguments to watch together, making them -dn1. Here is the final version:

$ watch -dn1 'df -h; ls -FlAt /path'


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Convert UTF-8 to WIN-1251



Function to convert UTF-8 to WIN-1251 charset.
function iconv-win1251 {
    if [ ${1%\.*} == ${1##*\.} ]; then
newfile="$1-win1251"
    else
newfile="${1%\.*}-win1251.${1##*\.}"
    fi
    iconv $1 --from-code UTF-8 --to-code WINDOWS-1251  > $newfile
    ls -l $newfile $1
}


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Disk imaging with netcat and dd



Want to create a disk image of a system but write it on another hard disk? This can easily be done with the help of netcat and dd.

For this example you will need two computers connected on the same network, and enough room on one machine to hold your disk image

Destination Machine
So we’ll start off this example by preparing our destination machine to listen on tcp port 4444 via netcat. The port is arbitrary so you can really pick any port that is not being used. Just have to make sure that its the same on both ends.
root@tree:~# netcat -l -p 4444 | dd of=remote-machine.img
Source Machine
Next we’ll start a dd on the source machine and pipe it to netcat on port 4444
root@leaf:~# dd if=/dev/sda1 | netcat destination-machine-ip 4444
Now sit back and wait for your image to be done, when it’s finished dd will print out its status something like

NOTE: you will have to push CTRL+C to cancel out after this is completed, as the netcat session will still be active.
root@leaf:~#
30820468+71926 records in
30867456+0 records out
15804137472 bytes (16 GB) copied, 739.395 s, 21.4 MB/s
^C
If you want to find out the status of dd during the copy theres a couple of ways to do this, open up the system monitor in Ubuntu Linux, and it should tell you the transfer rate. Launch iostat or ifstat through a terminal. Invoke a command from terminal to get dd to display the current progress .

Viola, we’ll now have a dd image of our disk or partition. I like to verify the exact size of the file matches the size output from fdisk.

Destination Machine
root@root:~# ls -la remote-machine.img
-rw-r--r-- 1 root root 15804137472 2010-02-04 10:53 remote-machine.img
Source Machine
root@leaf:~# fdisk -l /dev/sda
Disk /dev/sda: 15.8 GB, 15804137472 bytes
255 heads, 63 sectors/track, 1921 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Block Yahoo Messenger, AOL, MSN and ICQ using IPTABLES



If you’re a network administrator and you’re using Linux on your servers, you can stop the rest of the users from using IM applications by blocking their access to the most-used IM protocols:
ICQ and AOL:
# iptables -A FORWARD –dport 5190 -j DROP
# iptables -A FORWARD -d login.oscar.aol.com -j DROP

MSN:
# iptables -A FORWARD -p TCP –dport 1863 -j DROP
# iptables -A FORWARD -d 64.4.13.0/24 -j DROP
Yahoo Messenger:
# iptables -A FORWARD -p TCP –dport 5000:5010 -j REJECT
# iptables -A FORWARD -d cs.yahoo.com -j REJECT
# iptables -A FORWARD -b scsa.yahoo.com -j REJECT



Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

IPTABLES - blocking IPs simplified



You can block an IP from IPTABLES by using
iptables -I INPUT -s 192.168.0.88 -j DROP
You can simplify things a little bit by using a shell script and a predefined text file containing the IPs we want to be blocked. Either create a new file or add the following at the end of the script which activates the firewall:
#!/bin/sh
for i in $(< bad_hosts.lst) ; do
iptables -I INPUT -i eth1 -s “$i” -j DROP
done
Now create a new file in the same directory and name it bad_hosts.lst and add a new IP to be blocked on every single line, like in the example below:
192.168.2.99
192.168.2.67
86.138.2.7


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Set the clock from command line



If you want to set the date from the Linux command line, you can use thedate command:
date nnddhhmmyy
where
nn is the month
dd is the day
hh is the hour
mm is the minute
yy is the year
For example, date 080717292009 would set the date to August the 7th, 17:29, 2009

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

What RAM type you have in Linux



To check what RAM memory type yo have installed (and also see other useful information about your system), do a
#dmidecode
Depending on the version of dmidecode you have installed and the hardware configuration you have, each hardware device will have a certain type number assigned t it. On my machine, the RAM has type 6. So to see what RAM type and speed you have, do a
#dmidecode --type 6
and the output will be something like
# dmidecode 2.9
SMBIOS 2.3 present.

Handle 0×0008, DMI type 6, 12 bytes
Memory Module Information
Socket Designation: ROW-0
Bank Connections: 1 0
Current Speed: 800
Type: DIMM SDRAM
Installed Size: 256 MB (Double-bank Connection)
Enabled Size: 256 MB (Double-bank Connection)
Error Status: OK


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Update Twitter status from the Linux command line



You can easily update your Twitter status from the CLI by using this one simple command:
curl -u user:password -d status=”Your status message” http://twitter.com/statuses/update.xml
where user is your username and password is your Twitter password entered in plaintext. Replace the text Your status message with anything you wish

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Improve the speed of Firefox 3



We all know Firefox is a memory hog. To speed things up greatly, do the following:

  • Open Firefox and in the address bar type about:config.
  • Click on “I’ll be careful, I promise“
  • Use the search bar above to look for network.http.pipelining and double click on it to set it’s value to True.
  • Create a new boolean value named network.http.pipelining.firstrequest and set that to True, as well.
  • Find network.http.pipelining.maxrequests, double click on it, and change its value to 8.
  • Look for network.http.proxy.pipelining and set it to True.
  • Create two new integers named nglayout.initialpaint.delay andcontent.notify.interval, set them to 0.
  • Restart your browser.

All done. You should feel the browser is 5x more responsive than before while navigating websites.

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Change MAC address in Linux



If you wish to change your MAC address in Linux, all you have to do is bring the interface down then use the hw ether switch:
#ifconfig eth0 down
#ifconfig eth0 hw ether 02:01:02:03:04:08
#ifconfig eth0 up
but if you want your pc to change its MAC address on boot add that to a script in /etc/init.d/ folder, and also add symbolic link(ln) to /etc/rc2.d, /etc/rc3.d, /etc/rc4.d, /etc/rc5.d which refers to the script in /init.d/
#!/bin/bash

ifconfig eth0 down
ifconfig eth0 hw ether 02:01:02:03:04:08
ifconfig eth0 up
/etc/init.d/networking restart


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Generate random passwords using Perl



If you have a server where you have to generate new passwords daily, the following script can automate the process:
#!/usr/bin/perl
my @alphanumeric = (’a’..’z', ‘A’..’Z', 0..9);
my $randpassword = join ”, map $alphanumeric[rand @alphanumeric], 0..8;
print “$passwordgen\n”
Run it with ./passwordgen.pl and it will generate random strings of charaters and numerals, 8 characters long: lf78A7xv

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Backing up the bootsector



Afraid you might mess up your MBR? Back up the bootsector:
dd if=/dev/hda of=bootsector.img bs=512 count=1
If anything goes wrong, you can boot from a LiveCD and restore the bootsector with
dd if=bootsector.img of=/dev/hda


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Popular Posts