2

I made a script in bash for getting pings into a file.

#!/bin/sh
echo "Starting script"
echo "Working.."
while true; do
    DATE=$(date)
    PING=$(ping -c 1 google.pl | tail -1| awk '{print $4}' | cut -d '/' -f 2)
    echo "$DATE Ping: $PING" >> logs/ping.txt
    sleep 5000
done

But due to lack of free space i changed echo "$DATE Ping: $PING" >> logs/ping.txt to just echo "$DATE Ping: $PING" to recive every line in cmd, and it worked

But still the main idea is to run the scipt through the web browser and display its output. (i can run it tho but i have no idea how to show echo output in a browser)

PersianGulf
  • 2,845
  • 6
  • 47
  • 67
Grafitter
  • 78
  • 1
  • 7

3 Answers3

3

You can call the bash script from php using:

exec('myscript.sh');

And then open the ping.txt using:

$myFile = "ping.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

Without text file:

$ping = shell_exec('myscript.sh');
echo "<pre>$ping</pre>";
user986959
  • 620
  • 2
  • 7
  • 22
1

With a bit of ajax and using Net_Ping you could have a page that updates in near-realtime. Alternatively use shell_exec to run ping from inside your php and echo the output returned from it.

kguest
  • 3,804
  • 3
  • 29
  • 31
0

If you need to execute your bash script from PHP and display the output in a browser, then just use the shell_exec() PHP function

Example from php.net:

<?php
$output = shell_exec('/path/script-name-here');
echo "<pre>$output</pre>";
?>
2MAS
  • 120
  • 6
  • Output will be shown only when script will stop and I need realtime preview – Grafitter Feb 26 '14 at 14:11
  • Typically a webserver will always server the content from start to finish. It isnt really designed to keep a connection open and keep sending your information. So, along the lines of what kguest wrote; you will need to make multiple asynchronous requests with javascript, and display the results in the browser as each response comes back in. – 2MAS Feb 26 '14 at 14:23