Friday, June 17, 2011

Some Scripting Excersises

1.) Using Test Command to find Wether entered Argument is File or Directory.

[root@server2 ~]#cat
test -d $1
ks=`echo $?`
if [ $ks -eq "0" ];  then
 echo $1 is Directory;
 echo "Entered Argument is not Directory"
[root@server2 ~]#
[root@server2 ~]#./ /etc
/etc is Directory
[root@server2 ~]#
[root@server2 ~]#./ /etc/passwd
Entered Argument is not Directory
[root@server2 ~]#

2.) Bash For Loop Example for Unzip all the Zip file

The following example finds the list of files which matches with “*.zip*” in the root directory, and creates a new directory in the same location where the zip file exists, and unzip the zip file content.
# cat
#! /bin/bash
# Find files which has .zip
for file in `find /root -name "*.zip*" -type f`

# Skip the extension .zip
dirname=`echo ${file} | awk -F'.' '{print $1}'`

# Create the directory
mkdir $dirname

# Copy the zip file
cp ${file} ${dirname}
cd $dirname

# Unzip the zip file from newly created directory
unzip ${dirname}/$(echo ${file##/*/})
  • In this example find command returns the list of files, from which each file will be processed through a loop.
  • For each item, it creates the directory with the name of the zip file, and copies the zip file to the newly created directory and unzip the zip file from there.
  • The echo statement, echo ${file##/*/} gives you only the file name not the path.

3.) Processing a File Line by Line using script.

 [root@server199 ~]# cat
# PURPOSE : Proscess a File line by line and read its contents

while read LINE
        let count++
        echo "$count $LINE"
done < $FILENAME

echo -e "\nTotal $count Lines read"

[root@server199 ~]# ./
1 #!/usr/bin/perl
2 # print real UID
3 print "Real UID: $4 # print real GID
5 print "Real GID: $(n";
6 # print effective UID
7 print "Effective UID: $>n";
8 # print effective GID
9 print "Effective GID: $)n";

Total 9 Lines read

During Execution of the script we specify One command line Argument i.e. the name of any file. Then this script make the use of while-read loop for reading and displaying the file line by line.

!Enjoy Linux

Kuldeep Sharma

Tuesday, June 14, 2011

Yum groupinstall !

Yum groupinstall may save your hours!

Today I was creating some kind of setup, So during performing those installation steps, I have found one interesting and time saving option that is used with yum command  and can make our tasks really easy. The option is groupinstall and the whole command is

"yum groupinstall <"package name">".

Though I was little aware of this But have never gone through this. So today I have just done some R&D on this and here is the result. The option "groupinstall" is used to install all the package related to particular type of software. Suppose I want to install all Mysql Database Packages, then instead of installing packages one by one you can make the use of command given below.

[root@server2 ~]#yum groupinstall "MySQL Database" 

Now question may arise from where I have got string <"MySQL Database">, So answer is before running above command use following command for listing available groups. It will shows you list of installed groups along with Available groups.

[root@server2 ~]#yum grouplist
Loaded plugins: fastestmirror, priorities
Setting up Group Process
Loading mirror speeds from cached hostfile
 * addons:
 * base:
 * epel:
 * extras:
 * updates:
Installed Groups:
   Administration Tools
   Authoring and Publishing
   Cluster Storage
   DNS Name Server
   Dialup Networking Support
   Engineering and Scientific
   FTP Server
   GNOME Desktop Environment
   GNOME Software Development
   Games and Entertainment
   Graphical Internet
   Legacy Network Server
   Legacy Software Development
   Legacy Software Support
   Mail Server
   Network Servers
   Printing Support
   Server Configuration Tools
   Sound and Video
   System Tools
   Text-based Internet
   Windows File Server
   X Window System
   Yum Utilities
Available Groups:
   Development Libraries
   Development Tools
   Educational Software
   Electronic Lab
   Fedora Packager
   FreeNX and NX
   Hardware Support
   Java Development
   KDE (K Desktop Environment)
   KDE Software Development
   MySQL Database
   News Server
   OpenFabrics Enterprise Distribution
   PostgreSQL Database
   Web Development
   Web Server
   Window Managers
   X Software Development
[root@server2 ~]#

Now we know the all available groups name. So its time to execute.You should type the name as shown in the list including the quotation marks. It will install everything on that group.

Be careful before using it. It may install many unnecessary things. Use it where it is really necessary.

One More Case May arise that what if you want to get list of packages that is Default Packages and Optional Packages that particular group contains. In that Case use following command.

#yum groupinfo <"Group Name"> 


[root@server2 ~]#yum groupinfo "MySQL Database"
Loaded plugins: fastestmirror, priorities
Setting up Group Process
Loading mirror speeds from cached hostfile
 * addons:
 * base:
 * epel:
 * extras:
 * updates:
Group: MySQL Database
 Description: This package group contains packages useful for use with MySQL.
 Mandatory Packages:
 Default Packages:
 Optional Packages:
[root@server2 ~]#

Hope it will be Helpfull.


Kuldeep Sharma

Thursday, June 2, 2011

Identifying main traffic sources with netstat and awk (one-liner explained)

This is line command to get rid of All the hosts using web server. For this we can make the use of handy netstat command.
Sample of eventual output:
#netstat -natp | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | tail

You can use this command on any other port that you want to search.
Let me break this long command and explain the things to make them more understandabe.

First of all – how many connections are there to the web server:
#netstat -natp | grep :80 | wc -l
netstat is a very versatile tool.
In this case, the flags being used state the following:
-n” Numerical representation of the hosts rather than attempt to resolve to addresses
-a” All traffic (listening and non-listening sockets)
-t” TCP traffic only (UDP is a whole other ballgame)
-p” The PID of the process using the port – Just a course of habit for me – since I usually want to know
who is listening and taking up a port.
grep :80
Since this example deals with a web server, so we take port 80.
wc -l  # Count total number of lines.
A typical output for netstat -natp | grep :80 ::
tcp        0      0          TIME_WAIT   -                  
tcp        0      0          TIME_WAIT   -                  
tcp        0      0          TIME_WAIT   -                  
tcp        0   2885          ESTABLISHED 10439/nginx: worker
tcp        0      0          TIME_WAIT   -                  
tcp        0      0          TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0             TIME_WAIT   -                  
tcp        0      0          FIN_WAIT2   -                  
tcp        0      0          TIME_WAIT   -                  
tcp        0      0          TIME_WAIT   -

Next in Command we have make the use of *nix native tool "awk, sort, cut and uniq"  to get a
nice representation of the top port 80 tcp offenders.

awk '{print $5}'
Will give us the fifth column:
awk of course is a very powerful tool – however here we will just be using its most common function – printing a specific column.
We still need to clean it up a bit though – since we don’t really care about the remote port.
In this case we can either invoke another awk, or use the simple tool cut – here are the two options:

awk -F ":" '{print $1}'
cut -d: -f1
These two will basically do the same thing: in awk, the “-F” flag states the field delimiter (in this case the colon “:”) and print the first column.
With cut, the “-d” flag states the delimiter (in this case the colon), and “-f1” tells it to use the first field.
Now we finally have a simple clean list of lots of IPs.
All that is left is to sort them, count how many unique IPs there are and sort the output of that test.

sort | uniq -c | sort -n
First we must sort, otherwise uniq doesn’t work.
-c” tells uniq to count the occurences of each unique object.
In sort, “-n” tells it to do a proper numerical sorting rather than alphabetical, otherwise “10″ will come before “2″.
Finally the one-liner and its output:

#netstat -natp | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n | tail

This means that and alone comprise more than half of all connections
 to the server at port 80 – might raise a few red flags…

!Enjoy Hope It'll helpfull 4 You.


Wednesday, June 1, 2011

Some Useful Info about Firefox's "about" Protocal

Firefox "about" Protocol

First of all :
What is Protocol in Firefox?
                       A protocol is the part of a web address before the colon. For example, web pages are normally http or https protocols. If you click on a link that specifies a protocol other than http: or https: (such as aim:goim?screenname=MozillaSupport), you may receive an error message like:
Firefox doesn't know how to open this address, because the protocol (aim) isn't associated with any program.

            Today I have found some interesting thing about firefox.After that I have found that we can do a lots of things with firefox or we can make some changes in in "about:config" section to increase performance of Firefox. In Detail I'll write in Next post. In this post I will just show the different options with about protocol with you can play and can get a lots of usefull information.

Here is the list :

Please feel free to comment.