PHP function ssh2_connect is not working - php

Following is my script:
<?php
$connection = ssh2_connect('XX.XX.XX.XX', 22);
ssh2_auth_password($connection, 'root', '******');
$stream = ssh2_exec($connection, 'useradd -d /home/users/test -m testftp');
$stream = ssh2_exec($connection, 'passwd testftp');
$stream = ssh2_exec($connection, 'password');
$stream = ssh2_exec($connection, 'password');
?>
It showing the following error:
Fatal error: Call to undefined function ssh2_connect() in /home/chaosnz/public_html/fotosnap.net/test.php on line 2
How can I deal with this?
Thanks

I have installed the SSH2 PECL extension and its working fine thanks all for you help...

Honestly, I'd recommend using phpseclib, a pure PHP SSH2 implementation. Example:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
It's a ton more portable, easier to use and more feature packed too.

I have solved this on ubuntu 16.4 PHP 7.0.27-0+deb9u and nginx
sudo apt install php-ssh2

You need to install ssh2 lib
sudo apt-get install libssh2-php && sudo /etc/init.d/apache2 restart
that should be enough to get you on the road

If you are running a bomebrew on OSX, I used the following to install it:
brew install php56-ssh2
That worked for me. I pulled it from here. There should also be Ubuntu and OSX using mac port as well.

I am running CentOS 5.6 as my development environment and the following worked for me.
su -
pecl install ssh2
echo "extension=ssh2.so" > /etc/php.d/ssh2.ini
/etc/init.d/httpd restart

To expand on #neubert answer, if you are using Laravel 5 or similar, you can use phpseclib much simpler like this:
Run composer require phpseclib/phpseclib ~2.0
In your controller add
use phpseclib\Net\SSH2;
Then use it in a controller method like:
$host = config('ssh.host');
$username = config('ssh.username');
$password = config('ssh.password');
$command = 'php version';
$ssh = new SSH2($host);
if (!$ssh->login($username, $password)) {
$output ='Login Failed';
}
else{
$output = $ssh->exec($command);
}

For today pecl install ssh2 requires old versions of php (<=6.0). Here https://pecl.php.net/package/ssh2 you can see the latest beta versions of ssh2 supporting php7 and php8. So you should install one of them:
pecl install ssh2-1.3.1
* Do not forget to enable this ssh2.so extension in your php.ini file.

For WHM Panel
Menu > Server Configuration > Terminal:
yum install libssh2-devel -y
Menu > Software > Module Installers
PHP PECL Manage Click
ssh2 Install Now Click
Menu > Restart Services > HTTP Server (Apache)
Are you sure you wish to restart this service?
Yes
ssh2_connect() Work!

I know there are answers but I am just simplifying the answer here.
I have same issue and I fixed it with below solution in ubuntu 20:
sudo apt-get install libssh2-1 php7.1-ssh2 -y
you can change the php version as per your need like: php7.4-ssh2
Ref: https://blog.programster.org/ubuntu-16-04-install-php-ssh2-extension

Related

Error. Cannot connect to database: SQLSTATE[HYT00]

