Monday, 6 October 2014

Shell Script : Bash Shell CGI

Shell script with cgi allows you to run linux commands/scripts from web. It can be used to build a monitoring tool for your linux machines/servers or managing your servers from web.

For this you should have cgi integrated with your web service nginx/apache and the user should  have access the cgi document root and  permission to run cgi script.

Here I'm giving an example how you can have some system information by running linux commands/shell script code -


#!/bin/bash

echo "Content-type: text/html"
echo ""
echo "<html><body><head><title>Bash Shell CGI"
echo '</title></head><body bgcolor="#3EA99F">'

### Getting information into veriables

load=`uptime | awk '{print $8,$9,$10}'`

Date=`date +'%Y-%m-%d'`


#### Load Test

### Getting current load avg integer value


la=`uptime | awk '{print $8}' | awk -F '.' '{print $1}'`

## Doing a test

if [ $la -gt 10 ]
then
stat="Critical"
elif [ $la -le 10 ]
then
stat="OK"
fi

####### Printing the information


echo "<h1><font color="blue"><u>System Information</u></font></h1><br><br>"
echo "<font color="black"><h3>Hostname : <font color="white">`hostname`</font></h2></font><br>"
echo "<font color="black"><h3>Load Average : <font color="white">$load - $stat</font></h2></font><br>"
echo "<font color="black"><h3>Date : <font color="white">$Date</font></h2></font><br>"
echo "</body></html>"
 

##### 


Put this file to your cgi-bin directory and run in browser using -


http://your-domain/cgi-bin/filename.cgi


Output : 




Thanks...! If you have any query or suggestions, please comment.
  

No comments:

Post a Comment