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.
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.
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/hostsIP=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')read -p "Specify FQDN of the System [Example - RH601.linuxrocks.org] : " FQDNread -p "Specify Hostname of the System [Example - RH601] : " HNAMEecho "#Edited By Arindam Mitra" >> /etc/hostsecho $IP $FQDN $HNAME >> /etc/hosts#UPDATE HOSTNAME AND CONFIGURATION FILE - /etc/sysconfig/networkcat /etc/sysconfig/network | grep NETWORKING > /tmp/hostname.txtecho "#Edited By Arindam Mitra" >> /tmp/hostname.txtecho HOSTNAME=$HNAME >> /tmp/hostname.txtsed -i'' '2 s/^/#/' /etc/sysconfig/networkcat /tmp/hostname.txt > /etc/sysconfig/networkhostname $HNAMErm -rf /tmp/hostname.txt#SERVICE IPTABLES STOPPED NOW AND DURING BOOT TIMEservice iptables stop > /dev/nullchkconfig iptables off#DISABLE GUI BASED FIREWALLcat /etc/sysconfig/system-config-firewall | grep service > /tmp/firewall.txtecho "#Edited By Arindam Mitra" >> /tmp/firewall.txtecho "--disabled" >> /tmp/firewall.txtcat /tmp/firewall.txt > /etc/sysconfig/system-config-firewallrm -rf /tmp/firewall.txt#DISABLE SELINUXsed -i'' '7 s/^/#/' /etc/sysconfig/selinuxecho "#Edited By Arindam Mitra" >> /etc/sysconfig/selinuxecho "SELINUX=disabled" >> /etc/sysconfig/selinux#UPDATE TIMEZONEcat /etc/sysconfig/clock | grep ZONE > /tmp/clock.txtsed -i'' '1 s/^/#/' /tmp/clock.txtecho "#Edited By Arindam Mitra" >> /tmp/clock.txtecho "ZONE=\"Europe/Amsterdam\"" >> /tmp/clock.txtcat /tmp/clock.txt > /etc/sysconfig/clockrm -rf /tmp/clock.txt#UPDATE RUNLEVEL FROM 5 to 3sed -i'' '26 s/^/#/' /etc/inittabecho "#Edited By Arindam Mitra" >> /etc/inittabecho "id:3:initdefault:" >> /etc/inittab#OUTPUT ON TERMINAL :-echo "#############################"echo "UPDATED HOSTS FILE [/etc/hosts] :-"cat /etc/hostsecho "#############################"echo "UPDATED HOSTNAME :-"hostnameecho "#############################"echo "UPDATED HOSTNAME CONFIGURATION FILE :-"cat /etc/sysconfig/networkecho "#############################"echo "UPDATED IPTABLES STATUS :-"echo "CURRENT STATUS :-"service iptables statusecho -ne "\n"echo "DURING BOOT TIME :-"chkconfig --list | grep iptablesecho "#############################"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 :-"getenforceecho "*************************"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 ZONEecho "##############################"echo "CURRENT DATE AND TIME :-"dateecho "##############################"echo "UPDATED RUNLEVEL STATUS"echo "*************************"echo "AS PER CONFIGURATION :-"cat /etc/inittab | grep id:echo -ne "\n"echo "AS PER SYSTEM :-"runlevelecho "*************************"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 rootecho "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.
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.
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.origcat <<EOF > /etc/resolv.confsearch delhi.linuxrocks.org pune.linuxrocks.orgnameserver X.X.X.Xnameserver Y.Y.Y.YEOF |
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 TYPEHTYPE=`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/TypeCPUI=`cat /proc/cpuinfo | grep "model name" | cut -d ":" -f2`#CPU UsageCPU=`top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}'`#CPU DetailsCPUOM=`/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 DETAILSMEMUSAGE=`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 DETAILSSWAPFS=`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 DETAILSIP=`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:
Post a Comment