PHP outputs from command line but not from the browser - php

I have a PHP file testphp.php with the following content:
<?php
echo system('mysql -u root -pMyPassword -e "SELECT VERSION();"');
?>
It outputs perfectly from the command line:
D:\>php testphp.php
VERSION()
5.5.24-log
5.5.24-log
D:\>
When I execute the same file via a web browser (http://localhost/testphp.php) , I see no output. Why?

There are lots of reasons for this:
You are using the command mysql which may not be in the path of the user that the web server runs as. When you run it from the command line, you are running it with your own user account. The web server runs under a different (restricted) account. You should provide the full path to the mysql binary. Type which mysql as your normal user from the prompt to find out what is the full path to the executable.
The command mysql may be restricted by your system configuration so that not all users can execute it.
Your PHP configuration on the web server prohibits the use of system().
You can get the same information by running this (assuming you have mysql configured for your php installation):
$con = new mysqli("localhost", "root", "MyPassword");
printf("%s", $con->server_info);

Related

ssh2_exec not executing simple command

ssh_exec() is refusing to execute a command in Windows.
Here is my code:
<?php
$connection = ssh2_connect('localhost', 22);
ssh2_auth_none($connection, 'root');
$stream = ssh2_exec($connection, 'C:\Program Files\CCleaner\CCleaner.exe',FALSE);
?>
It shows me the following warning: Unable to request a channel from remote host in.
Firstly, it is unlikely that you are going to be able to connect to your Windows server using ssh2_connect, because SSH is not a protocol typically used to connect to Windows, nor is it an available item in any Windows install.
Secondly, make sure that you have created the user 'root' on the Windows server, because it won't exist by default.
Alternatively
If you are actually connecting to a Unix/Linux server FROM a Windows box, then the fact that you are trying to run a Windows command on it ('C:\Program Files\CCleaner\CCleaner.exe'), will fail. You can only execute commands on the remote server that are present, and any command that begins with "C:\" will obviously fail because Linux has an entirely different filesystem structure from Windows.
So from the code above it looks like you're probably trying to connect to a Linux server using the username 'root' with no password (which according to the PHP.net manual (ssh2_auth_none:
Attempt "none" authentication which usually will (and should) fail.
But as you have said you are trying to execute a command in Windows, and you are connecting to the server with host name 'localhost' - perhaps you actually are running this on Windows, and simply trying to use SSH to connect to the Windows server - which is pointless as the PHP code shown above will already be being executed on the Windows server, making the SSH connection completely redundant except to attempt to execute a command with elevated privileges (as 'root').
If you are trying to execute the command remotely using PHP as an administrator user, which is probably a bad idea, then you could consider using the function.exec.php command in combination with the Windows runas command. Or you could just run your web server as an administrator, which is both very easy to do and very bad for security.
You could basically achieve what you are trying to do above, by installing an SSH server on Windows (What are some good SSH Servers for windows?) - after which you would have to log into the server using a user that exists, and use the password for that user (or create the user 'root' and use that).
Depending on your SSH server installation, running the command C:\Program Files\CCleaner\CCleaner.exe could work in the way you described above.
Is there any 'sudo' command for Windows?
How to run PHP as an Administrator
There are more examples of how to go about this in Linux, different methods have different advantages and disadvantages.
Execute root commands via PHP
How to run PHP exec() as root?
<?php
$connection = ssh2_connect('localhost', 22);
ssh2_auth_none($connection, 'root');
$stream = ssh2_exec($connection, 'C:\Program Files\CCleaner\CCleaner.exe', false);
stream_set_blocking ($stream, true);

ssh connection from php

I need to ssh to the server using the username user has entered on my webform.
How can this be done?
If what you mean is, "How do I connect via SSH from my website (to another server)", then you can do this with the PECL ssh2 library.
See:
http://pecl.php.net/package/ssh2
Walkthrough (untested): http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/
At first, there are no PuTTy commands. These are shell commands.
To run PHP script in shell, you need to use php-cli:
maybe you could use Command Line Scripting in PHP, it depends on what you want. http://php.net/manual/en/features.commandline.php
I am not sure but I think(correct me if I am wrong) that you want to click somewhere on a link on the webpage and open putty(on the user's computer) to connect to a server.
You can configure Putty to handle ssh:// links. How to do it you can find out here.
When that is configured all you have to do is to have a link similar to this:
Click here to connect
Have in mind that this will work only on systems that are configured to handle the ssh:// link type
I hope that this answers your question.
This is how you use putty via PHP (not dependent of cli). Note that the passwords are not protected and that an interactive ssh session would be much more involved. However, HTTPS and mcrypt (if needing to store passwords and/or bash scripts) can make this a safe solution.
<?php
// EDIT: added escapeshellcmd() to following vars
$user = escapeshellcmd($_POST['user']); // username
$host = escapeshellcmd($_POST['host']); // domain
$pass = escapeshellcmd($_POST['pass']); // password
// create a string that will be loaded into a bash file for putty
// String can easily be made dynamically.
$bash_sh = <<<EOF #START OF BASH
\#!/bin/bash
echo "BASH ON SSHD SIDE"
for (( i=1; i<=5; i++ )) # BASH FOR LOOP
do
echo "echo \$i times in bash" #\$i is BASH not PHP, so have to escape
done
EOF; #END OF BASH
// creates a temp file called 'bash.sh' using the bash script above
file_put_contents("bash.sh", $bash_sh);
// executes putty using the args -ssh, -pw, -t, -m
// -ssh tells putty to use ssh protocol
// -pw tells putty to enter the password automaticaly
// -t tells putty to use a psudo terminal.
// -m tells putty read and execute bash.sh once logged in
exec("putty.exe -ssh ".$user."#".$host." -pw ".$pass." -t -m bash.sh");
// delete bash file since it has been sent
unlink('bash.sh');
?>

Freetds SQL Server connection error from PHP: Permissions?

I have a PHP script that makes a connection to a remote SQL Server.
From the command line as root I can call the freetds command:
tsql –H hostname –U username
and it connects just great and I can run queries.
I have a PHP script in /var/www/html/axis/public/test.php
When I: sudo –u apache –s (change the user from root to apache) and then run my PHP script from the command line, it connects and runs the queries just great.
But when executing the same script from the web browser, it fails - the browser returns a DB Connection error. The web browser is able to connect everywhere it is supposed to and renders all the web pages that don't need an SQL connection.
apache is the user that is running httpd. I’ve confirmed this via: ps aux | grep apache
Any ideas as to why apache can execute the PHP script fine from the command line, but when the browser attempts to connect to the very same script, it fails?
Thanks,
Derrick
As per the FreeTDS FAQ:
http://www.freetds.org/faq.html#php
Also can you post your connection code and what error's you're getting exactly?
So the problem was with SELinux. The web server was forbidden from connecting outside the network (to the SQL Server). I had to run this command:
/usr/sbin/setsebool -P httpd_can_network_connect_db 1
Once that was done, all was well. This answer was provided with help from Daniel Fazekas.
Thanks everyone for taking a look at this issue with me.

PHP shell_exec running a shellscript with ssh

I have a shellscript with connects to a a different machine with ssh and a key so it does not need the username and password.
When i run this script from commandline it works fine.. but when I run this script from php shell_exec it does not work.
If I make an ssh connection with PHP and run the script as my own user it does work.
Now for my question :D
Is there a way to just running the script in shell_exec from php without making an connection over ssh as a different user?
Did you specify the private key file correctly?
If you are using Ubuntu or Debian the web server is running with the user name www-data. For other systems please check the web server configuration for the user name. You can simply test if this user (and your php web application) is able to do the SSH connection.
1) Become the user of your web server
sudo su www-data
2) Try connecting the remote host
ssh remoteUser#remoteHost
If you will get connected without entering a password there must be a different problem. If you have to enter a password, the key files were stored for a different user - not for www-data. You have already configured SSH to use the key. Do the same for your local user www-data and it will work.
It seems ssh connection does not work with shell_exec. If i run the shellscript under ssh2_exec it does seem to work.
Which is a little strange as the ssh connection is made in the script file with a public and private key.. I would assume this would just run :s
The webserver is allowed to execute the file, as there are other command in there who work as expected.

