How to run Git Bash commands using PHP - php

Hi I have the following method which when triggered should run a command in Git Bash.
function convert($tmpName, $fileName, $fileSize, $fileType){
}
The command will be something like this:
pyang -f yin -o H:\\YangModels\\yin\\ietf-inet-types.yin ietf-inet-types.yang
I was looking at shell commands here but don't know if this relates to Git Bash or not.
Just looking for a way to run commands in Git Bash when a PHP method runs, thanks.
The command has to be run through Git Bash it will not work through the normal command line or Windows shell.
Edit: Been looking into it more and found a command that could be similar to what I need. The user seems to be trying to run his command through cygwin, trying to specify mine to target git bash but haven't figured it out yet.
$result = shell_exec('C:/cygwin/bin/bash.exe /c --login -i git');

exec or shell_exec can run any command you would normally run via command line e.g.
shell_exec('cd /var/www && /path/to/git pull origin master');
I'm not sure exactly how your code is formed but it might be something like:
function convert($tmpName, $fileName, $fileSize, $fileType){
$output = shell_exec('pyang -f '.$fileType.' -o '.$tmpName.' '.$fileName);
}

Related

How to exec a command installed with `npm` command in PHP using `exec()`?

In PHP running on Ubuntu, I can run exec('npm -v') and the output is good,
but I can't run exec('gitbook xxxx').
gitbook is a npm package I installed by
npm install gitbook -g
I can run gitbook xxxx in the Ubuntu terminal, how can I run it from my PHP code?
If you run php by nginx or apache (for example, visit url example.com/index.php), sometime you need to export the PATH
exec("export PATH=/usr/local/bin && gitbook build);
after I added export PATH, everything works fine.
I tried once like this on UNIX-based OS:
You can run shell commands via the exec() function:
// make an php file to execute shell script
exec("node yourscript.js &", $output);
Well output here become array of each line of output along with process id. You can kill process by processid also.
exec("kill " . $processid);
This how I was did. Other then this you can use node supervisor. Hope it will help you. Try your also with node command.

How to run Docker from php?

I am a beginner in using Docker, and I am trying to run docker from php, specifically I need to run openface code. I used the command lines provided here https://cmusatyalab.github.io/openface/setup/ to make sure that docker is running on my pc correclty and it works. but now I need to call it from PHP, I wrote the same commands in batch file as follows
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash
cd /root/openface
./demos/compare.py images/examples/{lennon*,clapton*}
pause
and tried to execute it in php by calling
echo shell_exec ("test.bat");
but when I run the batch file directly, only the first line is executed. as it seems the next command is not being executed inside the docker container.
how can I make all commands execute?
any help will be very much appreciated,
thank you
The problem is the first bash won't exit before you exit it and the rest of the commands are interpreted by host bash.
What you need is that your work is done and then you get inside a bash (inside the container)
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash -c "cd /root/openface && ./demos/compare.py images/examples/{lennon*,clapton*} && exec bash"
First "bash -c" is to execute the commands and last command exec bash override the main bash and gives you a shell

exec () php, sh script runs partially

sorry for bad english..
i have php file like this:
<?php
exec(`sh /tmp/script.sh`);
echo "Work!";
?>
and this is the script:
#!/bin/bash
url="http://someweb.com/get.php?user=user&pass=pass";
wget -O /tmp/file.txt $url
sed -i 's/#Test_file/Ok_Test_file/' /tmp/file.txt
cp /tmp/file.txt /var/www/_client/personale/file.txt
Now when load file.php to the browser, the script works ,but only commands
wget and sed are performed , except cp which doesn't work..does not copy the file!
If i run the script to terminal manually (Debian 8) all cmd are executed...
Where is the problem?
Thanks.
Joele
PHP likely does not have permission to execute the command. Try using sudo to execute the command.

wget not working when called from exec() or shell_exec()

I am trying to integrate a wget command I have written into a php script. The command recursively downloads every html/php file on a website (which is required functionality that I haven't found in file_get_contents()). I have tested the wget command in a terminal window, but when executing it using either exec() or shell_exec() nothing happens. I don't get any errors, or warnings.
Here is the command in question,
wget --recursive -m --domains oooff.com --page-requisites --html-extension --convert-links -R gif,jpg,pdf http://www.oooff.com/
I have tried simple wget commands (not as many parameters) from exec(), and shell_exec(), but they also don't work.
If wget isn't an option, I am open to using some other method of downloading a website in it's entirety.
My code that I have now is,
exec("wget google.com", $array);
Then when printing the array it is empty
I had to specify a path to wget. New command:
exec("/usr/local/bin/wget google.com", $array);
invoke wget with proper options
-q to remove it s information output
-O - to output the request response on stdout
php -r 'exec("wget -q -O - google.com", $array);var_dump($array);'
In our case wget didn't have enough permission to save wget'ed file in current dir. Solution:
$dir = __DIR__.'/777folder/';
exec("/usr/bin/wget -P ".$dir." --other-options");
where
-P, --directory-prefix=PREFIX save files to PREFIX/...
ps. we also added /usr/bin found with whereis wget but in our system it works fine without it

php sudo shell_exec command runs from command line, but not from browser

I am running lighttpd with php-fpm. I have added the http user to my sudoers, as a nopasswd sudoer. I have the following script:
<?php
echo shell_exec("sudo sh -c 'echo \"wtf3\" > /etc/openvpn/auth.txt'");
?>
It runs fine when I do:
sudo -u http php testsudo.php
but fails when run through the browser.
shell_exec("whoami");
confirms that I am user http when in browser.
What gives?
Would it be out of the question to make a shell script that would run your command?
#!/bin/sh
sudo whoami
in the php:
$retVal = `/path/to/custom/script/myscript.sh &2>1`;
if ($retVal) echo 'SWEET';
else echo 'POOP: ' . $retVal;
{command} inside `` means the same as shell_exec('{command}') on a linux server.
If I'm not way off base here your goal is to add users to your VPN (among other things perhaps) in which case you could pass command-line parameters for username/password/etc to your custom shell script.
It's not exactly a pure shell_exec() answer to your question, but maybe you can run myscript.sh without doing sudo. Just basing this on the fact you said everything you tried worked in shell and path was set in echo $PATH, but not sudo echo $PATH in your PHP script. Seems for some reason that sudo is an issue in php, but not in shell so...let's move the sudo back to shell and try it that way.

Categories