Results 1 to 5 of 5
Hi all,
I have quickly crafted a small ping script and published with apache for internal troubleshooting. It works ok, but i really wanted realtime line by line feedback for ...
- 07-22-2011 #1Just Joined!
- Join Date
- Nov 2007
- Posts
- 17
BASH CGI - pinger - realtime output
Hi all,
I have quickly crafted a small ping script and published with apache for internal troubleshooting. It works ok, but i really wanted realtime line by line feedback for the ping results, but it all comes at once.
Could anyone suggest how I can get said realtime style output?
Thanks!
#!/bin/sh
echo Content-type: text/html
echo ""
if [[ "$QUERY_STRING" =~ "pingntrace" ]]
then
if [[ "$QUERY_STRING" =~ "&pingntrace" ]]
then
echo "Sh1t detected. Quitting"
exit 1
fi
ip=`echo "$QUERY_STRING"|cut -d "&" -f1|cut -d "=" -f2`
cleanip=${ip//[^a-zA-Z0-9.]/}
cleanip=`echo -n $cleanip | tr A-Z a-z`
echo "<h3>Pinging $cleanip...</h3>"
ping -c 4 "$cleanip" 2>&1|sed 's/$/<br>/g'
echo "<hr>"
echo "<p>"
echo "<h3>Tracing route to $cleanip...</h3>"
traceroute -n "$cleanip" 2>&1|sed 's/$/<br>/g'
echo "<hr>"
fi
echo "<form method=get action=\"pinger\">"
echo "<input type=text name=pingntrace>"
echo "<input type=submit value=\"Ping & Trace\"><p>"
echo "</form>"
- 07-22-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
Try using the pre HTML tag, e.g.:
Code:#!/bin/sh echo Content-type: text/html echo "" if [[ "$QUERY_STRING" =~ "pingntrace" ]] then if [[ "$QUERY_STRING" =~ "&pingntrace" ]] then echo "Sh1t detected. Quitting" exit 1 fi ip=`echo "$QUERY_STRING"|cut -d "&" -f1|cut -d "=" -f2` cleanip=${ip//[^a-zA-Z0-9.]/} cleanip=`echo -n $cleanip | tr A-Z a-z` echo "<h3>Pinging $cleanip...</h3>" printf "<pre>\n" ping -c 4 "$cleanip" printf "</pre>\n" #ping -c 4 "$cleanip" 2>&1|sed 's/$/<br>/g' echo "<hr>" echo "<p>" echo "<h3>Tracing route to $cleanip...</h3>" printf "<pre>\n" traceroute -n "$cleanip" printf "</pre>\n" #traceroute -n "$cleanip" 2>&1|sed 's/$/<br>/g' echo "<hr>" fi echo "<form method=get action=\"pinger\">" echo "<input type=text name=pingntrace>" echo "<input type=submit value=\"Ping & Trace\"><p>" echo "</form>"
- 07-22-2011 #3Just Joined!
- Join Date
- Nov 2007
- Posts
- 17
genius! that is more or less perfect!! THANK YOU!!!
....errr... why the hell does this work!?!?
and how did you come across it?
thanks again.
- 07-22-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
actually, i think it has more to do with print/printf versus the pre tag. maybe the print is buffering and the printf is not-buffering? Anyway, the pre tag sends text input as-is, so maybe it is a combination of things, not sure.
I use the pre tag a lot when I'm doing CGI stuff and want to send the output of system commands to the browser "read-time". But I use Perl, not Bash, for CGI and I'm sure to set buffer flushing on ($| = 1) so that stuff printed gets sent to the screen immediately. That's why I think it has to do with print/printf. Just a guess.
- 07-22-2011 #5Just Joined!
- Join Date
- Nov 2007
- Posts
- 17
I think perhaps it is sed that is doing the buffering? i did attempt the unbuffer command but i dont think that helps once i hit the sed through a pipe.
i'll have a play with echo, print, printf, sed, unbuffer e.t.c and post if anything interesting comes up.
THANKS AGAIN!!


Reply With Quote
