In this article we are going to see how we can create a simple linux script that we can use to ping network nodes and print the results from HTML file. We will use shell as the scripting language.
We will achieve the printable ping results by specifying the name of the HTML file to output the results to, its structure and populating it with the results.
Using any text editor such as gedit or vi for coding, First lets indicate what linux interpreter should run our script, for our script we will use shell interpreter.
The code structure for our program is as follow:
#!/bin/sh
As indicated the results will go to an HTML file, we first remove any pingresult.html file that may be there by:
rm pingresult.html
We recreate pingresult.html and add the opening HTML tag by using redirection, that is sending the results of the command to a file, you redirect by ">> pingresult.html" after the command.
echo '<html>' >> pingresult.html
Now we format the heading of the report and the date that will be added later on, by centering and using heading 1 style.
echo '<center> <h1>' >> pingresult.html
Then the heading of the server we are pinging is added below, here we pinging Google.
echo 'Google Ping Results</br>' >> pingresult.html
Now lets add the date to script and make it italic in addition to the heading 1 style.
echo '<italic>' >> pingresult.html date >> pingresult.html
The text formating closing tags:
echo '</italic>' >> pingresult.html echo '</br>' >> pingresult.html echo '</h1> </center>' >> pingresult.html
We will ping the server 3 times, in linux you have to control the ping iteration as it it does not terminate automatically. We use the ping flag -c for count and 1 to indicate a single ping attempt, the results will be also be written to the pingresult.html
ping -c 1 www.google.com >> pingresult.html
echo '</br>' >> pingresult.html
ping -c 1 www.google.com >> pingresult.html
echo '</br>' >> pingresult.html
ping -c 1 www.google.com >> pingresult.html
echo '</br>' >> pingresult.html
echo '</html>' >> pingresult.html
Save the file with .sh extension, give it executable rights and run it on any linux box.

Figure1. The ping results on a browser.
The complete script is as follow:
#!/bin/sh
rm pingresult.html
echo '<html>' >> pingresult.html
echo '<center> <h1>' >> pingresult.html
echo 'Google Ping Results </br>' >> pingresult.html
echo '<italic>' >> pingresult.html
date >> pingresult.html
echo '</italic>' >> pingresult.html
echo '</br>' >> pingresult.html
echo '</h1> </center>' >> pingresult.html
ping -c 1 www.google.com >> pingresult.html
echo '</br>' >> pingresult.html
ping -c 1 www.google.com >> pingresult.html
echo '</br>' >> pingresult.html
ping -c 1 www.google.com >> pingresult.html
echo '</br>' >> pingresult.html
echo '</html>' >> pingresult.html

See the prices for this post in Mr.Bool Credits System below: