Saturday, February 26, 2011

Applicatin using dialog


#!/bin/bash
# SCRIPT : menu_dialog.sh
# PURPOSE : A menu driven Shell script using dialog utility
# which has following options:
# Display Today's Date and Time.
# Display calendar.
# Delete selected file from supplied directory.
# List of users currently logged in
# Disk Statistics
# Exit
#
##############################################################################
# Checking availability of dialog utility #
##############################################################################

# dialog is a utility installed by default on all major Linux distributions.
# But it is good to check availability of dialog utility on your Linux box.

which dialog &> /dev/null

[ $? -ne 0 ] && echo "Dialog utility is not available, Install it" && exit 1

##############################################################################
# Define Functions Here #
##############################################################################

###################### deletetempfiles function ##############################

# This function is called by trap command
# For conformation of deletion use rm -fi *.$$

deletetempfiles()
{
rm -f *.$$
}


######################## Show_time function #################################

# Shows today's date and time

show_time()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "DATE & TIME" \
--msgbox "\n Today's Date: `date +"%d-%m-%Y"` \n\n \
Today's Time: `date +"%r %Z"`" 10 60
}

###################### user_add fuction #####################################
user_add()
{
if [ $(id -u) -eq 0 ]; then
#       read -p "Enter username : " username
#       read -s -p "Enter password : " password
dialog --backtitle "MENU DRIVEN PROGRAM" --title "User Add" \
--inputbox "\nEnter User name you want to add " 12 60 2> temp5.$$
username=`cat temp5.$$`

dialog --backtitle "MENU DRIVEN PROGRAM" --title "User Add" \
--inputbox "\nEnter User Password for user $useradd " 12 60 2> temp6.$$
password=`cat temp6.$$`
egrep "^$username" /etc/passwd >/dev/null
        if [ $? -eq 0 ]; then
        dialog --backtitle "MENU DRIVEN PROGRAM" --title "useradd" \
        --msgbox "User $username already exists." 12 25
        else
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                useradd -m -p $pass $username
                [ $? -eq 0 ] && dialog --backtitle "MENU DRIVEN PROGRAM" --title "useradd" \
        --msgbox "User $username has been added to system." 12 25 || dialog --backtitle "MENU DRIVEN PROGRAM" --title "useradd"  --msgbox "User $username failed to add." 12 25
        fi
else
        echo "Only root may add a user to the system"
fi

#dialog --backtitle "MENU DRIVEN PROGRAM" --title "User Add" \
#--inputbox "\nEnter User name you want to add " 12 60 2> temp5.$$
#useradd=`cat temp5.$$`

#dialog --backtitle "MENU DRIVEN PROGRAM" --title "User Add" \
#--inputbox "\nEnter User Password for user $useradd " 12 60 2> temp6.$$
#passwd=`cat temp6.$$`

#egrep "^$useradd" /etc/passwd >/dev/null
#    if [ $? -eq 0 ]; then
#        echo "$username exists!"
#        exit
#    else
#    echo ${passwd} | ${PW} add user ${useradd} -h 0 >/dev/null 2>&1
   
#if [ $? -eq 0 ]
#then
#dialog --backtitle "MENU DRIVEN PROGRAM" --title "useradd" \
#--msgbox "User $useradd is successfully added" 12 25
#else
#dialog --backtitle "MENU DRIVEN PROGRAM" --title "useradd" \
#--msgbox "Failed to add user to the system." 12 25   
#fi
#fi
}

###################### user_del fuction #####################################
user_del()
{
if [ $(id -u) -eq 0 ]; then
#       read -p "Enter username : " username
#       read -s -p "Enter password : " password
dialog --backtitle "MENU DRIVEN PROGRAM" --title "User del" \
--inputbox "\nEnter User name you want to delete " 12 60 2> temp55.$$
username=`cat temp55.$$`

egrep "^$username" /etc/passwd >/dev/null
        if [ $? -eq 0 ]; then
    userdel $username
        dialog --backtitle "MENU DRIVEN PROGRAM" --title "userdel" \
        --msgbox "User $username succsessfully deleted." 12 25
        else
 dialog --backtitle "MENU DRIVEN PROGRAM" --title "userdel" \
        --msgbox "User $username has been not found in  system." 12 25
        fi
else
        echo "Only root may add a user to the system"
fi
}
###################### log_clear fuction #####################################
####################### show_cal function ###################################

