ssh2 extension to perform remote login commands and execute multiple commands

Install ssh2 extension

1. Install libssh2

download the libssh2 package from http://libssh2.org,command as following

wget https://libssh2.org/download/libssh2-1.8.0.tar.gz
tar vxzf libssh2-1.8.0.tar.gz
cd libssh2-1.8.0
./configure --prefix=/usr/local/libssh2
make && make install

We should remember libssh2 installation directory, as will be used in the installation of ssh2.

2. Install ssh2

download the php-ssh2 package from http://pecl.php.net/package/ssh2

wget http://pecl.php.net/get/ssh2-1.1.2.tgz
tar vxzf ssh2-1.1.2.tgz
cd ssh2-1.1.2
phpize
./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2(libssh2的路径)
make && make install

3. Modify the configuration file php.ini

#查找php.ini位置
sudo find / -name 'php.ini'  

Php.ini file edit, modify the configuration following table entry value, restart php
add "extension = / usr / lib64 / php / modules / ssh2.so" to php.ini

vi / vim editor, search for the keyword
/ keyword, press Enter. This is a document from the current location down to find keywords, press n to find the next key position;
? Keywords, press Enter. This is up from the document lookup key ring position, press n to find keywords upward;

4. Restart PHP

systemctl start php-fpm.service

At this point SSH2 extension for PHP has been completed, in order to verify that the installation is successful, we can verify by executing the following command.

php -i|grep ssh2
#结果如下:
Registered PHP Streams => php, file, http, ftp, compress.bzip2, compress.zlib, https, ftps, ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp  
ssh2
libssh2 version => 1.4.2
banner => SSH-2.0-libssh2_1.4.2

Other configurations

Edit /etc/php.ini file, the following table to modify the value of configuration items, restart php.
php.ini
Name Value Comment
maximum execution time max_execution_time 1800 each script, in seconds
date.timezone "Asia / Shanghai" timezone
session.gc_maxlifetime 10800 sessions maximum life time (in seconds)
pdo_mysql.default_socket /tmp/mysql.sock

 $connection = ssh2_connect(HOST,PORT);
        if(!ssh2_auth_password($connection,USER,PASSWORD)){
            $this->error('服务器的账号或密码错误');
        }
        //执行的命令
        $stream = ssh2_exec($connection, 'python '.TOP_PAY_PATH.');
        stream_set_blocking( $stream, true );//阻塞模式
        $output = '<pre>'.stream_get_contents($stream).'</pre>';
        $this->assign('output',$output);
        fclose($stream);

Guess you like

Origin blog.csdn.net/qq_41049126/article/details/90476482