Php : running ssh from Windows to login to a Linux and run a script

Here's my goal :
I have a Windows XP PC with all the source code in it and a development database.
Let's call it "pc.dev.XP".
I have a destination computer that runs Linux.
Let's call it "pc.demo.Linux".
Here's what I've done on "pc.dev.XP" (just so you get the context) :
installed all cygwin stuff
created a valid rsa key and put it on the dest
backup computer so that ssh doesn't
ask for a password
rsync works pretty well this way
If i try to do this on "pc.dev.XP" via a command line :
cd \cygwin\bin
ssh Fred#pc.demo.Linux "cd /var/www && ls -al"
this works perfectly without asking a password
Now here's what I want to do on the "pc.dev.XP":
launch a php script that extract the dev. database into a sql file
zip this file
transfer it via ftp to the "pc.demo.Linux"
log to the "pc.demo.Linux" and execute "unzip then mysql -e "source unzipped file"
if I run on "pc.dev.XP" manually :
putty -load "myconf" -l Fred -pw XXX -m script.file.that.unzip.and.integrates.sql
this works perfectly.
Same for :
cd \cygwin\bin
ssh Fred#dest "cd /var/www && ls -al"
If I try to exec() in php (wamp installed on "pc.dev.XP") those scripts they hangs. I'm pretty sure this is because the user is "SYSTEM" and not "Fred", and putty or ssh ask for a password but maybe I'm wrong.
Anyway I'm looking for a way to automate those 4 tasks I've described and I'm stuck because exec() hangs. There's no problem with safe_exec_mode or safe_exec_dir directives, they're disabled on the development machine, thus exec() works pretty well if I try some basic stuff like exec("dir")
Any idea what I could do / check / correct ?
I'm not sure if this is what you need, but I typically use a construct like this to sync databases across machines:
php extractFromDb.php | ssh user#remote.com "mysql remoteDatabaseName"
This executes the PHP script locally, and pipes the SQL commands the script prints out through SSH straigt into the remote mysql process which executes them in the remote database.
If you need compression, you can either use SSH's -C switch, or integrate the use of your compression program of choice like this:
php extractFromDb.php | gzip -9 | ssh user#remote.com "gunzip | mysql remoteDatabaseName"
You want to do this from PHP running under apache, as in I go to http://myWebserver.com/crazyScript.php and all this happens? Or you just want to write your scripts in PHP and invoke them via cmd line?
If you want the first solution, try running your apache/iss under a different user that has credentials to perform all those tasks.
"if I run on the development PC manually this works perfectly.".
Why not do it like that? When you run that script, I assume you're connecting to the local SSH server on the dev machine. When you do this, you are using the credentials Fred, so everything works. When you run the PHP script, you are right that it is probably running as SYSTEM.
Try either changing the user that apache is running as or use php to connect to the local ssh thereby using alternate credentials.
Here's what I did :
a batch file that :
Calls a php file via "php.exe my_extract_then_compress_then_ftp.php"
Calls rsync to synchronize the source folder
Calls putty -l user -pw password -m file_with_ssh_commands_to_execute
It works like a charm.

Categories