LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   store command output in html table format (https://www.linuxquestions.org/questions/linux-software-2/store-command-output-in-html-table-format-4175598356/)

hruday 01-27-2017 04:49 AM

store command output in html table format
 
My shell script is as follows which logins into remote system through ssh and calculates cpu and ram usage.

Now I need to show the output in html and insert cpu and ram usage values in table like format

for example

-----------------------------------
HOST | CPU-USAGE% | RAM-USAGE% |
-----------------------------------
a.b.c.d | 4.0 | 30.4 |
-----------------------------------
1.2.3.4 | 6.0 | 48.0 |
-----------------------------------


#!/bin/bash

USERNAME=root

HOSTS="a.b.c.d 1.2.3.4"

CPU_USAGE=ps -A -o pcpu | tail -n+2 | paste -sd+ | bc;

RAM_USAGE=free -m | grep Mem | awk '{printf("Used :%.4f%\n",\$3/\$2*100)}'

echo "<HTML>" >> cpunram.txt
echo "<HEAD>" >> cpunram.txt
echo " <TITLE>" >> cpunram.txt
echo " SISTUDIO-HEALTH-STATUS" >> cpunram.txt
echo " </TITLE>" >> cpunram.txt
echo "</HEAD>" >> cpunram.txt
echo "" >> cpunram.txt

echo "<BODY>" >> cpunram.txt

for ip in $HOSTS
do

if [ "$ip" == "a.b.c.d" ] ; then

ssh -i /tmp/key1.pem ${USERNAME}@${ip} "${CPU_USAGE}" "${RAM_USAGE}" >> cpunram.txt

elif [ "$ip" == "1.2.3.4" ] ; then

ssh -i /tmp/key2.pem ${USERNAME}@${ip} "${CPU_USAGE}" "${RAM_USAGE}" >> cpunram.txt

else
echo "Wrong IP"

fi

done

echo "</BODY>" >> cpunram.txt
echo "</HTML>" >> cpunram.txt

hydrurga 01-27-2017 04:54 AM

What is your question?

hruday 01-27-2017 05:36 AM

Quote:

Originally Posted by hydrurga (Post 5660894)
What is your question?

I want output in mentioned html table format.

hydrurga 01-27-2017 05:39 AM

What does your output currently look like?

TenTenths 01-27-2017 05:49 AM

So you'll need:

Code:

<table>
<tr><th>HOST</th><th>CPU-USAGE%</th><th>RAM-USAGE%</th></tr>

And then for each row of output you'll need (where VAL1 etc. is replaced with your actual result values)
Code:

<tr><td>VAL1</td><td>VAL2</td><td>VAL3</td></tr>
And at the end:
Code:

</table>

hruday 01-27-2017 05:49 AM

5.2
Used: 3.80
4.3
Used: 4.71

hruday 01-27-2017 05:49 AM

Quote:

Originally Posted by hydrurga (Post 5660911)
What does your output currently look like?

5.2
Used: 3.80
4.3
Used: 4.71

hruday 01-27-2017 05:51 AM

Quote:

Originally Posted by TenTenths (Post 5660913)
So you'll need:

Code:

<table>
<tr><th>HOST</th><th>CPU-USAGE%</th><th>RAM-USAGE%</th></tr>

And then for each row of output you'll need (where VAL1 etc. is replaced with your actual result values)
Code:

<tr><td>VAL1</td><td>VAL2</td><td>VAL3</td></tr>
And at the end:
Code:

</table>

How do I insert this in between commands.?

As I have SSH command with parameters.

TenTenths 01-27-2017 05:57 AM

Quote:

Originally Posted by hruday (Post 5660916)
How do I insert this in between commands.?

As I have SSH command with parameters.

Run your commands one command at a time, with HTML tables the tags don't have to be on the same line.

Also you might want to name your output file .htm or .html rather than .txt

You might also want to consider not using >> for the <HTML> tag as this will append to the file rather than create a new one.


All times are GMT -5. The time now is 06:19 AM.