Tuesday, 17 May 2016

Shell Scripts you will love to use

In this article, I will share a few of my shell scripts which will be helpful in automating repetitive infrastructure task. All Scripts are written and tested on RHEL 6.1 x86.
What the shell Script does and the agenda behind writing each script are also mentioned. The scripts have been named as per my requirement, feel free to rename and use it as per your environment’s needs.
SCRIPT#1 – I call it InitialConfig.sh
In my environment, I need to create VMs and decommission them every now and then. The Count of RHEL6 VMs is on the higher scale than any other Linux Flavors.
Once RHEL6 VM creation and OS Installation is done, we need to perform configuration changes which are by default common for all RHEL6 VMs.The task is very time consuming and repetitive.Hence, I thought of writing a shell Script which will automate the repetitive task and hence save time.
Note:Static IP Address needs to be SET manually.
In case of Dynamic Address [DHCP], No changes required in Network Settings
Basically, the Script performs the following:
1. Configures /etc/hosts file.
2. Configures /etc/sysconfig/network file.3. Sets Hostname.4. Disables GUI Firewall and iptables.5. Disables SELINUX.6. Updates Time Zone.7. Verifies Date and Time.8. Change Run level from five to three.9. Finally Reboot the System in one minute.
Now, follows the InitialConfig.sh script.
####################################
#Created By Arindam Mitra###########
####################################
 
#UPDATE HOST FILE - /etc/hosts
 
IP=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
read -p "Specify FQDN of the System [Example - RH601.linuxrocks.org] : " FQDN
read -p "Specify Hostname of the System [Example - RH601] : " HNAME
echo "#Edited By Arindam Mitra" >> /etc/hosts
echo $IP $FQDN $HNAME >> /etc/hosts
 
#UPDATE HOSTNAME AND CONFIGURATION FILE - /etc/sysconfig/network
cat /etc/sysconfig/network | grep NETWORKING > /tmp/hostname.txt
echo "#Edited By Arindam Mitra" >> /tmp/hostname.txt
echo HOSTNAME=$HNAME >> /tmp/hostname.txt
sed -i'' '2 s/^/#/' /etc/sysconfig/network
cat /tmp/hostname.txt > /etc/sysconfig/network
hostname $HNAME
rm -rf /tmp/hostname.txt
 
#SERVICE IPTABLES STOPPED NOW AND DURING BOOT TIME
 
service iptables stop > /dev/null
chkconfig iptables off
 
#DISABLE GUI BASED FIREWALL
cat /etc/sysconfig/system-config-firewall | grep service > /tmp/firewall.txt
echo "#Edited By Arindam Mitra" >> /tmp/firewall.txt
echo "--disabled" >> /tmp/firewall.txt
cat /tmp/firewall.txt > /etc/sysconfig/system-config-firewall
rm -rf /tmp/firewall.txt
 
#DISABLE SELINUX
sed -i'' '7 s/^/#/' /etc/sysconfig/selinux
echo "#Edited By Arindam Mitra" >> /etc/sysconfig/selinux
echo "SELINUX=disabled" >> /etc/sysconfig/selinux
 
#UPDATE TIMEZONE
 
cat /etc/sysconfig/clock | grep ZONE > /tmp/clock.txt
sed -i'' '1 s/^/#/' /tmp/clock.txt
echo "#Edited By Arindam Mitra" >> /tmp/clock.txt
echo "ZONE=\"Europe/Amsterdam\"" >> /tmp/clock.txt
cat /tmp/clock.txt > /etc/sysconfig/clock
rm -rf /tmp/clock.txt
 
#UPDATE RUNLEVEL FROM 5 to 3
sed -i'' '26 s/^/#/' /etc/inittab
echo "#Edited By Arindam Mitra" >> /etc/inittab
echo "id:3:initdefault:" >> /etc/inittab
 
#OUTPUT ON TERMINAL :-
 
