Launch pg_restore via CMD with PHP doesn't work - php

I try to restore a database (".backup" file) with pg_restore with cmd launch with PHP code but it doesn't work.
1- I give to my PHP variable all the cmd code
$cmd="cmd /c C:/Program Files/PostgreSQL/9.1/bin/pg_restore.exe -d ".$_POST['data2']." -i -h localhost -p 5432 -U ********** D:/backup/voirie.backup";
2- I launch it
exec($cmd);
My php work without result
On my server the process is launched
Other informations
In place of exec() I tested with system() and passthru()
I tested to create a batchfile from my php. This batchfile, work fine when launch him on the server but not from my PHP file
The generate code in $cmd was tested directly from CMD on the server and it works
For other codes on this server I use this who work fine: system("cmd /c C:/Python35-32/python.exe D:/python/serpent.py");
I tested with this quotes without result : $cmd='cmd /c "C:/Program Files/PostgreSQL/9.1/bin/pg_restore.exe" -d '.$_POST['data2'].' -i -h localhost -p 5432 -U ********** D:/backup/voirie.backup';

Related

PsExec from PHP does not run me

Executing the PsExce command from the shell_exce or exce does not generate any action on the machine that sent the command
My php code is
$execute = "C:\\pstools\\PsExec.exe \\\\".$ip." -u ".$user." -p xxxxxxxx -i -h -d C:\\app\\ejecutar.bat 1";
exec($execute,$out);
print_r($out);
The message that comes back to me is this
If that command is executed from the console if you run it on the remote machine

How to execute a local Shell Script with SSH commands on server?

I want to run a local shell script that have SSH commands on the server using PHP. And inside the script i am using ssh to run a command like ls -lart and save the result on a log file in the server, and then using scp to copy the remote file to my local host. Something like this:
/// my_local_shell.sh
#!/bin/bash
host=$1
user=$2
port=$3
ssh -p $port $user#$host 'ls -lart >> /home/remote/file.log'
scp -P $port $user#$host:/home/remote/file.log /home/local/file.log
If i run the script using the terminal user#local_host:~$ ./my_local_shell.sh everything works just fine. But if i use shell_exec() to execute the script using PHP like this:
/// index.php
$output = shell_exec("my_local_shell.sh 192.168.1.1 root 2222");
echo <pre>$output</pre>;
Nothing is printed on screen and the SSH commands inside the file are not executed.
I know I can use ssh2_shell(), but by using it I would have to send the commands inside the PHP, and it's not what i want.
I already gave the permissions needed to index.php and my_local_shell.sh
Any ideas how I can do this?
Apparently scp uses some sort of ncurses that you can't capture, so you could add the -v flag to your scp command in the shell script
scp -v -P $port $user#$host:/home/remote/file.log /home/local/file.log
or alternatively, since scp returns 0 on success you could write
scp -P $port $user#$host:/home/remote/file.log /home/local/file.log && echo Success
As for the PHP please check you have PHP opening and closing tags and correct your echo statement
echo "<pre>".$output."</pre>";

Change rsync port with php exec

With PHP, I am trying to connect to a remote server and exec an rsync command :
$rsync_cmd = 'rsync -P -azv -e "ssh -p 1234" /dir/file.jpg root#remoteserver2:/dir/file.jpg';
exec('sudo ssh -tt root#remoteserver1 '.$rsync_cmd.' 2>&1', $output);
The remote connection is working fine but then when I am including my rsync command I got a port error "ssh: connect to host remoteserver2 port 22: Connection refused"
The correct port to use is 1234 and this command is working fine on the Terminal (shell) but php "exec" function dont want to take it ("ssh -p 1234"), any idea ?
Did you test it in Terminal by sshing into remoteserver1, and then executing the rsync command, or by executing the entire ssh-to-rsync command on one line? Because I'm pretty sure doing it in two steps will work, but one step won't. This is because when it's done as one step, the command string is parsed by the local shell (including doing quote and escape interpretation and removal) before it's passed over ssh to the remote shell (which then does another pass of quote and escape interpretation and removal). Those double-quotes in "ssh -p 1234" get parsed and removed by the local shell, so they don't have the intended effect of being parsed and applied by the remote shell.
If I'm right about the problem, the solution is pretty simple: escape the double-quotes. That way the local shell will parse and remove the escapes, and pass the double-quotes through unmolested so the remote shell can see and apply them:
$rsync_cmd = 'rsync -P -azv -e \"ssh -p 1234\" /dir/file.jpg root#remoteserver2:/dir/file.jpg';
exec('sudo ssh -tt root#remoteserver1 '.$rsync_cmd.' 2>&1', $output);
The Solution is pretty mush simple try skipping -P flag from the command.
$rsync_cmd = 'rsync -azv -e \"ssh -p 1234\" /dir/file.jpg root#remoteserver2:/dir/file.jpg';
exec('sudo ssh -tt root#remoteserver1 '.$rsync_cmd.' 2>&1', $output);
It will work fine with that -P flag it took default port for the rsync.

Running tasks in background in laravel 4.2

I have a condition where I have to start iperf server as daemon on specified port and if iperf server is running, I have to send response to client. I tried
shell_exec('iperf -s -p {port} -D');
but it doesn't return control / infinite loop starts.
The server will start but the code below the shell_exec will never be executed.
Anyone has a solution or suggestion how I should approach this to get the result?
Your command iperf -s -p {port} -D happens to have stderr output, try doing this:
$outfile = "/tmp/erroutperf.out";
$port = 8080;
shell_exec("iperf -s -p $port -D > $outfile 2>&1");
basically the additional command > /tmp/erroutperf.out 2>&1, tells bash to save
both stderr output and stdout of a program (iperf) to a file /tmp/erroutperf.out
getting the output of the command is:
file_get_contents($outfile);

Run a command on a SSH screen through SSH with phpseclib

I have a PHP script that starts a detached screen through SSH:
$ssh->exec("screen -m -d -S ".$user);
I now need to execute a command in that screen without being in that screen. I have the code that does that, which I have tested through a SSH client, but when I try to use it with the phpseclib exec command, it does not work. This is the code that works:
screen -S ".$user." -X stuff "cd minecraft/servers/".$user."/;sh start.sh $(printf '\r')"
And this is it in the PHP script:
$ssh->exec("screen -S ".$user." -X stuff \"cd minecraft/servers/".$user."/;sh start.sh $(printf '\r')\"");
I attempted to escape the extra double quotes in the code.
Is there anything I can do to make this work through PHP? Thanks
Hmmm...
create please two bash script, first: create screen with user parameter with name f.e. run_screen, second: tester for SSH client with user parameter with name f.e. run_test.
Run first script:
$ssh->exec('[full_path]/run_screen ' . $user);
and second:
$ssh->exec('[full_path]/run_test ' . $user);
bash syntax is here bash syntax
Sure that the user of server (f.e. Apache) has permissions to run scripts.

Categories