degagne /
libssh2
| 1 | <?php |
||
| 2 | namespace LibSSH2\Sessions; |
||
| 3 | |||
| 4 | use LibSSH2\Authentication\Authentication; |
||
| 5 | use LibSSH2\Configuration; |
||
| 6 | use LibSSH2\Connection; |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Shell class. |
||
| 10 | * |
||
| 11 | * Shell interface. |
||
| 12 | * |
||
| 13 | * @package LibSSH2\Sessions |
||
| 14 | */ |
||
| 15 | class Shell extends Connection |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * SSH interactive shell resource. |
||
| 20 | * |
||
| 21 | * @var resource |
||
| 22 | */ |
||
| 23 | private $stream; |
||
| 24 | private $command; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor. |
||
| 28 | * |
||
| 29 | * @param object $configuration Configuration class object |
||
| 30 | * @param object $authentication Authentication class object |
||
| 31 | * @return void |
||
| 32 | */ |
||
| 33 | public function __construct(Configuration $configuration, Authentication $authentication) |
||
| 34 | { |
||
| 35 | parent::__construct($configuration, $authentication); |
||
| 36 | |||
| 37 | $this->shell(); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Destructor. |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function __destruct() |
||
| 46 | { |
||
| 47 | fclose($this->stream); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Returns SSH interactive shell. |
||
| 52 | * |
||
| 53 | * @return resouce SSH interactive shell |
||
|
0 ignored issues
–
show
The type
LibSSH2\Sessions\resouce was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 54 | */ |
||
| 55 | final public function shell() |
||
| 56 | { |
||
| 57 | if (($this->stream = @ssh2_shell($this->connection)) === false) |
||
|
0 ignored issues
–
show
|
|||
| 58 | { |
||
| 59 | throw new \RuntimeException($this->get_error_message()); |
||
| 60 | } |
||
| 61 | sleep(1); |
||
| 62 | return $this; |
||
|
0 ignored issues
–
show
|
|||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Retrieves shell command output. |
||
| 67 | * |
||
| 68 | * @return void |
||
| 69 | */ |
||
| 70 | final public function output() |
||
| 71 | { |
||
| 72 | $stdout = []; |
||
| 73 | while (!preg_match('/RETURN_CODE:\[([0-9]+)\]/', implode(PHP_EOL, $stdout), $retval)) |
||
| 74 | { |
||
| 75 | $buffer = fgets($this->stream); |
||
| 76 | if (!empty($buffer)) |
||
| 77 | { |
||
| 78 | $stdout[] = trim($buffer); |
||
| 79 | //print $buffer; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | $stdout = preg_replace('/RETURN_CODE:\[([0-9]+)\]/', '', $stdout); |
||
| 83 | $this->set_output(implode(PHP_EOL, $stdout)); |
||
| 84 | $this->set_exitstatus($retval[1]); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Execute remote command via SSH (shell). |
||
| 89 | * |
||
| 90 | * @param string $command command being executed |
||
| 91 | * @param instance $terminal Terminal instance |
||
| 92 | * @return object |
||
| 93 | */ |
||
| 94 | final public function write($command, $returncode = FALSE) |
||
| 95 | { |
||
| 96 | $command = ($returncode == FALSE) ? $command : $command.'; echo "RETURN_CODE:[$?]";'; |
||
| 97 | $write_count = 0; |
||
| 98 | $string_len = strlen($command.PHP_EOL); |
||
| 99 | while ($write_count < $string_len) |
||
| 100 | { |
||
| 101 | $fwrite_count = fwrite($this->stream, substr($command.PHP_EOL, $write_count), 1024); |
||
| 102 | if ($fwrite_count === FALSE) |
||
| 103 | { |
||
| 104 | throw new \RuntimeException('failed to write command to stream'); |
||
| 105 | } |
||
| 106 | $write_count += $fwrite_count; |
||
| 107 | } |
||
| 108 | sleep(1); |
||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | } |
||
| 113 |