Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning: Undefined array key 2 in SSH2 initShell #1789

Open
simPod opened this issue Apr 28, 2022 · 9 comments
Open

Warning: Undefined array key 2 in SSH2 initShell #1789

simPod opened this issue Apr 28, 2022 · 9 comments

Comments

@simPod
Copy link
Contributor

simPod commented Apr 28, 2022

Sometimes, I get Warning: Undefined array key 2

$this->server_channels[self::CHANNEL_SHELL],

I don't know why it happens but having an exception would be better, WDYT?

@terrafrost
Copy link
Member

Normally that'd be set here:

switch ($this->channel_status[$channel]) {
case MessageType::CHANNEL_OPEN:
switch ($type) {
case MessageType::CHANNEL_OPEN_CONFIRMATION:
list(
$this->server_channels[$channel],
$window_size,
$this->packet_size_client_to_server[$channel]
) = Strings::unpackSSH2('NNN', $response);

Since it isn't.... can you post your full SSH logs? You can get them by doing define('NET_SSH2_LOGGING', 2) at the top of your file and echo $ssh->getLog() after the error.

Thanks!

@simPod
Copy link
Contributor Author

simPod commented May 16, 2022

There's something wrong with my cisco devices.

I have no problem with having the error but it should be handled with exception rather than php warning.

@terrafrost
Copy link
Member

Can you get me the information I asked for?

Errors and exceptions behave fundamentally differently. Exceptions, even if caught, will prevent the rest of the function from executing. Errors won't. Now, speaking abstractly, if the PHP error isn't a fatal error, then maybe the code should be written in such a way not to cause that error to appear, whilst still executing.

What the best fix is ultimately depends on the context, and I would like to better understand what is happening.

@simPod
Copy link
Contributor Author

simPod commented Aug 15, 2022

Just to keep the thread going, I've sent logs to @terrafrost.

@codepeqatech

This comment was marked as off-topic.

@mewejo
Copy link

mewejo commented Jul 12, 2023

Did you make any progress with this @simPod?

@simPod
Copy link
Contributor Author

simPod commented Jul 12, 2023

No, there's nothing we can do until some exception is thrown.

@mewejo
Copy link

mewejo commented Jul 19, 2023

This is a weird one.

It only happens on some servers. But those it does happen on, it always happens.

The first connection to the server will consistently fail. The second will succeed. But if I restart a Laravel Tinker session (a PHP shell in production), the issue happens again. Something is cached, and it's not the SSH2 instance, it must be something at the PHP level or lower.

After a lot of debugging, adding a sleep(1) after login(), and before calling read() seems to consistently fix the issue. I don't like adding a sleep() but, right now, it seems the only viable solution.

The same issue doesn't happen with exec(). Maybe because the time it's taking to run the command is mimicking the benefit of the sleep() above...

Does this help at all with a theory @terrafrost ?

@terrafrost
Copy link
Member

@mewejo - Calling write() too quickly can result in unexpected behavior as discussed here:

http://phpseclib.com/docs/commands#gotcha-writing-without-first-reading

But it's not clear to me why read() wouldn't work if called too quickly.

Could you post your SSH2 logs with and without the sleep(1)? You can get define('NET_SSH2_LOGGING', 2) at top and then echo $ssh->getLog() after you run the command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants