Home > Backend Development > PHP Tutorial > PHP install ssh2 extension

PHP install ssh2 extension

藏色散人
Release: 2023-04-07 18:30:02
forward
4459 people have browsed it

Installation

Download package

$ wget http://www.libssh2.org/download/libssh2-1.4.2.tar.gz
$ wget http://pecl.php.net/get/ssh2-0.12.tgz
Copy after login

Install libssh2 first and then SSH2

$ tar -zxvf libssh2-1.4.2.tar.gz
$ cd libssh2-1.4.2
$ ./configure --prefix=/usr/local/libssh2
$ make && make install
Copy after login

Compile and install ssh2

$ tar -zxvf ssh2-0.12.tgz
$ cd ssh2-0.12
$ /usr/local/zend/bin/phpize
$ ./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2 --with-php-config=/usr/local/zend/bin/php-config
$ make && make install
Copy after login

Modify php.ini and add

extension=ssh2.so
Copy after login

Restart PHP

Debug

user Log in with name and password

$user="root";//远程用户名
$pass="******";//远程密码
$connection=ssh2_connect('10.10.10.10',22);
ssh2_auth_password($connection,$user,$pass);
Copy after login

Log in with sshkey

$connection=ssh2_connect('10.10.10.10',22);
if(ssh2_auth_pubkey_file($connection, 'root', '/home/id_rsa.pub', '/home/id_rsa', 'secret'))
{
    echo "Public Key Authentication Successful\n";
} else {    
    die('Public Key Authentication Failed');
}
Copy after login

Execute the command to get the return value

$cmd="ps aux";//命令
$ret=ssh2_exec($connection,$cmd);
stream_set_blocking($ret, true);
echo (stream_get_contents($ret));
Copy after login

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of PHP install ssh2 extension. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template