# Shows current month calendar

show_cal()
{
dialog --backtitle "MENU DRIVEN PROGRAM" --title "CALENDAR" \
--msgbox "`cal`" 12 25
}

####################### deletefile function #################################

# Used to delete file under supplied directory, not including sub dirs.

deletefile()
{

dialog --backtitle "MENU DRIVEN PROGRAM" --title "Directory Path" \
--inputbox "\nEnter directory path (Absolute or Relative) \
\nPress just Enter for current directory" 12 60 2> temp1.$$

if [ $? -ne 0 ]
then
rm -f temp1.$$
return
fi

rmdir=`cat temp1.$$`

if [ -z "$rmdir" ]
then
dirname=$(pwd) # You can also use `pwd`
rmdir=$dirname/*
else

# remove trailing * and / from directory path

echo "$rmdir" | grep "\*$" &> /dev/null && rmdir=${rmdir%\*}
echo "$rmdir" | grep "/$" &> /dev/null && rmdir=${rmdir%/}

# Check supplied directory exist or not

( cd $rmdir 2>&1 | grep "No such file or directory" &> /dev/null )

# Above codeblock run in sub shell, so your current directory persists.

if [ $? -eq 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Validating Directory" \
--msgbox "\n $rmdir: No such file or directory \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi

# Do you have proper permissions ?

( cd $rmdir 2> /dev/null )

if [ $? -ne 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Checking Permissions" \
--msgbox "\n $rmdir: Permission denied to access this directory \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi

if [ ! -r $rmdir ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Checking Permissions" \
--msgbox "\n $rmdir: No read permission \
\n\n Press ENTER to return to the Main Menu" 10 60
return
fi

dirname=$rmdir
rmdir=$rmdir/* # get all the files under given directory

fi

for i in $rmdir # process each file
do

# Store all regular file names in temp2.$$

if [ -f $i ]
then
echo " $i delete? " >> temp2.$$
fi

done

if [ -f temp2.$$ ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Select File to Delete" \
--menu "Use [UP/DOWN] keys to move, then press enter \
\nFiles under directory $dirname:" 18 60 12 \
`cat temp2.$$` 2> file2delete.$$
else
dialog --backtitle "MENU DRIVEN PROGRAM" --title "Select File to Delete" \
--msgbox "\n\n There are no regular files in $dirname directory" 10 60
return
fi

rtval=$?

file2remove=`cat file2delete.$$`

case $rtval in

0) dialog --backtitle "MENU DRIVEN PROGRAM" --title "ARE YOU SURE" \
--yesno "\nDo you Want to Delete File: $file2remove" 7 70


if [ $? -eq 0 ]
then
rm -f $file2remove 2> Errorfile.$$

# Check file successfully deleted or not.

if [ $? -eq 0 ]
then
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : FILE DELETED" \
--msgbox "\nFile : $file2remove deleted" 8 70
else
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : ERROR ON DELETION" \
--msgbox "\nProblem in Deleting File: $file2remove \
\n\nError: `cat Errorfile.$$` \n\nPress ENTER to return to the Main Menu" 12 70
fi

else
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "Information : DELETION ABORTED" \
--msgbox "Action Aborted: \n\n $file2remove not deleted" 8 70
fi ;;

*) deletetempfiles # Remove temporary files
return ;;
esac

deletetempfiles # remove temporary files
return
}

########################## currentusers function ############################

currentusers()
{
who > userslist.$$
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "CURRENTLY LOGGED IN USERS LIST" \
--textbox userslist.$$ 12 60
}

############################ diskstats function #############################

diskstats()
{
df -h | grep "^/" > statsfile.$$
dialog --backtitle "MENU DRIVEN PROGRAM" \
--title "DISK STATISTICS" \
--textbox statsfile.$$ 10 60
}

##############################################################################
# MAIN STRATS HERE #
##############################################################################
trap 'deletetempfiles'  EXIT     # calls deletetempfiles function on exit

while :
do

# Dialog utility to display options list

    dialog --clear --backtitle "MENU DRIVEN PROGRAM" --title "MAIN MENU" \
    --menu "Use [UP/DOWN] key to move" 12 60 6 \
    "DATE_TIME" "TO DISPLAY DATE AND TIME" \
    "CALENDAR"  "TO DISPLAY CALENDAR" \
    "DELETE"    "TO DELETE FILES" \
    "USERS"     "TO LIST CURRENTLY LOGGED IN USERS" \
    "DISK"      "TO DISPLAY DISK STATISTICS" \
    "USERADD" "TO ADD NEW user" \
    "USERDEL" "TO delete Old User" \
     "EXIT"      "TO EXIT" 2> menuchoices.$$

    retopt=$?
    choice=`cat menuchoices.$$`

    case $retopt in

           0) case $choice in

                  DATE_TIME)  show_time ;;
                  CALENDAR)   show_cal ;;
                  DELETE)     deletefile ;;
                  USERS)      currentusers ;;
                  DISK)       diskstats ;;
          USERADD) user_add;;
          USERDEL) user_del;;
                  EXIT)       clear; exit 0;;

              esac ;;

          *)clear ; exit ;;
    esac
done

Tuesday, February 22, 2011

log clear automatically


#!/bin/bash
remote_user=root
remote_address=192.168.100.200
ssh $remote_user@$remote_address "sh /mnt/raid1/j/avmper/clean/logclear_openfiler.sh"

#!/bin/bash
todaydate=`date +%F`
if [ ! -d "/mnt/raid1/j/avmper/log/$todaydate" ];then
echo "welcome to log clear process"
echo "\n now script take backup"
cp -rf /var/log/ /mnt/raid1/j/avmper/log/$todaydate
find /var/log/ -type f -not \( -name "*.gz" \) > /mnt/raid1/j/avmper/log/$todaydate/folderfilelist
echo "script clear all log files of /var/log/"
for i in `cat /mnt/raid1/j/avmper/log/$todaydate/folderfilelist`
do
cat /dev/null > $i;
done
echo " \n log clear process is successfully completed "
else
echo "log clear of $todaydate process is already done it"
fi

Extend Lvsize by adding new Hard disk

 
Extend Lvsize by adding new Hard disk


Extend VG Size by Adding new Hard disk /dev/hdc

Create New hard disk drive and convert it int lvm

# fdisk -l
# fdisk /dev/sdc
p
n
Enter
Enter
t
8e
w

Extend pv size

# fdisk -l
# lvmdiskscan

skip…
/dev/sdc1 [ 100.00 GB]
…skip…
# pvcreate /dev/sdc1
Physical volume “/dev/sdc1″ successfully created


Extend vgsize
# vgdisplay
--- Volume group ---
VG Name amco
System ID
Format lvm2
…skip…



# vgextend vgname_whatever /dev/sdc1
Volume group “VolGroup01″ successfully extended

Extend For each Lv size
# lvextend -L +2G /dev/vgname/lvname
Extending logical volume LogVol00 to 2 GB
Logical volume LogVol00 successfully resized
OPTIONAL
Extend the logical volume to use the full size of the volume group:
# lvm lvextend -l +100%FREE /dev/VolGroup01/LogVol00
Extending logical volume LogVol00 to 449.99 GB
Logical volume LogVol00 successfully resized
resize the partition for the OS to recognize the new size
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 155G 569M 146G 1% /
/dev/sda1 99M 9.0M 85M 10% /boot
tmpfs 95M 0 95M 0% /dev/shm
/dev/mapper/amco-g 2.0G 36M 1.9G 2% /mnt/amco/g
/dev/mapper/amco-h 2.0G 36M 1.9G 2% /mnt/amco/h
/dev/mapper/amco-j 20G 173M 19G 1% /mnt/amco/j
/dev/mapper/amco-k 20G 173M 19G 1% /mnt/amco/k
/dev/mapper/amco-l 21G 394M 19G 3% /mnt/amco/l
# resize2fs -p /dev/mapper/volgroupname-logvolname