Change rsync port with php exec - php

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.

Related

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>";

Passing a variable from PHP to ubuntu

I want to run a script on my ubuntu server with a variable from php in it.
Here are some of the things i've tried to pass a variable to ubuntu...
shell_exec('vpsName=HI3');
vpsName=`mysql -D jake_db -h 127.0.0.1 -u jake -pXXXXXXX -se "SELECT vpsName FROM reinstalls WHERE status = 'pending'"`;
The final way in which i thought I could fix it was to avoid running the script through ubuntu all together and run it from shell_exec(); but it fails on running the guestfish commands.
Here is my entire reinstalls.sh script.
sudo rm /var/lib/libvirt/images/"$vpsName".qcow2 && sudo wget -O /var/lib/libvi$
guestfish -a /var/lib/libvirt/images/"$vpsName".qcow2 <<'EOF'
run
mount /dev/ubuntu-vg/root /
rm /etc/network/interfaces
EOF
sudo fusermount -u /mnt && virsh start "$vpsName" && echo "IT WORKED!"
I am open to any way of getting this to work, as long as its secure,
Thanks in advance,
Jake
EDIT:
If I run the script with a VPS name instead of a variable, it works. I just can't find a way to pass the variables from the website to the ubuntu16.04 OS.
Well one way would be to run each command from PHP:
shell_exec("sudo rm /var/lib/libvirt/images/" . $vpsName . ".qcow2");
shell_exec("sudo wget -O /var/lib/libvi ... etc");
The other would be to invoke your shell script from PHP, passing the vps name as a parameter:
shell_exec("reinstalls.sh " . $vpsName)
But then you'd have to rewrite the shell script to pick up the command line parameter and apply it as necessary. In the case of bash, this explains how to go about that.

pass variable from PHP to Ubuntu shell script

I have a shell script like this (in /usr/local/bin/esm-script/import-master.php):
#! /bin/bash
echo "this is the file name $1."
script -c 'PGPASSWORD="pwd123" /usr/bin/psql --host localhost --port 5432 --username "postgres" --no-password --quiet "dbESM" < "$1"' /dev/null
Now I'm calling it through a PHP script like this:
$NewFile = "/var/www/...master-data.sql"; //full path
$strImport = '/usr/local/bin/esm-script/import-master.sh ' . $NewFile;
$strMsg = shell_exec($strImport);
echo "$strMsg<br />";
echo 'done!';
However, when I run the PHP code, this is the message I get on the browser:
this is the file name /var/www/ESM-Backend/uploads/master-data.sql. Script started, file is /dev/null Script done, file is /dev/null sh: 1: cannot open : No such file Script started, file is /dev/null
done!
I'm not a shell scripting person so I don't know if I'm missing something.
I've checked that the folder with the sql file has the correct permissions (775) and has data (insert statements).
So why does this not work? Any ideas and guidelines are really appreciated.
EDIT
I hard-coded the file in the shell script file like this:
script -c 'PGPASSWORD="#UR!23" /usr/bin/psql --host localhost --port 5432 --username "postgres" --no-password --quiet "dbESM" < "/var/www/ESM-Backend/uploads/master-data.sql"' /dev/null
And it works. But I need it to run with the file passed through PHP.
I think the problem might be with the way you are passing the argument to your shell script i.e. $NewFile. From a relevant SO Post, you might want to try enclosing the argument(s) in double quotes like this:
$strImport = '/usr/local/bin/esm-script/import-master.sh "'.$NewFile.'"';
I'm assuming the permissions are set correctly and you are able to use shell_exec() normally to execute shell scripts via PHP. Hope this helps.
First, a big thank you for Vivek for giving me the idea of using quotes. I then thought that the the error was the way the command-line argument was enclosed within single quotes in the shell script file.
So I tried the following:
script -c 'PGPASSWORD="#UR!23" /usr/bin/psql --host localhost --port 5432 --username "postgres" --no-password --quiet "dbESM" < "'$1'"' /dev/null
And yes, it solved the problem.
The idea is that the $1 argument was given outside of the quoted string for the script command.
I also had the doubt on how to concatenate strings in bash and here's a great link I found for it: How to concatenate string variables in Bash?

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.

Python's os.execvp equivalent for PHP

I've got a PHP command line program running. And I want to connect to a mysql shell straight from PHP. I've done this before in Python using os.execvp But I can't get the same thing to work in PHP.
I've tried the following functions:
system
passthru
exec
shell_exec
example:
system('mysql -u root -pxxxx db_name');
But they all seem to wait for mysql to exit and return something. What I really want is for PHP to launch the mysql shell and then exit it self. any ideas?
If you want shell commands to be interactive, use:
system("mysql -uroot -p db_name > `tty`");
That will work for most cases, but will break if you aren't in a terminal.
Give MySQL a script to run that's separate from the PHP script:
system('mysql -u root -pxxxx db_name < script.mysql');
In addition to what acrosman said, you can also use the -e switch to pass SQL from the command line.
$sql = ".....";
system("mysql -u root -pxxxx db_name -e \"$sql\"");
Also, I hope your not actually connecting to the database as root from a PHP application.

Categories