Bin Bash file not found - php

i'm really beginner with SH Scripts.
I found a small sh script that convert a php file with wget to html. I would like to do a small cronjob with it. But everytime i run that script i get the message (Translated) "Defect Interpreter" > File or folder not Found".
My script is only
#!/bin/bash
rm -rf header-wrapper.html && wget http://master.gnetwork.eu/header-wrapper.php -O header-wrapper.html -q

The error message tells you that your shebang is wrong (bash executable is not found at /bin/bash when cron job starts).
From your cron job, use bash explicitly when calling the script:
bash myscript.sh
instead of:
./myscript.sh
Also, do not make any assumptions on the working directory of the cron job. Change the directory in your bash script before doing anything else
#!/bin/bash
cd /my/desired/path && \
rm -rf header-wrapper.html && \
wget http://master.gnetwork.eu/header-wrapper.php -O header-wrapper.html -q

Try typing Bash in before the filename.
bash [File Name Here]
instead of
.[File Name Here]
Sorry if someone else already answered this.

Related

shell_exec() tcpdump from php

I am trying to write a script in php to run the following bash script using apache2+php7
#!/bin/bash
#cd /home/cwc/http/www/html/admin/web/
nowfile=$(date +"%Y%m%d-%H%M%S")
nohup tcpdump -w $nowfile.pcap -i enp2s0 >> /dev/null 2>&1 &
Now I understand I might have to use full paths
The above code works with a non sudo user using bash because I added a user the the pcap group.
I'm trying to figure out why this will not work with php
<?php
$command = "/pathtoscript/tcpdmp.sh
shell_exec($command); //not working?
?>

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

How to run Git Bash commands using 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);
}

Run bash command via PHP, change dir then execute binary

So here's what I'm trying to do. I'm trying to start a binary under another user, via a PHP script. Apache has sudo access. The command works fine when ran via putty logged in as "test".
passthru('bash -c "sudo -u test cd /home/test/cs/ ; ./hlds_run"');
also might I add that
passthru('bash -c "sudo -u test ./home/test/cs/hlds_run"');
Won't work because of how the binary is written (it won't find it's resources unless you cd to the folder before, tested on terminal)
If everyone has access to /home/test/cs:
passthru('cd /home/test/cs && sudo -u test ./hlds_run');
If only the user test has access to the directory:
passhtru('sudo -u test sh -c "cd /home/test/cs && ./hlds_run"');
To arrive at the second invocation, you should already be familiar with system vs execve semantics (used by passthru and sudo respectively).
This is the tested shell string we need to run as a specific user:
cd /home/test/cs && ./hlds_run
We can ensure that it always runs as a specific user with sudo, but sudo uses execve semantics. We have to convert our shell string to an execve array, and since this command A. relies on shell functionality like cd and B. does not include dynamic values, the best way to do this is to simply invoke a shell to interpret it verbatim:
{ sh, -c, cd /home/test/cs && ./hlds_run }
We can now apply sudo to run as our specific user:
{ sudo, -u, test, sh, -c, cd /home/test/cs && ./hlds_run }
passthru runs as a shell, so now we have to convert the execve array above back into a shell string, taking extreme care with quoting to ensure the shell will parse it into the exact argument list above. Fortunately this is a relatively simple case:
sudo -u test sh -c "cd /home/test/cs && ./hlds_run"
We can now give it to passthru:
passthru('sudo -u test sh -c "cd /home/test/cs && ./hlds_run"');
Have you try this? (make sure if you have exec right for hlds_run)
passthru('bash -c "sudo -u test && /home/test/cs/hlds_run"');

Categories