Archive for the ‘Shell Script’ Category


#!/bin/bash
#!/usr/bin/expect
IP=192.168.10.100
PASSWORD=root@123

echo -e "\n------------------------------------------------------------------------------------------"
echo -e "\t RHEL version"
echo "------------------------------------------------------------------------------------------"

expect -c "spawn ssh -qo StrictHostKeyChecking=no root@${IP}
 expect {
 \"(yes/no)?\"
 {
 send \"yes\"
 expect \"password: \"
 send \"${PASSWD}\r\"
 expect \"\\\\#\"
 send \"uname -r\r\"
 expect \"\\\\#\"
 send \"lsb_release -r\r\"
 expect \"\\\\#\"
 send \"logout\"
 }
 \"password: \"
 {
 send \"${PASSWD}\r\"
 expect \"\\\\#\"
 send \"uname -r\r\"
 expect \"lsb_release -r\\\\#\"
 send \"\r\"
 expect \"\\\\#\"
 send \"logout\"
 }
 \"# \"
 {
 expect \"\\\\#\"
 send \"uname -r\r\"
 expect \"\\\\#\"
 send \"lsb_release -r\r\"
 expect \"\\\\#\"
 send \"logout\"
 }
 }
 "
echo -e "\n ------------------------------------------------------------------------------------------\n"

#!/bin/bash
echo “Script to create password less between Linux Server”
echo “Enter Remote_username :”
read user

if [ $user != ‘root’ ]
then
echo “ssh passwordless connection only for full privilege user”
exit 1
fi

echo “Enter Remote_Server_IP :”
read ip

/usr/bin/ssh -o PasswordAuthentication=no $user@$ip /bin/true
if [ $? -eq 0 ]; then
echo Already passwordless connection between two systems
exit 1
else
echo ssh passwordless connection is not yet enabled between two system
fi

if [ -e /root/.ssh/id_rsa.pub ]
then
echo “Already ssh key generated”
else
echo ” Generating ssh key ”
ssh-keygen -t rsa
fi

ssh-copy-id $user@$ip
sleep 2

echo “Testing SSH password less connection”
ssh -q $user@$ip echo “test”
if [ $? -eq 0 ]; then
echo Passwordless enabled successfully and connection also tested
exit 1
else echo test connection have issue
fi


#!/bin/bash
echo “Script to verify password less between Linux Server”
echo “Enter Remote_username :”
read user

echo “Enter Remote_Server_IP :”
read ip

/usr/bin/ssh -o PasswordAuthentication=no $user@$ip /bin/true
if [ $? -eq 0 ]; then
echo Already passwordless connection between two systems
exit 1
else
echo SSH passwordless connection is not yet enabled between two system
fi


Script

#!/bin/bash
H=`hostname`
cpu=`nproc`
GB=1024
echo -e “—————————————————————————————————————————-”
echo -e “\t Hardware Resource and Model”
echo -e “—————————————————————————————————————————-”
echo -e “CPU Resources Allocated = $cpu core”
M=`dmidecode -t 17 | grep -w MB | awk ‘{ SUM += $2} END { print SUM }’`
res=$(echo “scale=3;$M/$GB” | bc)
echo -e “Memory Resoruces Allocated = $res GB”
P=`dmidecode -t processor | grep -i intel | grep -i version | head -1 | cut -d’:’ -f2`
echo -e “Processor Model = $P”
TH=`fdisk -l | grep -i sd | grep -i gb |wc -l`
echo -e “Total Number Disk connected = $TH”
TSH=`fdisk -l | grep -i sd | grep -i gb | awk ‘{ SUM += $3} END { print SUM }’`
dkres=$(echo “scale=3;$TSH/$GB” |bc)
echo -e “Total Size of all Disk = $dkres TB”
NN=`lspci | grep -i ethernet | cut -d ‘:’ -f3 | head -1`
echo -e “Network Interface card Model = $NN”
TN=`lspci | grep -i ethernet | wc -l`
echo -e “Total Number of NIC Connected = $TN”
NS=`ethtool eth0 | grep -i speed |awk ‘{print $2,$3}’`
echo -e “Network Interface Speed = $NS”
echo -e “—————————————————————————————————————————-”

Output:-
[root@VM ~]# ./test.sh
————————————————————————————-
Hardware Resource and Model
————————————————————————————-
CPU Resources Allocated = 16 core
Memory Resoruces Allocated = 32.000 GB
Processor Model = Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
Total Number Disk connected = 8
Total Size of all Disk = 7.891 TB
Network Interface card Model = VMware VMXNET3 Ethernet Controller (rev 01)
Total Number of NIC Connected = 1
Network Interface Speed = 10000Mb/s
————————————————————————————-


Scenario

For my production Environment i want take backup of daily basis under /backup/DB using incremental backup method.

Example:

