linux php5.6 安装ssh2拓展包 连接服务器
发布日期:2021-11-18 19:17:27 浏览次数:9 分类:技术文章

本文共 3272 字,大约阅读时间需要 10 分钟。

需求:需要将一些材料文件上传至接收方的服务器

接收方已提供 主机:merchant-sftp.*****.com 端口:2**2 协议:SFTP 用户名:商编 密钥:密钥文件 密码:不需要密码

思路:php可使用 ssh2_auth_password($connect, $username, $passport) 和 ssh2_auth_pubkey_file($connect, $username, $pubKey, $priKey) 实现连接服务器,此处对方要求使用第二个方法实现,但却只有提供私钥,因此还需用私钥生成对应的公钥

 

一、根据私钥生成公钥(java实现连接只需私钥即可,无需公钥,本次不做java实例)

1.图片展示在xshell如何生成公钥

2.php代码实现生成公钥 

/** * 生成公钥 * @param $priKeyPath 私钥格式:-----BEGIN RSA PRIVATE KEY----- MII****JSiQ==  -----END RSA PRIVATE KEY----- */public function pubKey($priKeyPath){    if (!file_exists($privatePath)) {        throw new Exception('私钥文件不存在');    }    $keyId   = openssl_get_privatekey(file_get_contents($privatePath));    $details = openssl_pkey_get_details($keyId);    $buffer  = pack("N", 7) . "ssh-rsa" . $this->sshEncodeBuffer($details['rsa']['e']) . $this->sshEncodeBuffer($details['rsa']['n']);    $pubKey  = "ssh-rsa " . base64_encode($buffer);    return $pubKey;}/** * 将N和e的第一个字符与0x80进行"与"运算,如果匹配,则在数字的开头添加另一个NULL字符,并将大小分别增加1 * @param $buffer * @return false|string  公钥格式如图片所示,ssh-rsa***** */function sshEncodeBuffer($buffer){    $len = strlen($buffer);    if (ord($buffer[0]) & 0x80) {        $len++;        $buffer = "\x00" . $buffer;    }    return pack("Na*", $len, $buffer);}

二、连接服务器

/* * Sftp操作类 * @note : 该拓展必须开启ssh2拓展 */class SftpHelper{    // 初始配置为NULL    private $config = NULL;    // 连接为NULL    private $conn = NULL;    // 是否使用秘钥登陆    private $use_pubkey_file = true;    /*     * 初始化     */    public function init($config)    {        $this->config = $config;    }    /*     * 连接     */    public function connect()    {        $methods = [];        if ($this->use_pubkey_file) {            $methods['hostkey'] = 'ssh-rsa';        }        $this->conn = ssh2_connect($this->config['host'], $this->config['port'], $methods);        if ($this->use_pubkey_file) {            // (1) 使用秘钥的时候            $rc = ssh2_auth_pubkey_file($this->conn, $this->config['user'], $this->config['pubkey_file'], $this->config['privkey_file']);        } else {            // (2) 使用登陆用户名字和登陆密码            $rc = @ssh2_auth_password($this->conn, $this->config['user'], $this->config['passwd']);        }        return $rc;    }    /*     * 其余操作方法,如下载ssh2_scp_recv;上传ssh2_scp_send;删除ssh2_sftp_rmdir和ssh2_sftp_unlink等等方法     */    public function doAnythings() {// you can do anything;}}$handle = new SftpHelper();$handle->init([    'host'         => empty($conf['host']) ? 'merchant-sftp.*****.com' : $conf['host'],    'port'         => empty($conf['port']) ? '2**2' : $conf['port'],    'user'         => '8*******9',    'pubkey_file'  => './601a*****293b.pub',    'privkey_file' => './601a*****293b.pri',    'passphrase'   => '',]);$handle->connect();

 

附录:

一、ssh2拓展包的安装

1、拓展下载地址:

下载:wget http://pecl.php.net/get/ssh2-0.13.tgz (鼠标右击“ssh2-0.13.tgz(28.3KB)”链接,复制出链接地址即可)

解压:tar -xvzf ssh2-0.13.tgz

进入:cd  ssh2-0.13

/usr/local/php5.6/bin/phpize (自己服务php对应的目录,我的是lnmp一键安装包,默认地址是/usr/local/php5.6)

./configure --with-php-config=/usr/local/php5.6/bin/php-config (当提示缺少libssh2,则证明服务器缺少libssh2依赖,可编译安装或yum安装,此处使用 yum install -y libssh2-devel,安装依赖后再次执行configure)

make && make install  (安装完成,系统会提示安装目录在哪里,去对应目录查看是否有 ssh2.so文件)

php.ini 添加  extension=提示的安装目录/ssh2.so

此时如果是浏览器执行phpinfo(),则需要重启下php-fpm

ps -ef | grep php-fpm 找到master进程id    kill -USR2 pid 重启fpm服务

完成

 

 

 

 

 

 

转载地址:https://blog.csdn.net/weixin_39177464/article/details/113630105 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PHP return 返回浮点数,却出现多位的解决方案
下一篇:linux连接ftp

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月07日 17时00分15秒