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

PHP install ssh2 extension

藏色散人
藏色散人forward
2019-11-05 13:37:284480browse

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

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

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

Modify php.ini and add

extension=ssh2.so

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

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');
}

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

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete