Mount remote file system using sshfs with a PHP script - php

I want my PHP script can mont a remote system with sshfs command.
But it doesn't seem to work, the folder has been created but the folder still empty after execution. I also tried with a user without SU and it was working fine.
mkdir ("/var/mont/remote/");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;

This script (test.sh) should work :
#!/usr/bin/env bash
php -r 'mkdir ("/var/mont/remote");
$cmd = "sshfs -o password_stdin -o allow_other enzo#192.168.0.29:/home/enzo/remote/ /var/mont/remote/ <<< 'MyRemotePassword'";
$output = nl2br(shell_exec($cmd));
echo $output;'
Run it as bash test.sh

Related

How to give a name to a command inside shell_exec()?

I am working in the Laravel framework where I am using shell_exec(). I am creating the string of command as in the following example.
$cmd = "php artisan serve:setup";
$resp = shell_exec($cmd);
How can I put a name to the above command? It shows in the system monitor as only "php"; I have multiple commands like this. I need to access the PID of that command and also the status of that command.
The command is running successfully, and in the system monitor, it shows the process name as "PHP".
I need process name with exec() or shell_exec() from PHP.
I have tried like this:
$cmd = "bash -c exec ServerCPP -a cd $path && php artisan serve:setup 2>&1";
$resp = shell_exec($cmd);
dd($resp);
It gives the error: "Could not open input file: artisan."
Your last attempt needs to be corrected:
cd first and quote the path in case the $path variable contains spaces
quotes around the command executed with bash -c
exec -a ServerCPP if you want to give the name ServerCPP to your process
$cmd = "cd '$path' && bash -c 'exec -a ServerCPP php artisan serve:setup' 2>&1";
$resp = shell_exec($cmd);
dd($resp);
The best idea would be to create a command.
php artisan make:command ServeSetup
Once the file is generated, set the name of your command in app/Console/Commands.
protected $signature = 'serve:setup';
Hope this helps.

use "wine" with php's shell_exec

I am currently trying to use a "wine" command from my PHP-Script.
If i execute this:
$shell = shell_exec("/usr/bin/wine --version");
All is working fine and WINE version is displayed in $shell.
But, if i try to make it like this:
$run = shell_exec("/usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");
Which results in:
$run = shell_exec("/usr/bin/wine /var/www/html/bin/tool.exe -m /var/www/html/bin/std.maps -a /var/www/html/bin/alias.file -n /var/www/html/files/1-59374-94700/upload/dump.bin -o /var/www/html/files/1-59374-94700/maps/definitions.list 2>&1");
I get the following output:
wine: chdir to /.wine : No such file or directory
What i am doing wrong? If i enter the command above directly to the shell, all is working fine. If i do it without /usr/bin/ in front of wine, the output's are the same.
Br, Chris
I haven't try it myself but I think it is because wine is run as different user with different environment settings. Try create .wine directory inside /var/www and make www-data user as owner of this directory and make /var/www/.wine HOME directory.
$run = shell_exec("HOME=/var/www/.wine /usr/bin/wine ".$workdir."/bin/tool.exe -m ".$workdir."/bin/std.maps -a ".$workdir."/bin/alias.file -n ".$workdir."/files/".$project_name."/upload/dump.bin -o ".$workdir."/files/".$project_name."/maps/definitions.list");

bash script does not work when called from PHP file?

I am trying to run a bash script from the web using php exec() method. The same bash script when ran from the terminal ./test.sh works fine executes all the command without any error. But when i try to run it from the command line it would absolutely not work. They both are in the same location. I have even give chmod 777 permission to that file.
Commnads like ls , pwd etc work but cf isn't working ?
test.sh
/usr/bin/cf login -a https://api.stage1.ng.bluemix.net -o jetmak#ca.blue.com -s dev -u jenk#ca.blue.com -p pass22
/usr/bin/cf api
/usr/bin/cf
executeshellfromphp.php
header('Content-Type: application/json');
$bluemixid = $_POST['bluemixid'];
$bluemixpwd = $_POST['bluemixpswd'];
$env = $_POST['env'];
$restart = $_POST['action'];
$result =shell_exec('sh /var/www/shellscriptphp/test.sh '.$env.' '.$restart.' '.$bluemixid.' '.$bluemixpwd.' ');
echo json_encode(array("result"=>$result));
exit();
Error:
FAILED
Config error: Error writing to manifest file:.cf/config.json
open .cf/config.json: no such file or directory

