douyuben9434 2019-04-09 18:08
浏览 126

php ssh连接phpseclib

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条回答 默认 最新

  • drtwqc3744 2019-04-10 04:02
    关注

    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
    "); // note the "
    "
    echo $ssh->read('username@username:~$');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable