SSH2 扩展的安装
PHP 的 SSH2 扩展
系统环境
- CentOS 6.8
- libssh2-1.8.0
- php-devel
- ssh2-0.13
前提环境安装好 LNMP 或 LAMP
-
安装 OpenSSL
$ yum install -y openssl openssl-devel
-
编译安装 libssh2
$ cd /usr/local/src $ wget https://www.libssh2.org/download/libssh2-1.8.0.tar.gz $ tar xvf libssh2-1.8.0.tar.gz $ cd libssh2-1.8.0 $ ./configure --prefix=/usr/local/libssh2 $ make && make install
-
编译安装 ssh2
$ cd /usr/local/src/ $ wget http://pecl.php.net/get/ssh2-0.13.tgz $ tar xvf ssh2-0.13.tgz $ cd ssh2-0.13 $ phpize ##没有的话yum install php-devel ubuntu apt-get install php7.0-dev $ ./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2 $ make
-
拷贝 ssh2.so
在当前目录/modules下会生成一个 $ cp modules/ssh2.so /usr/lib64/php/modules/
-
修改 php 配置文件
$ echo "extension=ssh2.so" >> /etc/php.ini
-
查看是否成功
$ php -m | grep ssh2 ssh2
SSH2 模块的连接应用
““
<?php
//传送文件到KVM虚拟机
$host = '192.168.1.213';
$instance = 'instance-000004ed';
$src_dir = '/usr/local/src/test.txt';
$dest_dir = '/home/';
$password = '123456';
$conn = ssh2_connect($host,22);
if(!ssh2_auth_password($conn,"root",$password)){
die('Authentication Failed...');
}
//$cmd = "virt-copy-in -d $instance $src_dir $dest_dir";
$cmd = 'virt-ls instance-000004d /etc';
$stream = ssh2_exec($conn , $cmd);
//输出错误信息
$errorStream = ssh2_fetch_stream($stream,SSH2_STREAM_STDERR);
stream_set_blocking($stream,true);
stream_set_blocking($errorStream,true);
echo (stream_get_contents($stream));
echo (stream_get_contents($errorStream));
fclose($stream);
fclose($errorStream);
版权声明:本文为u011198997原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。