echo "#############################"
echo "UPDATED HOSTS FILE [/etc/hosts] :-"
cat /etc/hosts
echo "#############################"
echo "UPDATED HOSTNAME :-"
hostname
echo "#############################"
echo "UPDATED HOSTNAME CONFIGURATION FILE :-"
cat /etc/sysconfig/network
echo "#############################"
echo "UPDATED IPTABLES STATUS :-"
echo "CURRENT STATUS :-"
service iptables status
echo -ne "\n"
echo "DURING BOOT TIME :-"
chkconfig --list | grep iptables
echo "#############################"
echo "GUI BASED FIREWALL DISABLED."
echo "Pls, verify using GUI :- System -> Administration -> Firewall"
echo "#############################"
echo "UPDATED SELINUX STATUS :-"
echo "*************************"
echo "AS PER CONFIGURATION :-"
cat /etc/sysconfig/selinux | grep SELINUX=
echo -ne "\n"
echo "AS PER SYSTEM :-"
getenforce
echo "*************************"
echo "SELINUX has been Disabled but Reboot is Required to bring the changes into effect..."
echo "#############################"
echo "UPDATED TIME ZONE :-"
cat /etc/sysconfig/clock | grep ZONE
echo "##############################"
echo "CURRENT DATE AND TIME :-"
date
echo "##############################"
echo "UPDATED RUNLEVEL STATUS"
echo "*************************"
echo "AS PER CONFIGURATION :-"
cat /etc/inittab | grep id:
echo -ne "\n"
echo "AS PER SYSTEM :-"
runlevel
echo "*************************"
echo "RUNLEVEL has been changed from 5 to 3 but Reboot is Required to bring the changes into effect..."
echo "##############################"
 
 
#REBOOT INITIATED AFTER ONE MINUTE 
shutdown -r +1 "Please save your work ASAP."
echo "##############################"
SCRIPT#2 – I call it ChangeRootPassword.sh
In our infrastructure environment, root user password is common for all Linux VMs and is not shared with anyone expect the Linux Administrator.
Once a year or every six months, the password of root user is changed for all Linux VMs. The task is repetitive and pretty boring. Hence, I wrote this one liner shell script which automated the process and saved time.
Basically, the Script performs the below:
1. Set New Password for Root user.
Now, follows the ChangeRootPassword.sh script.
#!/bin/bash
###################################
#Created By Arindam Mitra##########
###################################
 
echo "admin123" | passwd --stdin root
echo "Root Password Changed!!!"
SCRIPT#3 – I call it DNSConfig.sh
It was during our Domain Migration Activity when we had to change DNS Suffix and DNS IP Address in all Linux Servers.
At that point, I wrote this shell script to automate the task.
Basically, the Script performs the below:
1. Makes a copy the DNS Configuration file [/etc/resolv.conf] prior making changes.
2. Then, Updates the DNS Configuration file with New Suffixes and IP Addresses.
Now, follows the DNSConfig.sh script.
#!/bin/bash
####################################
#Created By Arindam Mitra###########
####################################
 
cp /etc/resolv.conf /etc/resolv.conf.orig
cat <<EOF > /etc/resolv.conf
search delhi.linuxrocks.org pune.linuxrocks.org
nameserver X.X.X.X
nameserver Y.Y.Y.Y
EOF
SCRIPT#4 – I call it ServerConfigDetails.sh
For Server Implementation, I happened to travel to various customer locations. One thing which I have faced and is common for all projects is that every time I had to pull Server Inventory details before I start with the actual configuration – How much Disk Space available, memory, Swap Space, CPU, Sockets, IP Address etc.
To make my life easy, I wrote this shell script, which pulls Complete Server Inventory details and displays the output on the terminal:
Basically, the Script performs the below:
1. It Pulls the below Inventory Details of Linux server and displays the output.
Hostname, Operating System, Architecture, Hypervisor, Manufacturer, Product Name, CPU Type, CPU Usage, CPU OP-MODE(s), No. of CPU, No. of CPU Sockets, CPU Speed, Maximum Memory, Used Memory, % Memory Used, Swap Space Details, Disk Space Details, IP Address, Subnet Mask and MAC Address.
Now, follows the ServerConfigDetails.sh script.
#!/bin/bash
####################################
#Created By Arindam Mitra###########
####################################
 
#HOSTNAME :-
HOSTN=`/bin/hostname`
#OPERATING SYSTEM :-
OS=`cat /etc/redhat-release`
#ARCHITECTURE :-
ARCH=`/bin/uname -p`
#HYPERVISOR TYPE
HTYPE=`dmidecode | grep -m 1 "Product Name" | cut -d ":" -f 2`
if [ "$HTYPE"  = " VMware Virtual Platform" ]
        then HTYPE="VMware Hypervisor"
