Pages

Friday, June 17, 2011

Some Scripting Excersises

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

[root@server2 ~]#cat test.sh
test -d $1
ks=`echo $?`
if [ $ks -eq "0" ];  then
 echo $1 is Directory;
 else
 echo "Entered Argument is not Directory"
fi
[root@server2 ~]#
[root@server2 ~]#./test.sh /etc
/etc is Directory
[root@server2 ~]#
[root@server2 ~]#./test.sh /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 zip_unzip.sh
#! /bin/bash
# Find files which has .zip
for file in `find /root -name "*.zip*" -type f`
do

# 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##/*/})
done
  • 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 fileread.sh
#!/bin/bash
# SCRIPT : fileread.sh
# PURPOSE : Proscess a File line by line and read its contents

FILENAME=$1
count=0
while read LINE
do
        let count++
        echo "$count $LINE"
done < $FILENAME

echo -e "\nTotal $count Lines read"

[root@server199 ~]# ./fileread.sh reids.pl
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: mirror.neu.edu.cn
 * base: mirror.neu.edu.cn
 * epel: ftp.jaist.ac.jp
 * extras: mirror.neu.edu.cn
 * updates: centosv4.centos.org
Installed Groups:
   Administration Tools
   Authoring and Publishing
   Cluster Storage
   Clustering
   DNS Name Server
   Dialup Networking Support
   Editors
   Engineering and Scientific
   FTP Server
   GNOME Desktop Environment
   GNOME Software Development
   Games and Entertainment
   Graphical Internet
   Graphics
   Java
   Legacy Network Server
   Legacy Software Development
   Legacy Software Support
   Mail Server
   Network Servers
   Office/Productivity
   Printing Support
   Server Configuration Tools
   Sound and Video
   System Tools
   Text-based Internet
   Windows File Server
   X Window System
   Yum Utilities
Available Groups:
   Base
   Beagle
   Development Libraries
   Development Tools
   Educational Software
   Electronic Lab
   Emacs
   Fedora Packager
   FreeNX and NX
   Hardware Support
   Horde
   Java Development
   KDE (K Desktop Environment)
   KDE Software Development
   KVM
   Mono
   MySQL Database
   News Server
   OpenFabrics Enterprise Distribution
   PostgreSQL Database
   Ruby
   Tomboy
   Virtualization
   Web Development
   Web Server
   Window Managers
   X Software Development
   XFCE-4.4
Done
[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"> 

e.g.

[root@server2 ~]#yum groupinfo "MySQL Database"
Loaded plugins: fastestmirror, priorities
Setting up Group Process
Loading mirror speeds from cached hostfile
 * addons: mirror.neu.edu.cn
 * base: mirror.neu.edu.cn
 * epel: ftp.jaist.ac.jp
 * extras: mirror.neu.edu.cn
 * updates: centosv4.centos.org
Group: MySQL Database
 Description: This package group contains packages useful for use with MySQL.
 Mandatory Packages:
   mysql
 Default Packages:
   MySQL-python
   libdbi-dbd-mysql
   mysql-connector-odbc
   mysql-server
   perl-DBD-MySQL
   unixODBC
 Optional Packages:
   mod_auth_mysql
   mysql-bench
   mysql-devel
   php-mysql
   qt-MySQL
[root@server2 ~]#


Hope it will be Helpfull.

!Enjoy

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

     25 195.150.23.130
     25 67.222.164.140
     28 95.34.20.117
     31 72.45.232.204
     34 209.56.4.6
     36 64.27.200.208
    106 50.17.245.114
    112 209.234.226.230
    247 216.242.75.236
    283 184.106.21.219
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
459
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 123.231.146.176:80          92.35.20.117:12205          TIME_WAIT   -                  
tcp        0      0 123.231.146.176:80          92.35.20.117:64428          TIME_WAIT   -                  
tcp        0      0 123.231.146.190:80          92.35.20.117:20645          TIME_WAIT   -                  
tcp        0   2885 123.231.146.176:80          92.35.20.117:57267          ESTABLISHED 10439/nginx: worker
tcp        0      0 123.231.146.190:80          92.35.20.117:50365          TIME_WAIT   -                  
tcp        0      0 123.231.146.176:80          92.35.20.117:52670          TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40214       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40199       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40248       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40229       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40151       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40185       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:40169       69.4.187.136:80             TIME_WAIT   -                  
tcp        0      0 123.231.146.176:80          92.35.20.117:10938          FIN_WAIT2   -                  
tcp        0      0 123.231.146.176:80          92.35.20.117:63684          TIME_WAIT   -                  
tcp        0      0 123.231.146.190:80          92.35.20.117:62401          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:
92.35.20.117:12205
92.35.20.117:64428
92.35.20.117:20645
92.35.20.117:57267
92.35.20.117:50365
92.35.20.117:52670
69.4.187.136:80
69.4.187.136:80
69.4.187.136:80
69.4.187.136:80
69.4.187.136:80
69.4.187.136:80
69.4.187.136:80
92.35.20.117:10938
92.35.20.117:63684
92.35.20.117:62401
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

     25 195.150.23.130
     25 67.222.164.140
     28 95.34.20.117
     31 72.45.232.204
     34 209.56.4.6
     36 64.27.200.208
    106 50.17.245.114
    112 209.234.226.230
    247 216.242.75.236
    283 184.106.21.219
This means that 184.106.21.219 and 216.242.75.236 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.

Kuldeep

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 :
about:
about:blank
about:buildconfig
about:cache
about:cache-entry
about:config
about:crashes
about:credits
about:license
about:mozilla
about:plugins
about:rights
about:rights#webservices
about:robots
about:support


Please feel free to comment.


!Enjoy