BASH Shell Script for Multiple Usage ......................
Bash shell script which perform following tasks:
Bash shell script which perform following tasks:
- Ping_Status: This option ping all the hosts and verify the results
- Host_Identification: To identify host Operating System
- SSH_Login: To Remote login into host
- Login_Failure: It provides information about unauthorized login attempts or any other problems
- Service_Status: Check various service status and START/STOP them as per your requirement
- Memory_Status: It can display system summary information as well as list of tasks currently being managed by the Linux kernel.
- Local_Backup: Backup of Local Host
- Remote_Backup: Backup to Remote Host
- Local_Restore: Restore backup file of local host
- Remote_Restore: Restore of backup file to Remote Host
- Package_Verification: Verify packages installed/uninstalled
- Multiple_Package_Installation: Install multiple packages on host
- Single_Package_Installation: Install packages one by one on host
- Package_Uninstallation: Uninstall packages
- Quit: Exit the script.
Entire code is as follows:
#!/bin/bash
#Author: Trupti
#Date: 29.11.2011
#Purpose: Illustrate usage of select
PS3='Please select a choice: '
LIST="Ping_Status Host_Identification SSH_Login Login_Failure Service_Status Memory_Status Local_Backup Remote_Backup Local_Restore Remote_Restore
Package_Verification Multiple_Package_Installation Single_Package_Installation Package_Uninstallation Quit"
function pingstatus () {
echo -en "Enter the machine: "; read machine
ping -c 2 $machine > /dev/null
if [ $? = 0 ]
then
echo "$machine is REACHABLE via ping"
else
echo "$machine is NOT REACHABLE via ping"
fi
}
function hostidentification () {
echo -en "Enter the MACHINE: "; read MACHINE
ping -c 2 $MACHINE > /dev/null
if [ $? = 0 ]
then
echo "$MACHINE is REACHABLE via ping"
nmap -O $MACHINE > nmap.txt
HOSTNMAP=`cat nmap.txt | grep OS`
echo "Host Identification is as follows: "
echo "$HOSTNMAP"
else
echo "$MACHINE is NOT REACHABLE via ping"
fi
}
function sshsession () {
echo -en "Enter the server: "; read server
ping -c 2 $server > /dev/null
if [ "$?" = 0 ]
then
echo "$server is UP and RUNNING"
echo -en "Do you want to connect this server through SSH session(yes/no): "; read check
if [ "$check" = "yes" ] || [ "$check" = "YES" ]
then
echo -en "Please provide the username for the server: "; read username
ssh $server -l $username
else
exit
fi
elif [ "$?" = 1 ]
then
echo "$server is UNREACHABLE"
fi
}
function loginfailure () {
NUMBER=`cat /var/log/secure | grep -i Failed | wc -l`
LIST=`cat /var/log/secure | grep -i Failed`
echo "Unauthorized logins are: $LIST"
echo "Number of unauthorized users are: $NUMBER"
}
function servicestatus () {
echo -en "Enter the Service name: "; read perservice
service $perservice status > /dev/null
if [ "$?" = "0" ]
then
echo "$perservice service is Running"
echo -en "Do you want to STOP the service(yes/no): "; read check
if [ "$check" = "yes" ] || [ "$check" = "YES" ]
then
echo "Stopping..............."
service $perservice stop
else
exit
fi
else
echo "$perservice service is NOT Running"
echo -en "Do you want to START the service(yes/no): "; read check
if [ "$check" = "yes" ] || [ "$check" = "YES" ]
then
echo "Starting..............."
service $perservice start
else
exit
fi
fi
}
function memorystatus () {
echo "Check the Memory status"
top -d10
}
function localbackup () {
function backupdirpath() {
if [ ! -e $BACKUPDIRPATH ]
then
echo "No such file or directory. Please give the correct path"
echo -en "Enter the path for backup directory: "; read BACKUPDIRPATH
backupdirpath
fi
}
echo -en "Enter the BACKUP Directory Name: "; read BACKUPDIRNAME
echo -en "Enter the BACKUP Directory Path: "; read BACKUPDIRPATH
backupdirpath
BACKUPFILE=scripts.backup.`date +%F`.tgz
if [ ! -e $BACKUPDIRPATH/$BACKUPDIRNAME ]
then
echo "Creating backup directory because it does not exist!"
mkdir $BACKUPDIRPATH/$BACKUPDIRNAME
cd $BACKUPDIRPATH/$BACKUPDIRNAME
echo -en "Enter the entire path of directory which you want to backup: "; read TOBEBACKUP
tar -czvf $BACKUPFILE $TOBEBACKUP
else
echo "File is already exists"
cd $BACKUPDIRPATH/$BACKUPDIRNAME
echo -en "Enter the entire path of directory which you want to backup: "; read TOBEBACKUP
tar -czvf $BACKUPFILE $TOBEBACKUP
fi
}
function remotebackup () {
function backupdirpath() {
if [ ! -e $BACKUPDIRPATH ]
then
echo "No such file or directory. Please give the correct path"
echo -en "Enter the path for backup directory: "; read BACKUPDIRPATH
backupdirpath
fi
}
echo -en "Enter the BACKUP Directory Name: "; read BACKUPDIRNAME
echo -en "Enter the BACKUP Directory Path: "; read BACKUPDIRPATH
backupdirpath
BACKUPFILE=scripts.backup.`date +%F`.tgz
if [ ! -e $BACKUPDIRPATH/$BACKUPDIRNAME ]
then
echo "Creating backup directory because it does not exist!"
mkdir $BACKUPDIRPATH/$BACKUPDIRNAME
cd $BACKUPDIRPATH/$BACKUPDIRNAME
echo -en "Enter the entire path of directory which you want to backup: "; read TOBEBACKUP
tar -czvf $BACKUPFILE $TOBEBACKUP
cd $BACKUPDIRPATH/$BACKUPDIRNAME
echo -en "Enter the Remote Backup Host: "; read REMOTEBACKUPHOST
scp $BACKUPFILE $REMOTEBACKUPHOST:
else
echo "File is already exists"
cd $BACKUPDIRPATH/$BACKUPDIRNAME
echo -en "Enter the entire path of directory which you want to backup: "; read TOBEBACKUP
tar -czvf $BACKUPFILE $TOBEBACKUP
cd $BACKUPDIRPATH/$BACKUPDIRNAME
echo -en "Enter the Remote Backup Host: "; read REMOTEBACKUPHOST
scp $BACKUPFILE $REMOTEBACKUPHOST:
if [ $? != 0 ]
then
echo "Problems Coping Backup File To Backup Host"
fi
fi
}
function localrestore () {
function backuppath () {
if [ ! -e $BACKUPPATH ]
then
echo "No such file or directory. Please give the correct PATH"
echo -en "Provide the BACKUP Path: "; read BACKUPPATH
backuppath
fi
}
echo -en "Provide the BACKUP Path: "; read BACKUPPATH
backuppath
cd $BACKUPPATH
function restorepath () {
if [ ! -e $BACKUPPATH/$RESTOREFILE ]
then
echo "No such file. Pleave provide the correct FILE: "
echo -en "Provide the BACKUP File which you want to Restore: "; read RESTOREFILE
restorepath
fi
}
echo -en "Provide the BACKUP File which you want to Restore: "; read RESTOREFILE
restorepath
tar -xzvf $RESTOREFILE
echo "Restore files are in $BACKUPPATH "
}
function remoterestore () {
function backuppath () {
if [ ! -e $BACKUPPATH ]
then
echo "No such file or directory. Please give the correct PATH"
echo -en "Provide the BACKUP Path: "; read BACKUPPATH
backuppath
fi
}
echo -en "Provide the BACKUP Path: "; read BACKUPPATH
backuppath
cd $BACKUPPATH
function restorepath () {
if [ ! -e $BACKUPPATH/$RESTOREFILE ]
then
echo "No such file. Pleave provide the correct FILE: "
echo -en "Provide the BACKUP File which you want to Restore: "; read RESTOREFILE
restorepath
fi
}
BACKUPFILE=scripts.backup.`date +%F`.tgz
echo -en "Enter the Remote Backup Host: "; read REMOTEBACKUPHOST
scp $BACKUPPATH $REMOTEBACKUPHOST:
echo -en "Provide the BACKUP File which you want to Restore: "; read RESTOREFILE
restorepath
tar -xzvf $RESTOREFILE
echo "Restore files are in $BACKUPPATH "
}
function packageverification () {
echo -en "Enter the Package: "; read Package
rpm -qa | grep -i $Package
if [ $? = 0 ]
then
echo "Package is Installed"
else
echo "Package is NOT Installed"
fi
}
function packageinstallation () {
echo -en "Enter the Package: "; read Package
rpm -qa | grep -i $Package
PACKAGEPATH2=`rpm -qa | grep -i $Package`
if [ $? = 0 ]
then
echo "Package is Installed"
else
echo "Package is NOT Installed"
echo -en "Do you want to INSTALL this package(yes/no): "; read check
if [ "$check" = "yes" ] || [ "$check" = "YES" ]
then
echo -en "Please Enter the Package Path: "; read Package_path
if [ -e $Package_path ]
then
echo "File exists"
echo "Installing.............."
rpm -ivh $Package_path/$Package*
echo "Package has been INSTALLED Successfully"
else
echo "File does not exist. Please Enter the correct path"
fi
elif [ "$check" = "no" ] || [ "$check" = "NO" ]
then
exit
fi
fi
}
function packageinstallation1 () {
echo -en "Enter the Package: "; read Package
rpm -qa | grep -i $Package
PACKAGEPATH2=`rpm -qa | grep -i $Package`
if [ $? = 0 ]
then
echo "Package is Installed"
echo -en "Provide the path of the package or type Quit to exit: "; read pathpackage1
if [ $pathpackage1 = "Quit" ]
then
exit
else
echo "Files from path cd `$pathpackage | ls -ltr`"
fi
echo -en "If you want to install another package then please give entire name or type Quit to exit: "; read anotherpackage
if [ $anotherpackage = "Quit" ]
then
exit
else
rpm -ivh $pathpackage1/$anotherpackage
fi
else
echo "Package is NOT Installed"
echo -en "Do you want to INSTALL this package(yes/no): "; read check
if [ "$check" = "yes" ] || [ "$check" = "YES" ]
then
echo -en "Please Enter the Package Path: "; read Package_path
if [ -e $Package_path ]
then
echo "File exists"
ls -la | grep -i $Package > tmp.txt
echo "Files found in the path are: `cat tmp.txt`"
LINES=`cat tmp.txt | wc -l`
if [ $LINES > 1 ]
then
echo -en "Please provide the package which you want to Install: "
read newpackage
rpm -ivh $Package_path/$newpackage
exit
fi
echo "Package has been INSTALLED Successfully"
else
echo "File does not exist. Please Enter the correct path"
fi
elif [ "$check" = "no" ] || [ "$check" = "NO" ]
then
exit
fi
fi
}
function packageuninstallation () {
echo -en "Enter the Package: "; read Package
PKRM=`rpm -qa | grep -i $Package`
if [ -e $PKRM ]
then
echo "Package is not available"
else
echo -en "Do you want to UNINSTALL this package(yes/no): "; read check
if [ "$check" = "yes" ] || [ "$check" = "YES" ]
then
rpm -e $PKRM
echo "Uninstalling........"
echo "Package has been Uninstalled"
else
exit
fi
fi
}
select var in $LIST
do
if [ $var = "Ping_Status" ]
then
pingstatus
elif [ $var = "Host_Identification" ]
then
hostidentification
elif [ $var = "SSH_Login" ]
then
sshsession
elif [ $var = "Login_Failure" ]
then
loginfailure
elif [ $var = "Service_Status" ]
then
servicestatus
elif [ $var = "Memory_Status" ]
then
memorystatus
elif [ $var = "Local_Backup" ]
then
localbackup
elif [ $var = "Remote_Backup" ]
then
remotebackup
elif [ $var = "Local_Restore" ]
then
localrestore
elif [ $var = "Remote_Restore" ]
then
remoterestore
elif [ $var = "Package_Verification" ]
then
packageverification
elif [ $var = "Multiple_Package_Installation" ]
then
packageinstallation
elif [ $var = "Single_Package_Installation" ]
then
packageinstallation1
elif [ $var = "Package_Uninstallation" ]
then
packageuninstallation
elif [ $var = "Quit" ]
then
exit
else
exit
fi
done
#END
No comments:
Post a Comment