dream198731 2014-10-17 06:21
浏览 79

如何格式化PHP SSH2 read()输出?

I've been trying to format the output of SSH2->read() method. I tried using ANSI.php following http://phpseclib.sourceforge.net/ssh/examples.html#top

My code

<?php
include('Net/SSH2.php');
include('File/ANSI.php');
$ssh = new Net_SSH2('10.106.240.212');
$ssh->login('Administrator', 'Nbv12345') or die("Login failed");
$ansi = new File_ANSI();

$ssh->enablePTY();
$ssh->exec("powershell.exe
");
$ssh->setTimeout(2);
echo $ssh->read();
$ssh->write("ls
");
echo $ssh->read();
?>

The output is full of ANSI characters and no 'ls' results

(B)0[?7l[H[J[1;1HWindows PowerShell[2;1HCopyright (C) 2013 Microsoft Corporation. All rights reserved.[4;1HPS C:\Users\Administrator> (B)0[?7l[H[J[1;1H [2;1H [3;1H [4;1H [5;1H [6;1H [7;1H [8;1H [9;1H [10;1H [11;1H [12;1H [13;1H [14;1H [15;1H [16;1H [17;1H [18;1H [19;1H [20;1H [21;1H [22;1H [23;1H [24;1H [1;1HMicrosoft Windows [Version 6.3.9600][2;1H(c) 2013 Microsoft Corporation. All rights reserved.[4;1HC:\Users\Administrator>ls[5;1H

I need something like this, Its not even close:enter image description here

How can I format this ?

  • 写回答

1条回答 默认 最新

  • duanhuanzhi6431 2014-10-17 13:10
    关注

    Just because you're initializing the class doesn't mean you're including it lol. Try this:

    <?php
    include('Net/SSH2.php');
    include('File/ANSI.php');
    $ssh = new Net_SSH2('10.106.240.212');
    $ssh->login('Administrator', 'Nbv12345') or die("Login failed");
    $ansi = new File_ANSI();
    
    $ssh->enablePTY();
    $ssh->exec("powershell.exe
    ");
    $ssh->setTimeout(2);
    $ansi->appendString($ssh->read());
    $ssh->write("ls
    ");
    $ansi->appendString($ssh->read());
    echo $ansi->getScreen();
    ?>
    

    ie. wrap all your $ssh->read() calls in $ansi->appendString() and then do $ansi->getScreen(). Or maybe only wrap the $ssh->read() after $ssh->write("ls ") in $ansi->appendString(). You could do $ansi->getHistory() too.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