Bash Shell Script To Create A Rich, Colorful Server Health Report(in HTML)

As you know,  my previous shell script produces an HTML Server Health Report displaying all vital stats in a plain format & I thought its magical to have a shell script create an HTML report even though it was a rather simple looking report 😉 Then I wrote a web-app that helps you track how much money you have saved & came up with the idea to enhance my script to output a rich, vibrant, vividly colored HTML report that has interactive 3D charts & other colorful visual elements 🙂 Sneak a peek below!

You can grab the script from my GitHub gist page.


So, I modified my script & made it fetch more server data and display that data in different tables & panels. These tables & panels include : –

Directories that eat up most of your disk space.


Dynamic table showing the directories that take up most of your disk-space.

Top Memory consuming processes.


Dynamic table displaying Top Memory consuming processes.

Top CPU consuming processes.


Dynamic table displaying Top CPU consuming processes.

2 Speedometers displaying # of processes running & # of logged in users.


Speedometer 🙂

A bars system that displays the resources utilisation (RAM/CPU/Filesystem/INodes) & these bars change their color to red if threshold is crossed 🙂


Bars displaying vital system stats.

3D interactive pie-chart showing the break-up of filesystem utilisation.


3D chart with break-up of filesystem usage.

Create a Server Health Report (HTML) Using Shell Script

Shell scripts are insanely powerful & convenient. We all know it 😉 Much of the beauty in shell scripts lies in the way they can be used to automate many aspects of System Administration. As a SysAdmin, you might have been asked to prepare health-reports on a regular basis. Today, I wrote one such script that will generate an HTML health-report containing some vital system information. Lets see how it works 🙂

Lab Description : –

Ubuntu 14.04 Server. Environment : – Bash shell

Instructions : –

Download or clone my GitHub repository from below location.

Place the file anywhere you want. I prefer keeping it under my home-directory but you may keep it anywhere.

Make it executable (if not already).

You may either run/execute it manually or you may also put it in a CRON job. I have chosen to generate the report twice a day, but its entirely upto you 🙂

Video Tutorial : –

To see the script in action, watch the video below on my YouTube Channel.

Additional Notes : –

I have kept the script & report minimal since I wrote it today only. You may customize it further so as to suit your needs. Sky is the limit 😉

Reset Apache HTTP User Passwords Using Expect In Linux

There are times when we need to reset HTTP user passwords. One of the common examples is Subversion (that uses Apache web front-end) users. There can be very many users who want to have their passwords reset. This can be very boring & tedious 😉 So, below is how I simplified it by writing a shell script using expect 🙂 If you recall, I had used expect earlier as well for this & this. Please read the script carefully. Its pretty self-explanatory 🙂

#Subversion password reset script
#Author : Shashank Srivastava
while read user; do
/usr/bin/expect << EOF
#/etc/svn-users is a flat-file that stores users & their encrypted passwords.
spawn htpasswd -m /etc/svn-users $user
puts "$user added"
expect "?New password: \r"
send "PASSWORD\r"
expect "?Re-type new password: \r"
send "PASSWORD\r"
expect eof
#/home/shashank/svn-users.txt is a text-file that stores the user-names

So, here is the output of my script 🙂

[root@server tcs_admin]# sh scripts/
spawn htpasswd -m /etc/svn-users user1
user1 added
New password:
Re-type new password:
Updating password for user user1
spawn htpasswd -m /etc/svn-users user2
user2 added
New password:
Re-type new password:
Updating password for user user2

Simple Shell Script To Calculate Total Storage On a Linux Server

Lets face it. Linux doesn’t provide a very simple way to tell how many disks are attached to server. There is no simple way to know the total storage if you don’t have palimpsest tool installed, which is unlikely if you are accessing server through putty 😦

I was asked to check total storage available on the server & it was an overwhelming task since my server has 36 disks attached (of-course coming from SAN)!! So, I decided to write a shell script that would allow me to get all the details in one go 😉

Below is my script : –

