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)
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>";
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.
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>";
?>
Related
I tried many solutions but nothing it works :
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
I launched indivdually them but in all cases, Firebug returned : '<pre>'
So I tried this code founded on Stack Overflow :
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
But firebug returned nothing.
My python file begin with #!/usr/bin/env python and if I launch it on server that works !
Do you knwo how can I launch my python file from PHP file ?
chmod will return 0 on success and > 0 on error.
Make sure that the file is able to run by just executing it as the web user. When +x is properly set, you can execute it by just calling $ /path/to/your/file.py, the shebang in the first line in your script #!/usr/bin/env python should define the correct python based on your env.
You can test this by running:
$ /usr/bin/env python /path/to/your/file.py
So check your file permissions to check if the file is executable by the user that runs the php script.
Just to test, you can just print a few lines in your python file
#!/usr/bin/env python
print "test line 1"
print "test line 2"
Then if you have verified permissions and the correct use of python, you can do this in your php.
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
At first, Do you have enabled shell_exec/system/passthru commands in php.ini?
shell_exec("python /home/folder/python/mapfile_compress.py");
I think, it could be problem with your $PATH. Try something like: (use full path to python)
shell_exec("/usr/bin/python /home/folder/python/mapfile_compress.py");
shell_exec("/usr/local/bin/python /home/folder/python/mapfile_compress.py");
In my case that's work if I write this code :
$command = escapeshellcmd('python /path/to/your/file.py');
exec($command);
I have a shell script in Linux that performs SFTP to get some files. It works fine when I execute it from a terminal.
I am trying to call the script from PHP. It seems to work until the echo, and then it doesn't do anything.
The script and the PHP file are in the same folder.
This is the PHP code:
<?php
$comando = "sh ftpgesdoc.sh";
$result=exec($comando);
echo $result;
?>
And this is shell script. When I execute from the web, I can see the echo "ejecutando sftp", but nothing happens after this point.
#!/bin/sh
echo "ejecutando sftp"
folder="/aaa/bbb"
file="xxx.PDF"
sftp UserXX#nnn.nn.n.nn << EOF
cd $folder
get $file test.pdf
EOF
exec returns only the last line from the command output. If you want to capture entire output, use proc_open. See this answer, for instance.
you have to give the full path to file
and use this 2>&1 and know the error
try something like this
$comando = "sh pathTofile/location/ftpgesdoc.sh";
if(exec("$comando 2>&1", $output, $return_var))
{
print_r($output);
echo "<br>";
print_r($return_var);
}
I am trying to start a cd rip with cdparanoia -E 1 and want to process the progress bar.
The sample code does output content in browser but just after everything is finished. If its called in cli content is displayed directly.
$cmd = 'cdparanoia -e 1';
#ob_end_clean();
$proc = popen($cmd, 'r');
while (!feof($proc))
{
echo fread($proc, 4096).'<br>';
flush();
#ob_flush();
}
My env is debian with nginx 1.6.2 and php5-fpm 5.6.17.
I was wondering if it is even at all possible to execute an command such as "crontab -l" via webpage? This seems to be an permission issue.
$out = shell_exec('crontab -l');
echo '<pre>'.$out.'</pre>';
When this is executed via PHP, the value of $out is empty.
From PHP.net :
It is not possible to detect execution failures using this function.
exec() should be used when access to the program exit code is
required.
I suggest you use exec so that you can check if the command executed successfully. You should be able to output any data.
Something like:
exec('crontab -l 2>&1', $output, $return_code)
if ($return_code !== 0) {
//Returned Error
echo implode("\n", $output); // display output
} else {
//Returned OK
echo implode("\n", $output); // display output
}
Well this is what I ended up doing...
I added a a cron job to update a txt file with the outout of crontab -l every minute
crontab -l > /var/www/html/logs/cronJobs.txt
Set the permission of cronJobs.txt
Link the cronJobs.txt from the PHP page
Want to archive a folder using tar from PHP:
$result = shell_exec("tar cf $sourceFile $sourceFolder -C $source > /dev/null; echo $?");
var_dump($result);
Output:
string(2) "2 "
the > /dev/null; echo $? thing is for outputing the result code of a script under linux;
the -C $source - changes to the right folder before doing anything
This is really strange because when i run this from linux console, it works just fine - creates the archive, so it's not a permission issue.
Other sripts like "whoami" or "ls" work fine.
Any ideas what does it mean?
Maybe: shell_exec("/bin/bash tar ....")
Just for debugging purposes redirect stderr to stdout and use passthru() to display the complete output (possibly including error messages).
$cmd = sprintf("tar cf %s %s -C %s 2>&1",
escapeshellarg($sourceFile),
escapeshellarg($sourceFolder),
escapeshellarg($source)
);
echo '<pre>', htmlspecialchars($cmd), ": \n";
flush();
passthru($cmd, $code);
var_dump($code);
echo "</pre>";