php ssh connection phpseclib

ぃ、小莉子 提交于 2020-01-11 13:01:31

问题


System: Linux
Server: XAMPP
Goal:
ssh-connection to a server (later: doing some stuff on this server, not part of this question)

Test-Code:

 <?php  
    set_include_path(get_include_path().PATH_SEPARATOR.'/home/myusername');
    include('Net/SSH2.php'); 
    $ssh = new Net_SSH2('123.45.6.78'); 
    if (!$ssh->login('user', 'password')) { 
    exit('Login Failed'); 
    }else{ 
    echo "connected".'<br>'; 
    echo $ssh->exec('whoami').'<br>';
    echo $ssh->exec('hostname')).'<br>';
    }   
    ?>

Output:

connected
(M4300-28G-PoE+) >
(M4300-28G-PoE+) >

Problem:
I do not get any errors (neither in the output of the website (see above) nor in /opt/lampp/logs/error_logs), so:
Question(s):
Why am I not getting any output (user, hostname)? Are there other/better ways to check whether I am properly connected?


回答1:


It looks like you are getting output. (M4300-28G-PoE+) > is the output you're getting and you're getting it for both of the commands you're trying to run.

M4300-28G-PoE+ is a NetGear switch. Switches are fairly notorious for not having full SSH implementations.

My guess: if you want to run commands you'll have to use phpseclib's interactive commands:

echo $ssh->read('username@username:~$');
$ssh->write("ls -la\n"); // note the "\n"
echo $ssh->read('username@username:~$');


来源:https://stackoverflow.com/questions/55598782/php-ssh-connection-phpseclib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!