Rev 1463 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1463 | Rev 1466 | ||
---|---|---|---|
Line 2838... | Line 2838... | ||
2838 | 2838 | ||
2839 | //if ($this->isPTYOpen()) { |
2839 | //if ($this->isPTYOpen()) { |
2840 | // throw new \RuntimeException('If you want to run multiple exec()\'s you will need to disable (and re-enable if appropriate) a PTY for each one.'); |
2840 | // throw new \RuntimeException('If you want to run multiple exec()\'s you will need to disable (and re-enable if appropriate) a PTY for each one.'); |
2841 | //} |
2841 | //} |
2842 | 2842 | ||
2843 | $this->openChannel(self::CHANNEL_EXEC); |
2843 | $this->open_channel(self::CHANNEL_EXEC); |
2844 | 2844 | ||
2845 | if ($this->request_pty === true) { |
2845 | if ($this->request_pty === true) { |
2846 | $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); |
2846 | $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); |
2847 | $packet = Strings::packSSH2( |
2847 | $packet = Strings::packSSH2( |
2848 | 'CNsCsN4s', |
2848 | 'CNsCsN4s', |
Line 2935... | Line 2935... | ||
2935 | * |
2935 | * |
2936 | * @param string $channel |
2936 | * @param string $channel |
2937 | * @param bool $skip_extended |
2937 | * @param bool $skip_extended |
2938 | * @return bool |
2938 | * @return bool |
2939 | */ |
2939 | */ |
2940 | protected function openChannel($channel, $skip_extended = false) |
2940 | protected function open_channel($channel, $skip_extended = false) |
2941 | { |
2941 | { |
2942 | if (isset($this->channel_status[$channel]) && $this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_CLOSE) { |
2942 | if (isset($this->channel_status[$channel]) && $this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_CLOSE) { |
2943 | throw new \RuntimeException('Please close the channel (' . $channel . ') before trying to open it again'); |
2943 | throw new \RuntimeException('Please close the channel (' . $channel . ') before trying to open it again'); |
2944 | } |
2944 | } |
2945 | 2945 | ||
Line 2992... | Line 2992... | ||
2992 | { |
2992 | { |
2993 | if (!$this->isAuthenticated()) { |
2993 | if (!$this->isAuthenticated()) { |
2994 | throw new InsufficientSetupException('Operation disallowed prior to login()'); |
2994 | throw new InsufficientSetupException('Operation disallowed prior to login()'); |
2995 | } |
2995 | } |
2996 | 2996 | ||
2997 | $this->openChannel(self::CHANNEL_SHELL); |
2997 | $this->open_channel(self::CHANNEL_SHELL); |
2998 | 2998 | ||
2999 | $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); |
2999 | $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); |
3000 | $packet = Strings::packSSH2( |
3000 | $packet = Strings::packSSH2( |
3001 | 'CNsbsN4s', |
3001 | 'CNsbsN4s', |
3002 | NET_SSH2_MSG_CHANNEL_REQUEST, |
3002 | NET_SSH2_MSG_CHANNEL_REQUEST, |
Line 3240... | Line 3240... | ||
3240 | * @param string $subsystem |
3240 | * @param string $subsystem |
3241 | * @return bool |
3241 | * @return bool |
3242 | */ |
3242 | */ |
3243 | public function startSubsystem($subsystem) |
3243 | public function startSubsystem($subsystem) |
3244 | { |
3244 | { |
3245 | $this->openChannel(self::CHANNEL_SUBSYSTEM); |
3245 | $this->open_channel(self::CHANNEL_SUBSYSTEM); |
3246 | 3246 | ||
3247 | $packet = Strings::packSSH2( |
3247 | $packet = Strings::packSSH2( |
3248 | 'CNsCs', |
3248 | 'CNsCs', |
3249 | NET_SSH2_MSG_CHANNEL_REQUEST, |
3249 | NET_SSH2_MSG_CHANNEL_REQUEST, |
3250 | $this->server_channels[self::CHANNEL_SUBSYSTEM], |
3250 | $this->server_channels[self::CHANNEL_SUBSYSTEM], |
Line 3342... | Line 3342... | ||
3342 | } |
3342 | } |
3343 | 3343 | ||
3344 | /** |
3344 | /** |
3345 | * Is the connection still active? |
3345 | * Is the connection still active? |
3346 | * |
3346 | * |
- | 3347 | * $level has 3x possible values: |
|
- | 3348 | * 0 (default): phpseclib takes a passive approach to see if the connection is still active by calling feof() |
|
- | 3349 | * on the socket |
|
- | 3350 | * 1: phpseclib takes an active approach to see if the connection is still active by sending an SSH_MSG_IGNORE |
|
- | 3351 | * packet that doesn't require a response |
|
- | 3352 | * 2: phpseclib takes an active approach to see if the connection is still active by sending an SSH_MSG_CHANNEL_OPEN |
|
- | 3353 | * packet and imediately trying to close that channel. some routers, in particular, however, will only let you |
|
- | 3354 | * open one channel, so this approach could yield false positives |
|
- | 3355 | * |
|
- | 3356 | * @param int $level |
|
3347 | * @return bool |
3357 | * @return bool |
3348 | */ |
3358 | */ |
3349 | public function isConnected() |
3359 | public function isConnected($level = 0) |
3350 | { |
3360 | { |
- | 3361 | if (!is_int($level) || $level < 0 || $level > 2) { |
|
- | 3362 | throw new \InvalidArgumentException('$level must be 0, 1 or 2'); |
|
- | 3363 | } |
|
- | 3364 | ||
- | 3365 | if ($level == 0) { |
|
3351 | return ($this->bitmap & self::MASK_CONNECTED) && is_resource($this->fsock) && !feof($this->fsock); |
3366 | return ($this->bitmap & self::MASK_CONNECTED) && is_resource($this->fsock) && !feof($this->fsock); |
3352 | } |
3367 | } |
- | 3368 | try { |
|
- | 3369 | if ($level == 1) { |
|
- | 3370 | $this->send_binary_packet(pack('CN', NET_SSH2_MSG_IGNORE, 0)); |
|
- | 3371 | } else { |
|
- | 3372 | $this->open_channel(self::CHANNEL_KEEP_ALIVE); |
|
- | 3373 | $this->close_channel(self::CHANNEL_KEEP_ALIVE); |
|
- | 3374 | } |
|
- | 3375 | return true; |
|
- | 3376 | } catch (\Exception $e) { |
|
- | 3377 | return false; |
|
- | 3378 | } |
|
- | 3379 | } |
|
3353 | 3380 | ||
3354 | /** |
3381 | /** |
3355 | * Have you successfully been logged in? |
3382 | * Have you successfully been logged in? |
3356 | * |
3383 | * |
3357 | * @return bool |
3384 | * @return bool |
Line 3419... | Line 3446... | ||
3419 | } |
3446 | } |
3420 | return false; |
3447 | return false; |
3421 | } |
3448 | } |
3422 | 3449 | ||
3423 | try { |
3450 | try { |
3424 | $this->openChannel(self::CHANNEL_KEEP_ALIVE); |
3451 | $this->open_channel(self::CHANNEL_KEEP_ALIVE); |
3425 | } catch (\RuntimeException $e) { |
3452 | } catch (\RuntimeException $e) { |
3426 | return $this->reconnect(); |
3453 | return $this->reconnect(); |
3427 | } |
3454 | } |
3428 | 3455 | ||
3429 | $this->close_channel(self::CHANNEL_KEEP_ALIVE); |
3456 | $this->close_channel(self::CHANNEL_KEEP_ALIVE); |
Line 3532... | Line 3559... | ||
3532 | } |
3559 | } |
3533 | throw new ConnectionClosedException($str); |
3560 | throw new ConnectionClosedException($str); |
3534 | } |
3561 | } |
3535 | 3562 | ||
3536 | $start = microtime(true); |
3563 | $start = microtime(true); |
- | 3564 | if ($this->curTimeout) { |
|
3537 | $sec = (int) floor($this->curTimeout); |
3565 | $sec = (int) floor($this->curTimeout); |
3538 | $usec = (int) (1000000 * ($this->curTimeout - $sec)); |
3566 | $usec = (int) (1000000 * ($this->curTimeout - $sec)); |
3539 | stream_set_timeout($this->fsock, $sec, $usec); |
3567 | stream_set_timeout($this->fsock, $sec, $usec); |
- | 3568 | } |
|
3540 | $raw = stream_get_contents($this->fsock, $this->decrypt_block_size); |
3569 | $raw = stream_get_contents($this->fsock, $this->decrypt_block_size); |
3541 | 3570 | ||
3542 | if (!strlen($raw)) { |
3571 | if (!strlen($raw)) { |
3543 | $this->bitmap = 0; |
3572 | $this->bitmap = 0; |
3544 | throw new ConnectionClosedException('No data received from server'); |
3573 | throw new ConnectionClosedException('No data received from server'); |