DISKNUM=`fdisk -l | grep -e "^Disk /dev/sd" | wc -l`
while [ $i -le $DISKNUM ]
D=`fdisk -l | grep -e "^Disk /dev/sd" | awk '{print$3}' | cut -f1 -d, | head -$i | tail -1`
SUM=`python -c "print $DISK" | cut -f1 -d.`
let i=$i+1
echo "Total Disks Attached Are : $DISKNUM."
echo "Total Storage Attached is : $SUM GB or $TB TB."

Upon its execution, this is what I found as output.

[shashank@server ~]$ sudo sh
Total Disks Attached Are : 36.
Total Storage Attached is : 5411 GB or 5 TB.

Using Expect Command To Automate User Creation on RHEL 6

Many a time my job requires me to add a number of users on a number of servers. The task can soon become painful once the number increases 😦

Thankfully, we have an amazing utility called Expect. Below is how I used Expect to automate user creation task. 🙂 This is basically a small script with self-explanatory tokens. I am also trying to incorporate a for /while loop so as to fully automate this task. This script is ideal for multiple users with same password.

#!/usr/bin/expect -f
#The script uses 2 arguments. user_name & password. Checking this condition.
if {[llength $argv] != 2} {
puts "usage: expect ./ user_name Password"
exit 1
set user [lindex $argv 0] #set user variable. Using first argument as username.
set pass [lindex $argv 1] #set password variable. Using second argument as username.
spawn useradd $user
puts "$user added" #print a message
expect "?server \r"
spawn passwd $user
expect "?Changing password for user $user. \r"
send "$pass\r"
expect "?Retype new password: \r"
send "$pass\r"
expect eof
puts "Password for $user set."

Below is the output 🙂

[shashank@server scripts]$ sudo expect ./ test4 Password
spawn useradd test4
test4 added
spawn passwd test4
Changing password for user test4.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
Password for test4 set.

Bash Script To Check If A Device Is In A Specific Network | Cutting First Octet of IP Address

Had to configure a Print Server few week back. While the configurations was easy, its testing was way too cumbersome. Thanks to this trick that did the job 🙂

Now that the configuration was correct, my task was to configure many (62 to be precise) printers on my Print Server. A few printers were in a different network. So, the task was to first ping them & determine their location. Doing it manually would have been a pain (pinging 62 devices) 😦 So I chose to write a script to help me achieve it 😉

Below is that script.

while read line
ping_response=`ping -c1 $line|grep PING|cut -d"(" -f2|cut -d")" -f1 2>/dev/null`
ipAddressFirstOctet=`echo "$ping_response"|cut -d"." -f1`
re='^[0-9]+$' //Any output other than ping response. Happens when ping doesn't find that host.
if [[ $ipAddressFirstOctet =~ $re ]]
if [ "$ipAddressFirstOctet" -eq 10 ] //10 is first octet for our DMZ
echo "$line ($ping_response) is TCS Printer"
echo "$line ($ping_response) is CSC Printer"

Expect Command in Linux

I was trying to automate a few things here for office & the search took me to a utility called “Expect”. Expect can be used to automate a variety of tasks where you know what should be the output & hence it allows you to feed the correct input 🙂 Put all this in a chain & voila! You got the job done. Below is a very simple script that makes use of Expect. Make sure, you install expect package first.

yum install expect* -y
spawn ssh shashank@server
expect "shashank@$server's password: "
send "Password" #This is your real password, so make sure you don't give any rights to this script to anyone. Root is god, obviously ;)
expect "$ "
send "hostname\r"
expect "$ "
send "exit\r"

Below is the brief explanation of the script/Expect.

spawn – Executes the command. In this case, its establishing an SSH connection to my machine using my user-id.

expect – What output will you see when you try to SSH into a server? yes, it prompts for a password.

send – Sends the password (input) to the question (output).

expect – Since the password has been supplied, there will b a $ prompt (obviously a normal user’s). It means we have logged in with the user-id.

send – This time it sends the command that needs to be executed on that server. In this case, its just the hostname. \r means execute the command & return (press enter).

expect – We can expect to see a $ prompt again since enter has been pressed (\r).

send – Issues exit command to log out.

To run this script, issue :-

#expect /path_to_/