Shashikant shah

Wednesday, 31 December 2014

How to set Variable in script

#!/bin/bash

source /root/shashi/credentials.prop

env=$1

if [ "$env" = = '' ] || [ "$username" = = '' ] || [ "$password" = = '' ];then

        echo "usuage ./build.sh"
exit
fi

source /root/shashi/properties

if [[ $codebasedir == '' ]]; then

        echo -n "Please provide tpd all package file:"
        read -s codebasedir
        echo
fi

[ ! -d "log" ] && mkdir log
[ ! -d "$codebasedir" ] && echo code base dir not found.

##validation for curl installation

verify_curl()
{
which curl > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "curl is not installed"
exit
else
echo "curl version is : `curl --version | head -1 |cut -d" " -f1,2`"
fi
}

##first test that we have the correct password
verify_password()
{
for x in `cat /root/shashi/environment/"$env"`
do
curl -s -f -k --user $username:$password $x/home/users/a/$username.json | grep -q 'rep'
if [ $? -eq 0 ]
then
echo "Username and Password is correct :$x"
else
echo "you don't have the correct username or password : $x"
exit
fi
done
}
check_app(){
if [ ${env} = = 'true' ] ; then 
        verify_curl
        verify_password
fi     
}
verify_curl
verify_password
check_app

===============================================================
#!/bin/bash
jail="${1:-/home/shashi}"
echo "set at ${jail}"

#!/bin/bash
var=${USER:-not_found}

=====================
#!/bin/bash
var=${USER:=value}


#!/bin/bash
var=${USER:value}
d=${var:=not_found}

sh -x script.sh
====================

#!/bin/bash
path=${1:?ERROR command line arg not passed}
echo "Backup path is $path."
echo "I m done if \$path is set."

./script.sh /home

Friday, 5 December 2014

How to replace a string in a file

#!/bin/bash

echo -n "Enter word:- "
read word

echo -n "location Search:- "
read search

E=`grep -r "$word" $search | awk -F':' '{print $1}' | uniq`
I=`grep -r "$word" $search | awk -F':' '{print $1}' | uniq | wc -l`

echo "$E" > /test/log

if [ "$I" == 0 ];then

        echo "file is not found."
else

echo -n "Enter Replace word:- "

read new

for x in `cat /test/log`
   do
        sed -i "s/$word/$new/g" $x
done
echo "***** Repalce file name logs '/test/log' ******"
echo "*****.Replace has been done.*****"

fi

Wednesday, 26 November 2014

How to replace particular word in a file

#!/bin/bash

echo -n "Enter File Name :- "
read name

echo -n "Enter File Path :- "
read path

file=`echo $path$name` > /dev/null

if [[ ! -f "$file" ]];then

        echo "'$file' Please Check Path & File Name..!"
exit 2
fi

echo -n "Enter Replace Word :- "
read old

E=`grep -oh "\W*\w*$old\w*\W*" $file | head -1 && grep -oh "\W*\w*$old\w*\W*" $file | sort -n | grep -w $old | head -1`

echo -n $E > /tmp/a

s=`grep -oh "\W*\w*$old\w*\W*" /tmp/a | sort | head -1`

if [[ "$old" != $s ]];then

        echo "'$old' Word is not Found in file."
exit 3
else

echo -n "Enter New Word :-  "
read new
f=`cp -r $file $file.old`
        echo "backup is created"

for runtime in 1 2 3 4 5
do
E=`sed -i "s/\(^\| \ .\)$old\( \|$\)/$new /g" $file | sed -i "s/\(.^\| \)$old\( \|$\)/ $new /g" $file`
done
echo "File has been replaces"
exit 0
fi

Tuesday, 18 November 2014

RPM Details


1. Find out what package a file belongs.

# rpm -qf /etc/passwd

2. Remove and installed package without checking for dependencies.

# rpm -ev --nodeps {rpmname}

# rpm -ivh --nodeps {rpmname}

3.Display list all installed packages

# rpm -qa {rpmname}

4.Display installed information along with package version and short description

# rpm -qi {package}

5.Display list of configuration file for a package

# rpm -qc {pacakge-name}

6.Find out what dependencies a rpm file has

# rpm -qpR {.rpm-file}

7.Display list of all recently installed RPMs

# rpm -qa --last

8.How To rebuild Corrupted RPM Database.

# cd /var/lib
# rm __db*
# rpm --rebuilddb
# rpmdb_verify Packages

Thursday, 13 November 2014

BASH Script for service check

  
#!/bin/bash
PIDFILE=/tmp/phonemonitor.pid
PHPSCRIPT=/usr/local/phonemonitor/monitor.php
echo 'Checking if our monitor script is active'
if [ -f $PIDFILE ] ; then
    PID=`cat $PIDFILE`
    if ps ax | grep -v grep | grep $PID > /dev/null
    then
        echo "Script is running"
    else
        echo "Script is not running - attempting to Start"
        rm -f $PIDFILE
        php $PHPSCRIPT & echo $! > $PIDFILE&
    fi
else
    echo "Script is not running - attempting to Start"
    php $PHPSCRIPT & echo $! > $PIDFILE&
fi

Saturday, 23 August 2014

Nagios Plugin for check ESTABLISHED connection "Web-Server" on Linux

