请先安装lamp之后才能继续: http://blog.csdn.net/zph1234/article/details/51248124
0.这里是centos,如果是windows环境安装扩展就更简单了,下载dll文件,在php.ini中打开就行了,记得关闭selinux和firewalld
1.yum install libssh2 libssh2-devel php-devel gcc-c++
2.下载ssh2扩展,从http://pecl.php.net/package/ssh2
3.如果是php5安装ssh2-0.13扩展,php7安装1.0
cd ssh2-0.13/
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config LIBS=-ldl
make
make install
4. vi /etc/php.ini
加入extension=ssh2.so
5.重启apache
6.vi test.php
- <?php
- $host='127.0.0.1';
- $user='root';
- $passwd='123456';
- // 链接远程服务器
- $connection = ssh2_connect($host, 22);
- if (!$connection) die('connection to '.$host.':22 failed');
- echo 'connection OK<br/>';
- // 获取验证方式并打印
- $auth_methods = ssh2_auth_none($connection, $user);
- print_r( $auth_methods.'<br/>');
- if (in_array('password', $auth_methods ))
- {
- // 通过password方式登录远程服务器
- if (ssh2_auth_password($connection, $user, $passwd))
- {
- echo $user.' login OK<br/>';
- $stream = ssh2_exec($connection, "pwd"); // 执行php
- stream_set_blocking($stream, true); // 获取执行pwd后的内容
- if ($stream === FALSE) die("pwd failed");
- echo 'pwd: '.stream_get_contents($stream).'<br/>';
- }
- else
- {
- die( $user.' login Failed<br/>');
- }
- }