Escaping quotes in a PHP script using ssh2_exec - php

I need some help with escaping the quotes in this script, I have code above and below, but it does not pertain to this because if I just issue a screen -d -m -S minecraft, it creates one fine. Any help would be amazing because i've been trying for an hour now, Thanks!
if (!($stream = ssh2_exec($con, 'screen -d -m -S minecraft -p 0 -X stuff "\printf "\say This is a test.\r"\"\' ))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
echo $data;
fclose($stream);
}

Shouldn't the \ should go before the ". Also, from your description, it sounds like that you only want to run "screen -d -m -S minecraft" and it looks like you have an extra printf in there.
if (!($stream = ssh2_exec($con, "screen -d -m -S minecraft")))
http://php.net/manual/en/language.types.string.php

You only need to change "\ to \".

Related

Why exec() is not working in foreach loop?

See, for testing purpose if I am trying to run PHP exec() statically with only one video file, then its compressing video perfectly(please see first line line).
Later when I am compressing dynamically in the loop, then exec function is not working. Please tell me why it's not working in the loop?
// echo shell_exec("/usr/local/bin/ffmpeg -i /home4/machine/public_html/riyaz/check_video/video_1494453415.mp4 -strict -2 -crf 20 /home4/machine/public_html/riyaz/zcompress/video_1494453415.mp4");
#include_once("start.php");
$directory_path = FILEPATH.'/';
$video_mp4 = glob("/home4/machine/public_html/riyaz/check_video/*.mp4");
/*video size will show in mb*/
foreach ($video_mp4 as $video_mp4_list){
$video_size = filesize($video_mp4_list);
$video_size_in_mb = round(($video_size/1048576), 2);
$get_file_name = explode('/',$video_mp4_list);
$get_file_name_for_destination = $get_file_name[6];
$getSourceFileNamePath = '/home4/machine/public_html/riyaz/check_video/'.$get_file_name_for_destination;
$getDestFileNamePath = '/home4/machine/public_html/riyaz/zcompress/'.$get_file_name_for_destination;
if ($video_size_in_mb >= 1000 ){
echo exec("/usr/local/bin/ffmpeg -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath);
}
}
shell_exec() returns the full output: PHP shell_exec() however
exec() returns only the last line from the outptut that might be empty.. So you have to provide second $output and third parameter $return_var to get more useful data: PHP exec()
if ($video_size_in_mb >= 1000 ){
$command = "/usr/local/bin/ffmpeg -y -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath . ' 2>&1';
echo PHP_EOL ."Executing command: ". $command;
if(file_exists($getDestFileNamePath)) echo "File already exists!";
$output = null;
$return_var = null;
exec($command, $output, $return_var);
print_r($command);
print_r($return_var);
print_r($output);
echo PHP_EOL;
} else {
echo "video too small to proceed';
}
php shell_exec() vs exec()
EDIT: The problem is that the destination file exists and ffmpeg exits without any action. One solution is to use attribute -y to overwrite the output file 5.4 Main options -y (global) Overwrite output files without asking.

Loop bash script until cURL response doesn't equal string

I'm trying to loop a bash script (cURL below) until my remote PHP script doesn't equal failure. I've tried the following, and it works when $fileName exists.. but doesn't loop when it returns failure.
My PHP:
<?php
$videoFile = $_GET["name"] . ".mp4";
if (file_exists($videoFile)) {
echo "success";
} else {
echo "failure";
}
?>
Bash command:
until $(curl --output /dev/null --silent --head --fail "my-remote-php-script.php"); do
printf '.'
sleep 1
done
This should work:
while [ "$(curl -s 'http://your-url.php')" == "failure" ]; do
echo -n '.'
sleep 1
done
The $() places the curl in a sub-shell. The resulting string, comming from your php script, is compared with failure.
Try this:
url="http://server.tld/foo.php"
until curl -sf "$url"; do echo -n "."; sleep 1; done

sudo_exec returns nothing

I try to ping www.google.de with shell_exec and store the result into a variable but i get no result back from shell_exec.
<?php
$ping = 'sudo ping -c 4 ';
$url = 'www.google.de';
$command = $ping . $url;
$ping_result = shell_exec($command);
$datei = fopen("/var/www/myProject/result_ping","w") or die ("Could not open file!");
sleep(10);
if ($datei == false)
{
$ping_result = "Cannot open file!";
}
else
{
fwrite ($datei , $ping_result);
fclose ($datei);
}
echo $command; //Output: sudo ping -c 4 www.google.de
echo $ping_result; //Output: nothing
?>
The file result_ping has all rights (chmod 777).
Maybe the webserver is not allowed to execute ping?
Add 2>&1 to your command to ensure you're not getting an error message that shell_exec would filter off:
$command = $ping . $url . ' 2>&1';
shell_exec will return NULL in case of error. With that modification you redirect any error message to normal output, thus forcing shell_exec show every message you would normally get on a console session.

SSH2 and PHP not working 'Terminal'

Whenever I try and execute
if (!($stream = ssh2_exec($con, "cd $dir; screen -S creative java -Xmx500M -Xms500M -jar spigot.jar" ))) {
it keeps returning with 'Must be connected to a terminal.' which I can't seem to get around.
How do I get around/fix this?
You could try to use phpseclib's read() / write() functions. eg.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->read('username#username:~$');
$ssh->write("ls -la\n"); // note the "\n"
echo $ssh->read('username#username:~$');
Test this :
You should pass a pty emulation name ("vt102", "ansi", etc...) if you want to emulate a pty, or NULL if you don't.
Passing false will convert false to a string, and will allocate a "" terminal.
cf. http://php.net/manual/fr/function.ssh2-exec.php
Starting screen like you did requires a valid TTY because of it get's attached to your session. The ssh implementation of PHP just doesn't create a session like that.
Starting screen detached should do the trick:
screen -d -m [<command>]
So your code would look like:
if (!($stream = ssh2_exec($con, "cd $dir; screen -d -m -S creative java -Xmx500M -Xms500M -jar spigot.jar" ))) {

can't download file using wget with authentication while being called from php

I've created a small remote download script which downloads remote files to my server using wget.
Now here's how it works:
I have a html form, which the user can enter the URL, destination file name, and optionally username and password to authenticate, in case needed.
The form calls a php script, using AJAX, and the php script passes the information to a shell script.
Now the problem is, the shell script works flawlessly on my local linux machine, but on my server, it doesn't work with authentication(without authentication, it works just fine.)
#!/bin/bash
if test $# -eq 2
then
wget -O "$2" $1
elif test $# -eq 4
then
wget -O "$2" --http-user="$3" --http-password="$4" $1
else
wget $1
fi
it might be worth mentioning that, I own a shared server.
Thanks
EDIT:
Is it possible that the version of wget installed on the server, doesn't support http authentications???
EDIT 2:
I piped the output of wget to a file like this:
wget -O "$2" --http-user="$3" --http-password="$4" $1 | echo >> output
but the output is empty, i.e. no messages is being printed from wget!!!
And I did this too, to check if the credentials passed is ok:
echo "$3 | $4" >> credentials
And it was ok.
And this is the php code which runs the shell script:
if(isset($_POST["startdownload"]))
{
list($url, $dest, $user, $pass) = explode("|", urldecode($_POST["data"]));
$filelen = file_len($url);
$freespace = getRemainingSpace();
//die( "( Free: $freespace, File: $filelen )" );
if($filelen > $freespace - 10000)
{
$result = json_encode(array("error" => true,
"message" => "There is not enough space to download this file. ( Free: $freespace, File: $filelen )"));
die($result);
}
else
{
if($user != "NULL" && $pass != "NULL")
{
execInBackground("../dl.sh '{$url}' '../{$dest}' '{$user}' '{$pass}'| at now");
}
else
{
execInBackground("../dl.sh '{$url}' '../{$dest}' | at now");
}
$result = json_encode(array("error" => false, "message" => "Starting download..."));
die($result);
}
}
function execInBackground($cmd)
{
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
Well, running this script via system/exec/shell_exec or any others, probably going to "block" the rest of the PHP script anyway. But as an answer, check out this: http://www.zarafa.com/wiki/index.php/Using_wget_and_HTTP_authentication_for_supported_downloads
wget -O "$2" --user="$3" --ask-password="$4" $1

Categories