安装ssh2
php的一个扩展ssh2. 下载ssh2扩展,从http://pecl.php.net/package/ssh2
如果是php5安装ssh2-0.13或0.12扩展,php7安装1.0
wget http://pecl.php.net/get/ssh2-0.13.tgz
tar -zxvf ssh2-0.13.tgz
cd ssh2-0.13//usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config LIBS=-ldl
(如果失败,请安装 yum install libssh2-devel)
make
make install
测试
- <?php
- $host='127.0.0.1';//被控制的linux的ip
- $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/>');
- }
- }