linux下php扩展ssh2的详解

一、php扩展ssh2安装

①安装libssh2:
a.  # cd /home/spider/lamp/
b.  # wget http://www.libssh2.org/download/libssh2-1.2.8.tar.gz

c.  # tar -zxvf libssh2-1.1.tar.gz
d.  # cd libssh2-1.1
e.  # ./configure --prefix=/home/spider/lamp/libssh2
f.  # make all install

Compiling libssh2 PHP Extension

a.  # wget http://pecl.php.net/get/ssh2-0.11.0.tgz
b.  # tar -zxvf ssh2-0.11.0.tgz
c.  # cd ssh2-0.11.0

d. # phpize && ./configure –with-ssh2=/home/spider/lamp/libssh2 && make

cp ./modules/ssh2.so  /usr/lib/php/modules/ssh2.so 

e. cp ./modules/ssh2.so   ../../php5/lib/php/extensions/no-debug-non-zts-20060613/
f.  修改php.ini,添加 extension=ssh2.so

③重启apache服务器

# service httpd restart

二、php ssh2使用示例

function getLogBySSH($location, $username, $userpwd, $cmd) 
{
        $ret = '';
        if (!function_exists("ssh2_connect")) {
                $ret = "function ssh2_connect() doesn't exist.\n";    
                return $ret;
        }

        if (!($conn = ssh2_connect($location, 22))){
                $ret = "fail: unable to establish connection.\n";              
                return $ret;
        }

        if (!ssh2_auth_password($conn, $username, $userpwd)) {
                $ret = "Authentication Fail.\n";               
                return $ret;
        }
       
        if (!($stream = ssh2_exec($conn, $cmd)) ){
                $ret = "ssh2_exec() Fail.\n";             
                return $ret;
        } else {

                stream_set_blocking($stream, true);
               
				//按字节数读取数据
                while($buf = fread($stream, 4096) ){                      
                        $ret .= $buf;
                }
               
				/*
				//按行读取数据
				$nCount = 0;
                while($buf = fgets($stream, 4096) ){
                        $nCount ++;
                        $ret .= $buf;

                        if ($nCount > 100) {
                                break;
                        }
                }
				*/
        }
        fclose($stream);
        return $ret;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值