开发者

can't use php's ssh2_exec to restart nginx or get the nginx version

开发者 https://www.devze.com 2023-02-12 06:15 出处:网络
I have installed the php extension ssh2 and it work fine. but when I want use it to get the remote server\'s nginx version or restart the nignx after I change the config file of the remote use the ssh

I have installed the php extension ssh2 and it work fine. but when I want use it to get the remote server's nginx version or restart the nignx after I change the config file of the remote use the ssh2 too, It don't work as I wish.

my php code is like this:

 <?php 
 $connection = ssh2_connect('115.238.73.136', '65000', array(开发者_JAVA技巧'hostkey'=>'ssh-rsa'));
   if(!$connection) "error_1\n";
     if(ssh2_auth_pubkey_file($connection, 'root','/data/web/control_center/sh/.ssh/id_rsa.pub','/data/web/control_center/sh/.ssh/id_rsa', 'test')){
    $stream = ssh2_exec($connection, "/usr/local/nginx/sbin/nginx -v");
    stream_set_blocking($stream, true);
    while( !feof($stream) ) {
            $cmd[] = fgets($stream);
    }
    var_dump($cmd);
   }

there is nothing in the $cmd. and I change the "

/usr/local/nginx/sbin/nginx -v 

to

/usr/local/nginx/sbin/nginx -t; 
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`;

and I can't get anything too. I guess the

/usr/local/nginx/sbin/nginx 

can't be execute by the


My recommendation would be to stop using libssh2 and start using phpseclib, a pure PHP SSH implementation.

ssh2_exec() (using php5 ssh ext) sometimes never returns output, and sometimes it does! This is with setting blocking to true, and even adding wait periods, which is a no no. Try connection to LOCALHOST and sometimes you get output, sometimes not. Im running Karmic Ubuntu, and for the life of me pissed me off to no end. phpseclib, however, works perfect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号