LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-30-2017, 09:36 AM   #1
hruday
Member
 
Registered: Jun 2015
Posts: 88

Rep: Reputation: Disabled
unable to insert command output in html table format


I've a bash script written to insert ping statistics into html table.


#!/bin/bash

PINGGOOGLE=echo -e "<H1>Ping Status</H1>"; echo "<table border="1">" ; ping 8.8.8.8 -c 3 | grep ping -A 2; echo "</table>"

echo '<html>' >> pinghost.txt

echo '<body>' >> pinghost.txt

ssh root@192.168.16.130 "${PINGGOOGLE}" >> pinghost.txt

echo '</body>' >> pinghost.txt

echo '</html>' >> pinghost.txt

I want to put output of "ping 8.8.8.8 -c 3 | grep ping -A 2" in a table. What is wrong in my script?
 
Old 01-30-2017, 09:56 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
There are three mistakes.

One is [code] [/code] tags are missing to make the script more readable.

The second is this line, which due to the lack of quoting turns out to be four different lines.

Quote:
Originally Posted by hruday View Post
PINGGOOGLE=echo -e "<H1>Ping Status</H1>"; echo "<table border="1">" ; ping 8.8.8.8 -c 3 | grep ping -A 2; echo "</table>"
It could be written like this:

Code:
PINGGOOGLE='echo -e "<H1>Ping Status</H1>"; echo "<table border="1">" ; ping 8.8.8.8 -c 3 | grep ping -A 2; echo "</table>"'
Notice the single quotes However, only the ping needs to be run on the remote system. However, that still does not add cell elements to the table. Maybe you would prefer <pre> element instead of a table?

Lastly, and the most important mistake, is that you are logging into the remote machine as root. Remote root access should be turned off. It is certainly not needed for ping and can be worked around for other cases.
 
Old 01-30-2017, 09:58 AM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Presumably this is being run as a cgi by your web server as the user "apache" or "nano" or something. Have you tried running it under the same environment by hand? Is the script mode set to executable? Is it in the CGI directory?

Also, note that the first line output by the script should be: "Content-type: text/html".
Also HTTP line endings should be CR LF, not just LF.
 
Old 01-30-2017, 10:04 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by hruday View Post
I want to put output of "ping 8.8.8.8 -c 3 | grep ping -A 2" in a table.
Also, can you paste some manually marked up HTML for how you want that output to look?
 
Old 01-30-2017, 10:05 AM   #5
hruday
Member
 
Registered: Jun 2015
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
There are three mistakes.

One is [code] [/code] tags are missing to make the script more readable.

The second is this line, which due to the lack of quoting turns out to be four different lines.



It could be written like this:

Code:
PINGGOOGLE='echo -e "<H1>Ping Status</H1>"; echo "<table border="1">" ; ping 8.8.8.8 -c 3 | grep ping -A 2; echo "</table>"'
Notice the single quotes However, only the ping needs to be run on the remote system. However, that still does not add cell elements to the table. Maybe you would prefer <pre> element instead of a table?

Lastly, and the most important mistake, is that you are logging into the remote machine as root. Remote root access should be turned off. It is certainly not needed for ping and can be worked around for other cases.
Hi thanks for your reply. single quotes didnt work.

Cant we use echo itself.?
 
Old 01-30-2017, 10:07 AM   #6
hruday
Member
 
Registered: Jun 2015
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hruday View Post
Hi thanks for your reply. single quotes didnt work.

Cant we use echo itself.?
however command is working when i execute it in cli. It doesnt work when i assign it to a variable.
 
Old 01-30-2017, 10:10 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by hruday View Post
however command is working when i execute it in cli. It doesnt work when i assign it to a variable.
Then it needs to have the quotes escaped, and be careful of the dollar signs which also need to be escaped.

Edit: I would do the echos on the local machine and only run ping on the remote.

Code:
ssh user@192.168.16.130 "ping -c 3 -w 1 -q | sed '/ping statistics/,$!d'"

Last edited by Turbocapitalist; 01-30-2017 at 10:16 AM.
 
Old 01-30-2017, 10:20 AM   #8
hruday
Member
 
Registered: Jun 2015
Posts: 88

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Then it needs to have the quotes escaped, and be careful of the dollar signs which also need to be escaped.

Edit: I would do the echos on the local machine and only run ping on the remote.

Code:
ssh user@192.168.16.130 "ping -c 3 -w 1 -q | sed '/ping statistics/,$!d'"
if i try escaping quotes it shows the same value which i assigned doesnt excute.

PINGGOOGLE=" echo -e "<H1>Ping Status</H1>"; echo "<table border="1">" ; ping 8.8.8.8 -c 3 | grep ping -A 2; echo "</table>" "

output: echo -e "<H1>Ping Status</H1>"; echo "<table border=1>" ; ping 8.8.8.8 -c 3 | grep ping -A 2; echo "</table>"
 
Old 01-30-2017, 10:33 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Sorry. Copy-past error above.

Code:
ssh user@192.168.16.130 "ping -c 3 -w 1 -q 8.8.8.8" | sed '/ping statistics/,$!d'

You need only ping to run on the remote machine. Everything else can be local.
 
Old 01-30-2017, 10:41 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
To elaborate on Turbocapitalist post you can
Code:
pinggoogle=$(ssh user@192.168.16.130 "ping -c 3 -w 1 -q 8.8.8.8" | sed '/ping statistics/,$!d')
You can then insert $pinggoogle variable into the desired echo statement.

Last edited by michaelk; 01-30-2017 at 10:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
store command output in html table format hruday Linux - Software 8 01-27-2017 05:57 AM
inserting command output in specific html table aristosv Linux - Software 1 04-06-2016 03:19 PM
unable to insert data into the table vinaytp Linux - Newbie 2 04-27-2009 01:44 AM
Invalid module format. Unable to insert tg3.ko!! Please help ravichandran_swamy Linux - Newbie 4 01-29-2009 06:35 AM
i need rpm output in table format Uday123 Fedora 1 12-17-2005 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration