phpseclib read()接受上一个命令输出

时间:2016-03-18 07:48:11

标签: php phpseclib

我使用phpseclib将文件从远程主机复制到localhost。在我继续前进之前,我会使用交互式shell,以便我能够理解这种行为。我对当前代码中的行为感到困惑,似乎它的read()函数也显示了上一个命令的结果。见下文:

if (!$ssh->login("myusername", "mypwd")) { 
    exit('Login Failed');
}

$ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); 
$ssh->write("cd /tmp; ls *.csv1\n");
$output = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$ansi->appendString($output);
echo $output = htmlspecialchars_decode(strip_tags($ansi->getScreen()));

$ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); 
$ssh->write("cd /tmp; ls *.txt\n");
$txt_output = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$ansi->appendString($txt_output);
echo $txt_output = htmlspecialchars_decode(strip_tags($ansi->getScreen()));

以下是输出:

cd /tmp; ls *.csv1 ls: *.csv1: No such file or directory [user@host tmp]#

cd /tmp; ls *.csv1 ls: *.csv1: No such file or directory [user@host tmp]#cd /tmp; ls *.txt ls: *.txt: No such file or directory [user@host tmp]#

在我看来,当我的代码处理第二个命令时,它也将采用第一个命令。如何以新模式执行第二个命令?感谢您的回复,谢谢。

2 个答案:

答案 0 :(得分:0)

所以经过一段时间玩代码后,我开始意识到一种模式。在我看来,每次我想要使用它时我都需要重新初始化ANSI

$ansi = new File_ANSI(); // <== NOTE THIS LINE
$ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); 
$ssh->write("cd /tmp; ls *.csv1\n");
$output = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$ansi->appendString($output);
echo $output = htmlspecialchars_decode(strip_tags($ansi->getScreen()));

$ansi = new File_ANSI(); // <== INITIALIZE AGAIN TO OUTPUT
$ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); 
$ssh->write("cd /tmp; ls *.txt\n");
$txt_output = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
$ansi->appendString($txt_output);
echo $txt_output = htmlspecialchars_decode(strip_tags($ansi->getScreen()));

输出结果如下,你可以随心所欲:

cd /tmp; ls *.csv1 ls: *.csv1: No such file or directory [user@host tmp]#

cd /tmp; ls *.txt ls: *.txt: No such file or directory [user@host tmp]#

希望这会帮助那里的人。

答案 1 :(得分:0)

试试type: web$ansi->loadString()附加,顾名思义。