PHP安裝ssh2擴展

安裝

下載包

$ wget http://www.libssh2.org/download/libssh2-1.4.2.tar.gzphp

$ wget http://pecl.php.net/get/ssh2-0.12.tgzssh

先安裝 libssh2 再安裝 SSH2

$ tar -zxvf libssh2-1.4.2.tar.gz.net

$ cd libssh2-1.4.2調試

$ ./configure --prefix=/usr/local/libssh2code

$ make && make installget

編譯安裝ssh2

$ tar -zxvf ssh2-0.12.tgzcmd

$ cd ssh2-0.12it

$ /usr/local/zend/bin/phpizeio

$ ./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2 --with-php-config=/usr/local/zend/bin/php-config編譯

$ make && make install

修改php.ini 加入

extension=ssh2.so

重啓PHP

調試

用戶名密碼方式登陸
$user="root";//遠程用戶名
$pass="******";//遠程密碼
$connection=ssh2_connect('10.10.10.10',22);
ssh2_auth_password($connection,$user,$pass);
用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');
}
執行命令獲取返回值
$cmd="ps aux";//命令
$ret=ssh2_exec($connection,$cmd);
stream_set_blocking($ret, true);
echo (stream_get_contents($ret));
相關文章
相關標籤/搜索