I am getting the following error when trying to connect to my Microsoft SQL Server database using the default port number:
Error. Cannot connect to database: SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
This is the PHP code I am using to connect to the database:
<?php
class DB_Connect {
private $db;
function construct() {
}
public function connect() {
require_once 'String.php';
try {
$this->db = new PDO('sqlsrv:Server=$server,1433; Database=$db', $user, $pass);
} catch (PDOException $e) {
return "Error. Cannot connect to database: " . $e->getMessage();
}
}
}
?>
I am 100% sure that the credentials are correct since they are workign when I run the script on localhost using xampp.
What I have done till now:
Installed the pdo drivers on the linux machine
Installed the obdc drivers on the linux machine
Installed the sqlsrv drivers on the linux machine
This is the configuration I am using on the server:
PHP Version 7.0.30 connecting to SQL Server 2017 hosted on Gearhost.
Can anyone please shed a light on what might be wrong?
Basically the issues were 2:
First of all, the drivers were not installed correctly. I needed to install the drivers through PECL. This is how I installed them on Redhat Cent OS 7:
Install PHP:
sudo su
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm
subscription-manager repos --enable=rhel-7-server-optional-rpms
yum-config-manager --enable remi-php72
yum update
yum install php php-pdo php-xml php-pear php-devel re2c gcc-c++ gcc
Install Prerequisites
sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
sudo yum install devtoolset-7
scl enable devtoolset-7 bash
Install PHP Drivers for SQL Server
pecl download sqlsrv
tar xvzf sqlsrv-5.2.0.tgz
cd sqlsrv-5.2.0/
phpize
./configure --with-php-config=/usr/bin/php-config
make
sudo make install
Install Apache and restart the service
sudo yum install httpd
sudo apachectl restart
--EDIT--
You can alternatively download the prebuilt binaries from the Github project page, or install from the Remi repo:
sudo yum install php-sqlsrv php-pdo_sqlsrv
In my case I also had to open the firewall connection from the server the database. You check if this is necessary, run sqlcmd from Terminal to connect to the database. If you get a network error with the correct credentials, then you know you've got a firewall problem.
Hope this helps anyone who is struggling connecting to a Linux server to SQL Server.
Can you use this here?
try{
$db = new PDO("sqlsrv:Server,1433=$server;dbname=$db;charset=utf8","$user","$pass");
$query=$db->prepare("Some SQL String Here");
$query->execute();
}catch(PDOException $e ){
echo "Error: ".$e;
}

Install sqlite for php5.6

The php manpage sais, you have to install sqlite via PECL, so I tried the latest version:
cd /tmp/
wget https://pecl.php.net/get/SQLite-1.0.3.tgz
tar xvzf SQLite-1.0.3.tgz
cd SQLite-1.0.3/
phpize
But I cannot install php_sqlite on Ubuntu 15.10 like this. I get an error at make :
/tmp/SQLite-1.0.3/sqlite.c: In function 'zif_sqlite_open':
/usr/include/php5/main/php_globals.h:32:29: error: 'struct _php_core_globals' has no member named 'safe_mode'
And many more, so is there an actual version working on PHP 5.6?
I want to run an old program that uses the function sqlite_open
And installing the package didn't make it work:
apt-get install php5-sqlite
So maybe there's a good wrapper library around that uses SQLite3::open to simulate the old functions?
I've been wracking my brain for a couple of days on this, but this finally worked for me:
sudo apt-get install php5.6-sqlite3
I have php5.6 as an executable so I then confirmed that php5.6 -m showed sqlite3 to be loaded and it was.
After that, I was able to execute the following simple script. Previously it was telling me that the class SQLite3 was not found.
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
As far as I can see the pecl page shows that package was last updated in 2004. The link you linked also states: "(PHP 5 < 5.4.0, PECL sqlite >= 1.0.0)". Simply said It's not for 5.6.
Regardless you should just install the default package instead of using pecl when this is an option. For ubuntu this is php5-sqlite.
The extension you linked is very old. I would recommend taking a look at SQLite3 which should be included by default.

ssh2_connect is undefined on debian

i have several server in debian 6.0.8 with php version at 5.3.28-1~dotdeb.0.
I have made an apt-get install libssh2-php on each.
When u made an php -v, i have openssl and ssh2 in the list on each.
I have already restarted apache after that.
But when i run a php script with ssh2_connect() call, it doesnt' work on 2 servers but it works on the other server.
I have the error : "call to undefined function ssh2_connect()."
I don't understand, debian version and php version are the same.
Can you help me ?
Thanks.
In packaged distros like Debian and EL you have the "non-core" parts of PHP packaged separately. In Debian flavors you have to have the php5-ssh2 package installed.
sudo apt-get install php5-ssh2
I'd just use phpseclib, a pure PHP SSH implementation. eg.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
phpseclib has a number of a advantages over libssh2. You can also use phpseclib to emulate the libssh2 functions, if you're so inclined. eg.
https://github.com/phpseclib/libssh2-compatibility-layer

Installing SSH2 for PHP to run on a Xampp Apache server on a OSX 10.9.1

I am running Xampp on a Mac with OS X 10.9.1 and I am trying to develop a web page that calls a PHP script through an Ajax call. Xampp is used here for development purposes and it is not intended to form the backend of an online page.
A PHP script needs to connect though ssh to a remote machine, reason for which I
plan to use the ssh2 library. When I try to run the commands:
$connection = ssh2_connect('my.remote.machine.address', 5432);
I get the following error:
Fatal error: Call to undefined function ssh2_connect() in /Applications/XAMPP/xamppfiles/htdocs/Project/getdata.php on line 8
and, by calling 'phpinfo()', ssh2 does not seem to be installed. I successfully managed to use ssh2_connect on Windows, Ubuntu and Scientific Linux and after a week I can't seem to figure out what I'm not doing right on a OSX I suspect there could be a problem with broken paths or interaction with the Apache server OSX came with. Here is what I have tried:
I. The MacPorts and PECL method (reference: http://lassebunk.dk/2011/05/26/installing-ssh2-for-php-on-mac-os-x/):
stuff already on my computer: xcode (from iStore), MacPorts (installer from their site), PHP 5.4.17 and apparently an Apache server that comes with OSX, that I had to stop with:
sudo apachectl stop
so that the Xampp one worked.
ran the command:
sudo port install libssh2
as the libssh2 and openssl libraries are prerequisites to using ssh2_connect in php (reference: http://www.php.net/manual/en/ssh2.requirements.php); openssl appears to be already installed on Xampp's according to phpinfo().
installed Pear:
sudo php /usr/lib/php/install-pear-nozlib.phar
installed Homebrew (otherwise the 'pecl install' commands fail):
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
ran:
brew install autoconf
now I was finally able to run:
$ sudo pecl install ssh2-0.12
with "/opt/local" as prefix
now I got a nice ssh2.so file, which I add to the relevant php.ini:
/Applications/XAMPP/xamppfiles/etc
by adding the line:
extension=/opt/local/lib/php54/extensions/no-debug-non-zts-20100525/ssh2.so
restarted the Xampp Apache Web Server from the GUI that comes with Xampp and still ssh2 was not found.
II. The compile the ssh2.so from the terminal method (reference: zizifu's comment http://www.php.net/manual/en/ssh2.installation.php):
got the libssh2 archive from http://libssh2.org/ and:
./configure
make
make install
got the latest ssh2 archive from http://pecl.php.net/package/ssh2
phpize
./configure --with-ssh2
make
make install
got a ssh2.so file again, added it to the relevant php.ini, restarted server and it still doesn't work.
Sorry if this has too much detail, hopefully the information I gave can point what I'm doing wrong; I would really appreciate any solution/hint/thing to try to solve this issue.
Have you considered phpseclib, a pure PHP SSH2 implementation? It has a number of advantages over libssh2, including improved portability and speed. Example:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>

SSH2 PHP Extension

I have been trying for a while to get the SSH 2 extension for php installed on my CentOS 5.6 x64 server. I started the server from scratch and here are the steps I have taken
install and configure APF
install apache
install php
install mysql
add epel repository
install php-pear so I can use pecl
run yum install libssh2-devel
run pecl install ssh2-beta
echo extension=ssh2.so>>/etc/php.ini
(There were a few extra things in there such as installing phpMyadmin and wordpress)
After this I run php -m to see the php modules and ssh2 is not listed. I have been searching forever for tutorials and have found a few but from what I can see I have done everything correctly. Can anyone see where I am going wrong. I can provide any config files you may need. Also I have hear about phpseclib and was wondering if this would be an easier/better route? Any help would be greatly appreciated!
You could maybe try phpseclib, a pure PHP SFTP implementation, instead. eg.
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
exit('Login Failed');
}
echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
Make sure /etc/php.ini is actually used. check php --info output to see the used ini file(s)
At least on debian the path would be /etc/php5/cli/php.ini
Maybe there's no difference, but if not already like this, try:
extension="ssh2.so"

Categories