How to launch unoconv from php file

I want to launch the command "unoconv" from a script php.
$command = '/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf >/dev/null 2>/dev/null';
$rc = system( $command );
echo $rc;
The command return no result and the file is not created.
I think is a problem from access with www-data and unoconv.
When I'm launching the command in shell, the file is created.
Any idea?
You can add command unoconv to sudoers.
I do this in this way:
I create wrapper bash script in for example /usr/local/bin where I have command unoconv.
#!/bin/bash
if [ -z "$1" ]; then
echo "Must pass file";
exit 10;
fi
/usr/bin/unoconv -f pdf $1.rtf
after this I adding entry in /etc/sudoers.d:
www-data ALL=NOPASSWD: /usr/local/bin/unoconv.sh
And now you can call script in php:
exec('sudo /usr/local/bin/unoconv.sh '.$fileName);
Try to run
$output = `/usr/bin/unoconv --server localhost --port 2002 --format=pdf file.rtf`;
instead and see error messages.
For me works like this:
$cmd = "/usr/bin/unoconv -f docx files/thefile";
shell_exec($cmd);
of course you have to do this previously (if you lounch your php script from the web):
chown -R www-data:www-data files/
I have found a solution to this problem when running Apache. You have to create the home folder for the www-data user
sudo mkdir /home/www-data
sudo chown www-data /home/www-data
Lastly we will have to edit the home directory and default shell for the www-data user
sudo vim /etc/passwd
For the entry of www-data the last two strings have to be replaced respectively with
/home/www-data
/bin/bash
Simple as this
$output = shell_exec('/opt/libreoffice5.0/program/python unoconv -f rtf test.html');
Edit the path to suite your configuration.
It just works!
You may be running into an issue with LibreOffice, OpenOffice or soffice not being able to write to the current user's $HOME directory.
By running the command below I was able to identify the correct $HOME directory and see the error that was being generated.
$cmd = 'echo $HOME & unoconv -vvvv --format %s --output %s %s 2>/tmp/unoconv.debug.txt';
exec($cmd);
The verbose output of $cmd will be generated written to the file: /tmp/unoconv.debug.txt.
In my case the output was:
Verbosity set to level 5
DEBUG: Connection type: socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
DEBUG: Existing listener not found.
DEBUG: Launching our own listener using /usr/lib64/libreoffice/program/soffice.bin.
Failed to connect to /usr/lib64/libreoffice/program/soffice.bin (pid=32012) in 6 seconds.
Connector : couldn't connect to socket (Success)
Error: Unable to connect or start own listener. Aborting.
The command ran seemed to fine as root, and as sudo -u nobody. On seeing this output I realized there was an issue with the home directory.
Kudos to Dag Wieers for his help - I'm hoping this helps other unoconv devs with their debugging.

How to make php run a prolog program,The command works when i use on command line but not with php

I am trying to run a program on swi prolog through php. I am running on windows
My command are as follow
$cmd = "C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
exec( $cmd );
nothing happens when i try to run my php code it waits for while and returns to command prompt on windows/system32/exe
but when i execute the same command on cmd line i get the desired out put
C:\Program Files (x86)\pl\bin\swipl -f test1.pl -g test " halt;
i had been trying it for last two days, any help will be highly appreciated
Thanks in advance for all people who will give it a thought and help me
Try using double-backslashes (and including "halt" within the double-quotes):
$cmd = "C:\\Program Files (x86)\\pl\\bin\\swipl -f test1.pl -g test halt";
Program Files (x86) => progra~2
$cmd = 'C:\progra~2\swipl\bin\swipl.exe -s C:\prolog\web\base.pl -g test,halt';
it's work for me

Categories