SSH2_EXEC() Command failed - php

I have successed connected and authorized via ssh2 functions.
But I can't exec command via ssh2_exec() function.
Notoriously get this error:
ssh2_exec(): Unable to request command execution on remote host
I was trying pass '/bin/ls' 'ls -l' nothing works.
Also I have changed user and getting the same error.
EDIT:
I have changed remote host and all works as expected.
How to set up first host? User name that I'm using in credentials has bash shell enabled. So I might log in via putty and run same command that I can't via PHP.

Sorted!
Problem was that I have default port 22 assigned to SFTP service that's why I was successfuly connecting to remote host but I could not execute command.
SSH listening on port 23 on that problematic machine, so I changed my script to port 23 and all works as expected now.

Related

Tunnelling / Proxy SSH

I need to connect to a particular API but that API only accept request from my mate server. Then include thoses informations back in our website.
So basically I need to connect to the server make the request receiving the answer transfering it to my host so that I can play around with them and integrating them to my website through a php file.
I've already exchange ssh keys and I can connect to my server easily, I know I probably need to use ssh -L (not -R or -D) for the tunneling, though I don't know what to do with my php files to make that request etc etc or what are the other steps once I've entered that command.
If anyone can help that would be lovely :) !
You can create an SSH tunnel using the -L command line switch:
$ ssh -L [port on local]:[apiserver hostname]:[port on apiserver]
[user]#[your friend's server hostname]
E.g.
$ ssh -L 8080:apiserver.com:80 bob#friend.server.com
After the above command successfully connects to friend.server.com, any requests sent to your localhost:8080 will be tunneled through friend.server.com host and arrive at apiserver.com:80. From apiserver's perspective, the request's origin is friend's server.
(This will actually open an SSH session in the terminal window where executed, i.e. you get the prompt of the remote server, which is not required and you can ignore that prompt. It is possible ro run this in the background w/o console login with other switches)

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.

FTP in PHP script and command line fails with 553, but Filezilla transfers OK

I'm getting a weird FTP failure in a PHP script. I've recreated the failure just using command line FTP, so I think I can eliminate PHP from my enquiries.
Using command line FTP in Ubuntu 10.10, I can connect to my FTP server, log in, list directories, etc. However, when I try to transfer a file from my local machine to the FTP server using "put" I get a "553 Could not create file" error. I get the same error in active and passive modes. Transfer is set to binary.
When I run FileZilla on the same machine and connect to the same FTP server using the same credentials, and try to transfer the same file to the same destination directory, the transfer works just fine, after defaulting to binary and passive mode.
FWIW, the PHP script (which is a simple ftp_connect, ftp_login and ftp_put) using the same details fails with "Illegal PORT command", although I suspect this is just PHP's interpretation of the same error I get from command line FTP. Again, this error occurs in both active and passive FTP modes.
FileZilla and ftp command probably starts in a different directories. You can setup starting remote directory in FileZilla and you have to go to the same location from command line.
Try using pwd command after logging in from command line client, it will show current working directory, compare that with the one FileZilla is using and then go to the correct directory with a cd command.

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.

mysql_connect() doesn't work when run by apache; works from command line

I have a strange issue. I'm trying to write a simple php webpage on my server, but mysql_connect() doesn't connect to any server, either local or otherwise. Here's where it gets strange. If I take the same php script and run it from the commandline, the script works. phpinfo() indicates that both the file (being run by apache) and the commandline (run as root) are calling the same version of php, mysql is loaded, and the php.ini is the same.
Furthermore, I'm running a MediaWiki installation on this same server, and it's using the mysqld installed locally and works just fine, so I'm completely at a loss as to why the code isn't working. The error I receive on runtime:
Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (13)
(The IP is x'd out for the privacy of the owner of the server I'm connecting to)
What operating system are you trying to connect from? It sounds like it may be an SELinux issue.
With SELinux, you can usually allow apache to make network connections by using
/usr/sbin/setsebool httpd_can_network_connect 1
OR
/usr/sbin/setsebool httpd_can_network_connect true
An operating system without SELinux may have a similar protection mechanism.
Check basic network connectivity. ping xxx.xxx.xxx.xxx from the webserver. If that doesn't work, check the network configuration on both ends.
Try running mysql from the webserver: mysql -h xxx.xxx.xxx.xxx -u user -p If that can't connect, track the cause: it could be a SELinux issue on either end (check the security log), or maybe there is an alias in /etc/hosts? Also enable verbose messages with -v -v -v on the mysql command line.
Check that the user is allowed to log on using the user administration feature of mysql.

Categories