else
        HTYPE="OTHERS"
fi
 
#HYPERVISOR MANUFACTURER :-
MANUFAC=`/usr/sbin/dmidecode --type system | grep Manufacturer | cut -d ":" -f2`
 
#PRODUCT NAME :-
PRODUCTNAME=`/usr/sbin/dmidecode | grep "Product Name: V" | cut -d ":" -f2 | awk '$1=$1'`
 
#CPU Info/Type
CPUI=`cat /proc/cpuinfo | grep "model name" | cut -d ":" -f2`
 
#CPU Usage
CPU=`top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'`
 
#CPU Details
CPUOM=`/usr/bin/lscpu | grep "CPU op" | cut -d ":" -f2 | awk '$1=$1'`
CPUC=`/usr/bin/lscpu | grep -i "CPU(s):" | cut -d ":" -f2 | awk '$1=$1'`
CPUS=`/usr/bin/lscpu | grep -i "CPU socket(s)" | cut -d ":" -f2 | awk '$1=$1'`
CPUMHZ=`/usr/bin/lscpu | grep -i "CPU MHz" | cut -d ":" -f2 | awk '$1=$1'`
 
#MEMORY DETAILS
MEMUSAGE=`top -n 1 -b | grep "Mem"`
MAXMEM=`echo $MEMUSAGE | cut -d" " -f2 | awk '{print substr($0,1,length($0)-1)}'`
USEDMEM=`echo $MEMUSAGE | cut -d" " -f4 | awk '{print substr($0,1,length($0)-1)}'`
USEDMEM1=`expr $USEDMEM \* 100`
PERCENTAGE=`expr $USEDMEM1 / $MAXMEM`%
 
#SWAP DETAILS
SWAPFS=`swapon -s | grep -vE '^Filename' | awk '{ printf $1}'`
SWAPS=`swapon -s | grep -vE '^Filename' | awk '{ printf $3}'`
SWAPU=`swapon -s | grep -vE '^Filename' | awk '{ printf $4}'`
SWAPP=`swapon -s | grep -vE '^Filename' | awk '{ printf $5}'`
 
#DISK
DISK=`df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ printf $5 " " $1" | "}'`
 
#NETWORK DETAILS
IP=`ifconfig eth0 | grep "inet addr" | cut -d ":" -f2 | awk '{printf $1}'`
SM=`ifconfig eth0 | grep "inet addr" | cut -d ":" -f4`
MAC=`cat /etc/sysconfig/network-scripts/ifcfg-eth[0123] | grep -i HWADDR | cut -d "=" -f2`
 
#OUTPUT :-
echo -ne "\n"
echo "###################SERVER-DETAILS######################"
echo "1.  HOSTNAME = $HOSTN "
echo "2.  OPERATING SYSTEM = $OS"
echo "3.  ARCHITECTURE = $ARCH"
echo "4.  HYPERVISOR = $HTYPE"
echo "5.  MANUFACTURER = $MANUFAC"
echo "6.  PRODUCT NAME = $PRODUCTNAME"
echo "7.  CPU TYPE = $CPUI"
echo "8.  CPU USAGE = $CPU"
echo "9.  CPU OP-MODE(s) = $CPUOM"
echo "10. NO. OF CPU = $CPUC"
echo "11. NO. OF CPU SOCKETS = $CPUS"
echo "12. CPU SPEED IN MHz = $CPUMHZ"
echo "13. MAXIMUM MEMORY = $MAXMEM"
echo "14. USED MEMORY = $USEDMEM"
echo "15. PERCENTAGE MEMORY USED = $PERCENTAGE"
echo "16. SWAP DETAILS :-"
echo "       a. File System = $SWAPFS"
echo "       b. Size = $SWAPS"
echo "       c. Used = $SWAPU"
echo "       d. Priority = $SWAPP"
echo "17. DISK DETAILS [% Usage, FileSystem] = $DISK"
echo "18. IP ADDRESS = $IP"
echo "19. SUBNET MASK = $SM"
echo "20. MAC ADDRESS = $MAC"
echo "###################SERVER-DETAILS######################"
echo -ne "\n"
 
Conclusion
This concludes the article. Hope you enjoyed while reading. Please do provide you valuable feedbacks. You can reach me at theja0473@mail.com theja0473@gmail.com for any queries or questions.

No comments: