Unix and Linux port control shell script with html output


You can use this script to check port connectivity for your servers. On this script, I  only checked  “22” and  “1521”  port which belongs to ssh and Oracle  Database. Please take a look the script and change it as you wish. Also, script ‘ll be creating an HTML file that you can send it to your e-mail.

If you create a user named  “usrcntrl” on the destination server, this script will be report memory usage for the HP-UX server to. Please do not forget to define ssh key-paring for “usrcntrl” user.

 

#!/bin/ksh
#NOW=$(date +"%m-%d-%Y-%H:%M")
NOW=$(date +"%d/%m %H:%M")
echo '<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;border-color:#aaa;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#333;background-color:#fff;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aaa;color:#fff;background-color:#f38630;}
.tg .tg-lvh0{background-color:#efefef;font-size:16px;text-align:center;vertical-align:top}
.tg .tg-lhw1{background-color:#32cb00;font-size:16px;text-align:center;vertical-align:top}
.tg .tg-cjeo{background-color:#fe0000;color:#efefef;text-align:center;vertical-align:top}
.tg .tg-qnmb{font-weight:bold;font-size:16px;text-align:center}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 456px">
<colgroup>
<col style="width: 138px">
<col style="width: 132px">
<col style="width: 200px">
<col style="width: 113px">
</colgroup>
<tr>
<th class="tg-qnmb">Date</th>
<th class="tg-qnmb">Hostname</th>
<th class="tg-qnmb">Control Type</th>
<th class="tg-qnmb">Control Result</th>
<th class="tg-qnmb">Memory Usage</th>
</tr>' > /unixadmin/port_control
for i in $(echo "TESTSERVER1=10.10.50.228
casesup1=10.10.50.227")
do
IP_H=`echo $i | cut -d '=' -f 1,1`
IP_I=`echo $i | cut -d '=' -f 2,2`
if nc -w 2 $IP_I 22 >/dev/null 2>&1
then
DEG_STATE_Mem=`ssh -l usrcntrl $IP_I "/usr/sbin/swapinfo -tam"|grep memory|awk '{print $5}'`
DEG_STATE=`echo "OS UP"`
echo "<tr>
<td class="tg-lhw1">$NOW</td>
<td class="tg-lhw1">$IP_H</td>
<td class="tg-lhw1">Operation System</td>
<td class="tg-lhw1">$DEG_STATE</td>
<td class="tg-lhw1">$DEG_STATE_Mem</td>
</tr>" >> /unixadmin/port_control

else
DEG_STATE_Mem=`echo "Unreacable`
DEG_STATE=`echo "OS Unreacable"`
echo "<tr>
<td class="tg-cjeo">$NOW</td>
<td class="tg-cjeo">$IP_H</td>
<td class="tg-cjeo">Operation System</td>
<td class="tg-cjeo">$DEG_STATE</td>
<td class="tg-cjeo">$DEG_STATE_Mem</td>
</tr>" >> /unixadmin/port_control
fi
done
for i in $(echo "TESTSERVER1=10.10.50.228
everest1=10.10.50.227")
do
IP_H=`echo $i | cut -d '=' -f 1,1`
IP_I=`echo $i | cut -d '=' -f 2,2`
if nc -w 1 $IP_I 1521 >/dev/null 2>&1
then
DEG_STATE=`echo "DB UP"`
echo "<tr>
<td class="tg-lhw1">$NOW</td>
<td class="tg-lhw1">$IP_H</td>
<td class="tg-lhw1">Operation System</td>
<td class="tg-lhw1">$DEG_STATE</td>
</tr>" >> /unixadmin/port_control
else
DEG_STATE=`echo "DB Unreacable"`
echo "<tr>
<td class="tg-cjeo">$NOW</td>
<td class="tg-cjeo">$IP_H</td>
<td class="tg-cjeo">Operation System</td>
<td class="tg-cjeo">$DEG_STATE</td>
</tr>" >> /unixadmin/port_control
fi
done
echo "</table>" >> /unixadmin/port_control
(
echo "To: abdurrahim.yildirim@casesup.com"
echo "Subject: Database | OS Port Control"
echo "Content-Type: text/html"
echo
echo "<html><b><font size='3'>OS&DB Port Control Results</b></html>"
echo "<html><b><font size='3'>*********************************</b></html>"
cat /unixadmin/port_control
echo
) | /usr/sbin/sendmail -t

I'm a IT Infrastructure and Operations Architect with extensive experience and administration skills and works for Turk Telekom. I provide hardware and software support for the IT Infrastructure and Operations tasks.

205 Total Posts
Follow Me