[root@server1 DB]# pwd
/backup/DB
[root@server1 DB]# ls -lt
total 20
drwxr-xr-x 2 root root 4096 Dec  5 05:29 backup-29-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:27 backup-28-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:25 backup-27-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:22 backup-26-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:21 backup-25-01-2016

From above output i am taking backup daily basis . But after few days my File system got full to avoid this we can delete except recent DB backup. But this i have to do daily or when ever disk full i have take action. Avoid this i am writing small script and scheduling.

Script: clean-old-backup.sh

#!/bin/bash
clear
P=’/backup/DB’
echo “script to Clean up Database”
cd $P
echo -e “\n\tBefore Clean up”
echo “————————————————————”
ls -lt
echo “————————————————————”
ls -t | tail -n +4 | xargs rm -rf
echo -e “\n\tAfter Clean up”
echo “———————————————————–”
ls -lt
echo “———————————————————–”

Run #./clean-old-backup.sh

Output

script to Clean up Database

Before Clean up
————————————————————————–
total 24
drwxr-xr-x 2 root root 4096 Dec  5 05:40 backup-30-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:29 backup-29-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:27 backup-28-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:25 backup-27-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:22 backup-26-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:21 backup-25-01-2016
—————————————————————————

After Clean up
————————————————————————-
total 12
drwxr-xr-x 2 root root 4096 Dec  5 05:40 backup-30-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:29 backup-29-01-2016
drwxr-xr-x 2 root root 4096 Dec  5 05:27 backup-28-01-2016
————————————————————————

[root@server1 ~]# crontab -l
00      20      *       *       *       /bin/bash /root/clean-old-backup.sh


Scenario :

I want to create 10GB LVM partition….using fdisk -l get disk name “/dev/sdd”….. below script we have to pass 4 arguments like LUN name, Volume Group name,Logical Volume name and Mount Points…. first scan the disk and create LVM partition…

script :

[root@autel ~]# cat diskmanagr.sh
#!/bin/bash

echo -e ” \nScan and Detect newly connected SCSI LUN”
host=`ls -l /sys/class/scsi_host/ | grep -v total | awk ‘{print $9}’ | awk -F”host” ‘{print $2}’`
for i in $host
do
echo “Rescaning scsi host /sys/class/scsi_host/host$i”
echo “- – -” > /sys/class/scsi_host/host$i/scan
done
echo -e “\n All the SCSI LUN scanned Sucessfully…..”
lun=$1
vg=$2
lv=$3
mp=$4
mkdir -p $mp
echo -e “\n\n Partition creation is begin”
partx -av $lun
pvcreate $lun
echo -e “\nPhysical Volume $lun Created”
vgcreate $vg $lun
echo -e “\n Volume Group $vgCreated”
lvcreate -l 100%FREE -n $lv $vg
echo -e “\n Logical Volume $lv Created”
mkfs.ext4 /dev/$vg/$lv
echo “/dev/$vg/$lv      $mp     ext4    defaults        1 2 ” | cat >> /etc/fstab
mount -a
s=` df -h | grep /test | awk ‘{print $1}’`
echo -e “\nNewly created LUN $mp = $s ”
echo -e “Partition successfully created”

output :

[root@autel ~]# time ./diskmanagr.sh /dev/sdd new disk10 /autel

Scan and Detect newly connected SCSI LUN
Rescaning scsi host /sys/class/scsi_host/host0
Rescaning scsi host /sys/class/scsi_host/host1
Rescaning scsi host /sys/class/scsi_host/host2

All the SCSI LUN scanned Sucessfully…..

Partition creation is begin
device /dev/sdd: start 0 size 20971520
gpt: 0 slices
Physical volume “/dev/sdd” successfully created

Physical Volume /dev/sdd Created
Volume group “new” successfully created

Volume Group
Logical volume “disk10” created

Logical Voume disk10 Created
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2620416 blocks
131020 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Newly created LUN /autel = 5.0G
Partition successfully created

real    0m1.487s
user    0m0.034s
sys     0m0.873s
[root@autel ~]#


Script : scandisk.sh

#!/bin/bash

echo -e ” \nScan and Detect newly connected SCSI LUN\n”

host=`ls -l /sys/class/scsi_host/ | grep -v total | awk ‘{print $9}’ | awk -F “host” ‘{print $2}’`

for i in $host

do

echo “Rescaning scsi host /sys/class/scsi_host/host$i”

echo “- – -” > /sys/class/scsi_host/host$i/scan

done

echo -e “\n All the SCSI LUN scanned Successfully…..\n”

 

Output : 

[root@autel ~]# ./scandisk.sh

Scan and Detect newly connected SCSI LUN

Rescaning scsi host /sys/class/scsi_host/host0
Rescaning scsi host /sys/class/scsi_host/host1
Rescaning scsi host /sys/class/scsi_host/host2

All the SCSI LUN scanned Successfully…..

[root@autel ~]#