Friday, November 28, 2008

Ping Every Host in /etc/hosts File.

If you want to know whether hosts specified in /etc/hosts file are reachable, just give the following command:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1

Sample run:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.046 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.028 ms

--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.028/0.028/0.028/0.000 ms


Friday, November 7, 2008

Missing Linux Entry in GRUB Menu

If any Linux entry is not present in GRUB menu,
you can visit following forum to view a solution.

Linux Forum: Unable to see Fedora 7 in GRUB Menu

Wednesday, November 5, 2008

Finding Best Linux Distribution

We can find ranking of Linux distributions (distros) at site distrowatch.com. This ranking is based on Page Hits Per Day.In order to find best Linux distro, I have written a Perl script:

#!/usr/bin/perl -w
#===============================================================================
#
# FILE: best_distro.pl
#
# USAGE: ./best_distro.pl
#
# DESCRIPTION: Finds best distro from distrowatch.com
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh)
# VERSION: 1.0
# CREATED: Tuesday 04 November 2008 04:05:19 IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

my $ndistro = 1;

if (@ARGV >= 1)
{
$ndistro = $ARGV[0];
if ($ndistro > 100)
{
$ndistro = 100;
}
}

my $lynx = `which lynx`;
chomp($lynx);
if ($lynx eq "")
{
print STDERR "$0: please install lynx\n";
print STDERR "sudo apt-get install lynx\n";
exit(-1);
}

open(LYNX, "lynx -source http://distrowatch.com/ |") or die("$0: cannot open lynx: $!\n");

my $i = 1;

while ($i <= $ndistro)
{
my $line;
my $distro;
my $rank = "";
while ($line = <LYNX>)
{
chomp($line);
$line =~ s/<th class=([^>]*)>(.*?)<\/th>/$2/i;
$line =~ s/(\s+)//g;
if ($line eq "$i")
{
$rank = $line;

unless ($line = <LYNX>) {last;}
chomp($line);
$line =~ s/<td class=([^>]*)><a href=([^>]*)>(.*?)<\/a><\/td>/$3/i;
$line =~ s/(\s+)//g;
$distro = $line;

print "$rank\t$distro\n";
last;
}
}
++$i;
}

close(LYNX);

Sample runs of above program:
$ ./best_distro.pl
1 Ubuntu
$ ./best_distro.pl 5
1 Ubuntu
2 openSUSE
3 Mint
4 Fedora
5 Debian

Tuesday, November 4, 2008

Counts from min to max

In order to get counts from start number to end number,
I have written a shell script.


#!/bin/sh

if [ $# -ne 2 ]
then
echo "Usage: $0 min max"
exit
fi


min=$1
max=$2

while [ $min -le $max ]
do
echo -n $min " "
min=`expr $min + 1`
done
#end script



Sample run:
$ ~/bin/count.sh 2 5
2 3 4 5

Tip: This is very handy script, that is very helpful
in getting counts in the loop. For example:
$ for i in `~/bin/count.sh 2 8`
> do
> echo "Hello $i"
> done

Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8

Friday, October 17, 2008

Total RAM in your System

In order to find total RAM in your system, give following command:

$ cat /proc/meminfo | awk '/^MemTotal/ {print $2/1024 " MB"}'
2008.07 MB

If you all information related to memory:

$ cat /proc/meminfo


Extra:
Total CPU cores in your system:

$ cat /proc/cpuinfo | awk '/^processor/ {ncores++} END {print "Total cores = " ncores}'
Total cores = 2

Thursday, October 16, 2008

Finding Processes, which are Accessing a File System

After completing every task related to CD/DVD/USB, you might find it annoying to unmount/eject CD/DVD/USB, while it is being used by some unknown/stuck process. Therefore I am providing a solution (fuser). Suppose your were accessing CDROM mounted at /mnt/cd/.

To view which files (of CD) are being accessed by whom :
$ fuser -mu /mnt/cd/*

If you are sure that, the processes are stuck/hang. Then you can kill all those processes :
$ fuser -kmu /mnt/cd/*


Warning!!!: If you are using writable medium (like USB stick), please give following command before killing processes:
$ sudo sync

Wednesday, October 15, 2008

Retrieving Date and Time from Unix Timestamp

In Unix, we can retrieve Date and Time from Timestamp by using following command:
$ date -r 1220000000
Fri Aug 29 14:23:20 IST 2008

But in Linux, there is no '-r' option. Therefore, I have written a Perl script to
retrieve date and time from timestamp. Here is the script:

$ vi ~/bin/dater.pl

#!/usr/bin/perl -w

use strict;
use warnings;

my $ts = time;
if (@ARGV >= 1)
{
    $ts = $ARGV[0];
}
my ($sec, $min, $hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
++$mon;
$year+=1900;
print "$mday-$mon-$year $hour:$min:$sec\n";


# end dater.
pl


Sample runs of above program:
$ dater.pl
15-10-2008 12:34:37
$ dater.pl 1220000000
29-8-2008 14:23:20

PS: you can change parameters in print function in the program
according to output you desire.