[英]SSH2 (PHP ext) long command gets malformed. (escaping code issue?)
[1D gets into long commands and break them. [1D进入长命令并打破它们。 I have same issue in phpseclib and SSH2 PHP extension.
我在 phpseclib 和 SSH2 PHP 扩展中有同样的问题。 Putty works without issue.
腻子没有问题。
Here is SSH2 code:这是SSH2代码:
$stream = ssh2_shell($session, "vt100", null, 200, 25, SSH2_TERM_UNIT_CHARS);
stream_set_blocking($stream, true);
usleep(500000);
fwrite($stream, "enable\n");
usleep(500000);
fwrite($stream, "mmi-mode enable\n");
usleep(500000);
fwrite($stream, "aaaa aaaa aaaa aaaa ");
usleep(500000);
fwrite($stream, "aaaa aaaa aaaa aaaa ");
usleep(500000);
fwrite($stream, "aaaa aaaa aaaa aaaa ");
usleep(500000);
fwrite($stream, "aaaa aaaa aaaa aaaa ");
usleep(500000);
fwrite($stream, "aaaa aaaa aaaa aaaa \n"); //does not matter if i write command over multiple writes
usleep(500000);
echo nl2br(fread($stream, 8192));
fclose($stream);
Response from server:来自服务器的响应:
MA5683T>enable
MA5683T#mmi-mode enable
MA5683T#aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aa [1Daa aaaa aaaa aaaa aaaa aaaa [1D
Added phpseclib code:添加了 phpseclib 代码:
$ssh = new \phpseclib3\Net\SSH2($ip, 22, 1);
if (!$ssh->login($login, $password)) {
throw new \Exception('Login failed');
}
$ssh->setTerminal("VT100");
$ssh->setWindowColumns(200);
$ssh->write("enable\n");
$ssh->write("mmi-mode enable\n");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa \n");
echo nl2br($ssh->read());
echo $ssh->getLog();
$ssh->disconnect();
Phpseclib output: Phpseclib output:
MA5683T>enable
MA5683T#mmi-mode enable
MA5683T#aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aa [1Daa aaaa aaaa aaaa aaaa aaaa [1D
Phpseclib log: phpseclib 日志:
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000 00:00:00:01:00:00:00:07:65:6e:61:62:6c:65:0a ........enable.
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:01:00:00:00:10:6d:6d:69:2d:6d:6f:64:65 ........mmi-mode
00000010 20:65:6e:61:62:6c:65:0a enable.
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:01:00:00:00:14:61:61:61:61:20:61:61:61 ........aaaa aaa
00000010 61:20:61:61:61:61:20:61:61:61:61:20 a aaaa aaaa
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:01:00:00:00:14:61:61:61:61:20:61:61:61 ........aaaa aaa
00000010 61:20:61:61:61:61:20:61:61:61:61:20 a aaaa aaaa
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:01:00:00:00:14:61:61:61:61:20:61:61:61 ........aaaa aaa
00000010 61:20:61:61:61:61:20:61:61:61:61:20 a aaaa aaaa
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:01:00:00:00:14:61:61:61:61:20:61:61:61 ........aaaa aaa
00000010 61:20:61:61:61:61:20:61:61:61:61:20 a aaaa aaaa
-> NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:01:00:00:00:15:61:61:61:61:20:61:61:61 ........aaaa aaa
00000010 61:20:61:61:61:61:20:61:61:61:61:20:0a a aaaa aaaa .
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.021, network: 0s)
00000000 00:00:00:02:00:00:00:01:08 .........
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0s)
00000000 00:00:00:02:00:00:00:04:0a:0d:0a:0d ............
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0, network: 0s)
00000000 00:00:00:02:00:00:00:0c:0d:0a:0d:0a:4d:41:35:36 ............MA56
00000010 38:33:54:3e 83T>
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0706, network: 0s)
00000000 00:00:00:02:00:00:00:01:65 ........e
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000 00:00:00:02:00:00:00:01:6e ........n
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.009, network: 0s)
00000000 00:00:00:02:00:00:00:01:61 ........a
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0008, network: 0s)
00000000 00:00:00:02:00:00:00:01:62 ........b
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0093, network: 0s)
00000000 00:00:00:02:00:00:00:01:6c ........l
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0s)
00000000 00:00:00:02:00:00:00:01:65 ........e
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0298, network: 0s)
00000000 00:00:00:02:00:00:00:0c:0d:0a:0d:0a:4d:41:35:36 ............MA56
00000010 38:33:54:23 83T#
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000 00:00:00:02:00:00:00:01:6d ........m
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0095, network: 0s)
00000000 00:00:00:02:00:00:00:01:6d ........m
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0008, network: 0s)
00000000 00:00:00:02:00:00:00:01:69 ........i
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0092, network: 0s)
00000000 00:00:00:02:00:00:00:01:2d ........-
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0s)
00000000 00:00:00:02:00:00:00:01:6d ........m
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0088, network: 0s)
00000000 00:00:00:02:00:00:00:01:6f ........o
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0s)
00000000 00:00:00:02:00:00:00:01:64 ........d
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0096, network: 0s)
00000000 00:00:00:02:00:00:00:01:65 ........e
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.1, network: 0s)
00000000 00:00:00:02:00:00:00:01:20 ........
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0102, network: 0s)
00000000 00:00:00:02:00:00:00:01:65 ........e
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0003, network: 0s)
00000000 00:00:00:02:00:00:00:01:6e ........n
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0266, network: 0s)
00000000 00:00:00:02:00:00:00:01:61 ........a
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0s)
00000000 00:00:00:02:00:00:00:01:62 ........b
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0035, network: 0s)
00000000 00:00:00:02:00:00:00:01:6c ........l
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0s)
00000000 00:00:00:02:00:00:00:01:65 ........e
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0278, network: 0s)
00000000 00:00:00:02:00:00:00:0c:0d:0a:0d:0a:4d:41:35:36 ............MA56
00000010 38:33:54:23 83T#
<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0s)
00000000 00:00:00:02:00:00:00:6d:61:61:61:61:20:61:61:61 .......maaaa aaa
00000010 61:20:61:61:61:61:20:61:61:61:61:20:61:61:61:61 a aaaa aaaa aaaa
00000020 20:61:61:61:61:20:61:61:61:61:20:61:61:61:61:20 aaaa aaaa aaaa
00000030 61:61:61:61:20:61:61:61:61:20:61:61:61:61:20:61 aaaa aaaa aaaa a
00000040 61:61:61:20:61:61:61:61:20:61:61:61:61:20:61:61 aaa aaaa aaaa aa
00000050 20:1b:5b:31:44:61:61:20:61:61:61:61:20:61:61:61 .[1Daa aaaa aaa
00000060 61:20:61:61:61:61:20:61:61:61:61:20:61:61:61:61 a aaaa aaaa aaaa
00000070 20:1b:5b:31:44 .[1D
[1D occurs after 80 characters, which is default value of terminal columns. [1D出现在 80 个字符之后,这是终端列的默认值。 I tried to increase them, but it does not work.
我试图增加它们,但它不起作用。
Hex code of [1D : 1b5b3144 [1D的十六进制代码:1b5b3144
This is how it looks when i run actual command:这是我运行实际命令时的样子:
MA5683T(config-if-gpon-0/0)#ont add 0 sn-auth XXXXXXXXXXXXXXXX omci ont-lineprof [1Dile-id 22 ont-srvprofile-id 0 desc "XXXXXXXXXXXX" [1D
Failure: The line profile does not exist
Parameter should be ont-lineprofile-id instead ont-lineprof [1D ile-id参数应该是ont-lineprofile-id而不是ont-lineprof [1D ile-id
What i am dong wrong?我错了什么? Can i somehow break command into multiple lines before [1D occurs?
我可以在[1D发生之前以某种方式将命令分成多行吗?
Also i am using write instead ssh exec because it is losing connection in both phpseclib and SSH2.我也使用 write 代替 ssh exec,因为它在 phpseclib 和 SSH2 中都失去了连接。
Thanks谢谢
$ssh = new \phpseclib3\Net\SSH2($ip, 22, 1);
if (!$ssh->login($login, $password)) {
throw new \Exception('Login failed');
}
$ssh->setTerminal("VT100");
$ssh->setWindowColumns(200);
$ssh->write("enable\n");
$ssh->write("mmi-mode enable\n");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa \n");
echo nl2br($ssh->read());
echo $ssh->getLog();
$ssh->disconnect();
I'd rewrite that thusly:我会这样重写:
$ssh = new \phpseclib3\Net\SSH2($ip, 22, 1);
if (!$ssh->login($login, $password)) {
throw new \Exception('Login failed');
}
$ssh->setTerminal("VT100");
$ssh->setWindowColumns(200);
$ssh->read('MA5683T>');
$ssh->write("enable\n");
$ssh->read('MA5683T>');
$ssh->write("mmi-mode enable\n");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa ");
$ssh->write("aaaa aaaa aaaa aaaa \n");
echo nl2br($ssh->read());
echo $ssh->getLog();
$ssh->disconnect();
Or something like that.或类似的东西。 I don't have a Huwaii device (I Google'd mmi-mode enable) but the fact that you're writing before you're reading is a bit of a code smell.
我没有 Huwaii 设备(我在 Google 上启用了 mmi 模式),但事实上你在阅读之前就在写作,这有点代码味道。 https://phpseclib.com/docs/commands#gotcha-writing-without-first-reading elaborates.
https://phpseclib.com/docs/commands#gotcha-writing-without-first-reading详细说明。
Then the fact that you're sending another command before having gotten the output o the last command.然后你在得到 output o 最后一个命令之前发送另一个命令。 Another code smell.
另一种代码气味。
idk how mmi-mode enable
is supposed to work. idk
mmi-mode enable
应该如何工作。 Does running that command put you into some sort of console where you can just type whatever and it's echo'd back to you?运行该命令是否会让您进入某种控制台,您可以在其中输入任何内容并回显给您?
https://phpseclib.com/docs/commands#determining-what-to-read-passwd talks about how to use write
/ read
in more detail. https://phpseclib.com/docs/commands#determining-what-to-read-passwd更详细地讨论了如何使用
write
/ read
。 Sure, that example is for exec()
but the same basic principals apply.当然,该示例适用于
exec()
但相同的基本原则适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.