Execute a shell command through ssh using PHP

后端 未结 3 1034
逝去的感伤
逝去的感伤 2020-12-17 07:10

I\'m trying to execute a simple shell command and print the result on a web page but the results are empty. Below is one bit of code I found but nothing has worked thus far.

3条回答
  •  -上瘾入骨i
    2020-12-17 07:58

    Using a more object oriented solution, you can install phpseclib version 2 with:

    composer require phpseclib/phpseclib
    

    And then just create your ssh object:

    $ssh = new SSH2('yourhost');
    if (!$ssh->login('username', 'password')) {
        exit('Login Failed');
    }
    

    In this exemple i have used a connection through username and password but you can also connect via ssh-keys. If the connection is successful you can execute the method exec to execute you command on the server.

提交回复
热议问题