#!/bin/bash

# Check for missing parameters
if [[ -z "$1" ]] || [[ -z "$2" ]] || [[ -z "$3" ]]; then
        echo "Missing parameters! Syntax: ./check_netstat.sh HOSTNAME WARNING_THRESHOLD CRITICAL_THRESHOLD"
        exit 2
fi

host=$1
port=$2
Warning=$3
Critical=$4

Time=`netstat -an | grep :$port | grep -c TIME_WAIT`;
Est=`netstat -an | grep :$port | grep -c ESTABLISHED`;
Close=`netstat -an | grep :$port | grep -c CLOSE_WAIT`;

# Connection is Establish---OK;

if [[ "$Est" -lt "$Warning" ]]; then
        echo "OK - ESTABLISHED $Est, TIME_WAIT $Time, CLOSE_WAIT $Close"
        exit 0
fi

# Connection is WARNING---WARNING;

if [[ "$Est" -gt "$Warning" ]] && [[ "$Est" -lt "$Critical" ]]; then
        echo "WARNING - ESTABLISHED $Est, TIME_WAIT $Time, CLOSE_WAIT $Close"       
        exit 1
fi

# Connection is CRITICAL---CRITICAL;

if [[ "$Est" -gt "$Critical" ]]; then
       echo "CRITICAL - ESTABLISHED $Est, TIME_WAIT $Time, CLOSE_WAIT $Close"       
        exit 2
fi

Monday, 18 August 2014

Shell Script To Read File Date – Last Access / Modification Time

#!/bin/bash
# Write a shell script to display / read file date / time in following format:
# Time of last access
# Time of last modification
# Time of last change
#
  FILE="$1"
 
  # make sure we got file-name as command line argument
if [ $# -eq 0 ]
then
  echo "$0 file-name"
  exit 1
fi

which stat > /dev/null

# make sure stat command is installed
if [ $? -eq 1 ]
then
  echo "stat command not found!"
  exit 2
fi

# make sure file exists
if [ ! -e $FILE ]
then
  echo "$FILE not a file"
  exit 3
fi

# use stat command to get info
echo "Time of last access : $(stat -c %x $FILE)"
echo "Time of last modification : $(stat -c %y $FILE)"
echo "Time of last change : $(stat -c %z $FILE)"

Wednesday, 23 July 2014

How to Install JDK 1.6 update 32 on ubuntu-12-04



How to Install JDK 1.6 update 32 on ubuntu-12-04

Currently Oracle prohibit the community from adding java to the linux repositories. So it needed to be manually download from oracle website and installed.

Java can be downloaded from:
From: http://www.devsniper.com/ubuntu-12-04-install-sun-jdk-6-7/

Steps to install java:
1.) Make the bin file executable:

1
chmod +x jdk-6u32-linux-x64.bin
2.) Extract the bin file:

1
./jdk-6u32-linux-x64.bin
3.) Move extracted folder:

1
sudo mv jdk1.6.0_32 /usr/lib/jvm/
4.) Install new java source in system:

1
2
3
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_32/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_32/bin/java 1
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_32/bin/javaws 1
5.) Choose default java:

1
2
3
4
5
sudo update-alternatives --config javac
sudo update-alternatives --config java
sudo update-alternatives --config javaws
java version test:
java -version
6.) Verify the symlinks all point to the new java location:

1
ls -la /etc/alternatives/java*

Friday, 25 April 2014

How to Resolve INIT: Id "x" respawning too fast: disabled for 5 minutes


INIT: Id "x" respawning too fast: disabled for 5 minutes

In most distributions this means that the system is booting by default into runlevel 5, which is supposed to respawn (re-start again after it’s been exited) a gui login via x windows, kdm, gdm, or whatever, but the system can’t locate the program. As a result below logs will keep appearing on your /var/log/messages for every 5 minutes.

# vi /etc/inittab
 id:3:initdefault:
 
# reboot server
 
# yum grouplist
# yum groupinstall “X Window System”
# yum groupinstall “GNOME Desktop Environment”
#change runlevel
# vi /etc/inittab
 id:5:initdefault:

You have now have successfully installed GNOME GUI. Restart the server and you will have GUI on your screen.

How to enable grouplist in yum : RHEL-5

How to enable grouplist in yum

You may encounter an error in yum if you type the following command :

[root@shashi ~]# yum grouplist

Error: No group data available for configured repositories.

In order to solve this problem do the following:

 [root@shashi ~]# yum install yum-utils

Copy your entire dvd to any location in your system
and configure yum.

I have copied my entire DVD to the
‘/var/ftp/pub/repo/’ directory.

And I have already configured yum.

In order to enable group list feature execute the following.

[root@shashi ~]# createrepo –g /var/ftp/pub/repo/server/repodata/21f8a36d9
bca1b5b606702c5ce46b65017360348cace161b7910edfaa3cb087
-comps-rhel6-Server.xml     var/ftp/pub/repo/Packages

# The starting filename in the repodata folder can be anything
# but you have to select the one which ends
# with ‘-comps-rhel6-Server.xml’.

Clean all the yum cache

[root@shashi ~]# yum clean all

Make the cache again.

[root@shashi ~]# yum makecache

Check if the grouplist feature is working.

[root@shashi ~]# yum grouplist