| Generate a table of hostnames |
This script accepts a comma-delimited file of hostnames
and outputs an HTML table. Yes, you can do this by exporting
from a spreadsheet, but this is faster, smaller and more
flexible:
The input file looks like this:
HOSTNAME,Architecture,OS,Description,Status,IP_Address
illusion,i386,Linux,RayTracing Engine,,192.168.93.1
tardis,i386,Linux,HTML Server,,192.168.93.2
deepthought,i386,FreeBSD,File/MP3 Server,,192.168.93.3
helios,Sparc,Solaris,Mathematics Tools,,192.168.93.4
The first line contains column headings.
The actual script is:
#!/usr/bin/awk -f
BEGIN { FS = ",";
print "<html>";
print "<head></head>";
print "<body>";
print "<center><h3>Updated Machine Listing</h3></center>";
print "<center>";
print strftime("Updated: %A, %B %d, %Y, %R <br>");
print "<table>";
print "<table BORDER=1>"; }
$0 && NR==1{print "<tr BGCOLOR=\"#A6A6A6\">";
print " <td>", $1, "</td>";
print " <td>", $2, "</td>";
print " <td>", $3, "</td>";
print " <td>", $4, "</td>";
print " <td>", $5, "</td>";
print " <td>", $6, "</td>";
print "</tr>";
}
$0 && NR>1 { color=((NR%2)==0) ? "#E6E6E6" : "#FFFFFF";
ping_command = "ping -c 2 -q " $1 ">/dev/null"
status=(system(ping_command)) ? "INACTIVE" : "ACTIVE";
printf ("<tr BGCOLOR=%s>\n", color);
print " <td><a href=\"telnet://"$1"\">", $1, "</a></td>";
print " <td>", $2, "</td>";
print " <td>", $3, "</td>";
print " <td>", $4, "</td>";
print " <td>", status, "</td>";
print " <td><a href=\"telnet://"$6"\">", $6, "</a></td>";
print "</tr>";
}
END { print "</table>";
print "</center>";
print "</body>";
print "</html>"}
chmod +x the script, then run it with: ./scriptname inputfile.
You'll get a table similar to the following:
Updated Machine Listing
Updated: Wednesday, March 07, 2001, 21:20
Sometimes you may need to keep a log of connections. If
webalizer is
overkill, you can try something like this:
#!/bin/bash
TARGET=216.227.80.37
STATUS=`ping -c 2 -q $TARGEET 2>/dev/null`
CODE=$?
NOW=`date`
if [ $CODE -gt 0 ]; then
echo $NOW DOWN >> ~/temp/status
else
echo $NOW UP >> ~/temp/status
fi
These scripts are what I use to backup my machines to a central backup
server with a tape robot.