| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Runtime.ExceptionServices; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading; |
| | | 7 | | |
| | | 8 | | using Renci.SshNet.Abstractions; |
| | | 9 | | using Renci.SshNet.Channels; |
| | | 10 | | using Renci.SshNet.Common; |
| | | 11 | | using Renci.SshNet.Messages.Connection; |
| | | 12 | | using Renci.SshNet.Messages.Transport; |
| | | 13 | | |
| | | 14 | | namespace Renci.SshNet |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Represents SSH command that can be executed. |
| | | 18 | | /// </summary> |
| | | 19 | | public class SshCommand : IDisposable |
| | | 20 | | { |
| | | 21 | | private readonly Encoding _encoding; |
| | 784 | 22 | | private readonly object _endExecuteLock = new object(); |
| | | 23 | | |
| | | 24 | | private ISession _session; |
| | | 25 | | private IChannelSession _channel; |
| | | 26 | | private CommandAsyncResult _asyncResult; |
| | | 27 | | private AsyncCallback _callback; |
| | | 28 | | private EventWaitHandle _sessionErrorOccuredWaitHandle; |
| | | 29 | | private Exception _exception; |
| | | 30 | | private StringBuilder _result; |
| | | 31 | | private StringBuilder _error; |
| | | 32 | | private bool _hasError; |
| | | 33 | | private bool _isDisposed; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the command text. |
| | | 37 | | /// </summary> |
| | 2356 | 38 | | public string CommandText { get; private set; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the command timeout. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <value> |
| | | 44 | | /// The command timeout. |
| | | 45 | | /// </value> |
| | | 46 | | /// <example> |
| | | 47 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 48 | | /// </example> |
| | 1534 | 49 | | public TimeSpan CommandTimeout { get; set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the command exit status. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <example> |
| | | 55 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand RunComm |
| | | 56 | | /// </example> |
| | 1102 | 57 | | public int ExitStatus { get; private set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the output stream. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <example> |
| | | 63 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 64 | | /// </example> |
| | | 65 | | #pragma warning disable CA1859 // Use concrete types when possible for improved performance |
| | 5507 | 66 | | public Stream OutputStream { get; private set; } |
| | | 67 | | #pragma warning restore CA1859 // Use concrete types when possible for improved performance |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Gets the extended output stream. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <example> |
| | | 73 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 74 | | /// </example> |
| | | 75 | | #pragma warning disable CA1859 // Use concrete types when possible for improved performance |
| | 2864 | 76 | | public Stream ExtendedOutputStream { get; private set; } |
| | | 77 | | #pragma warning restore CA1859 // Use concrete types when possible for improved performance |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the command execution result. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <example> |
| | | 83 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand RunComm |
| | | 84 | | /// </example> |
| | | 85 | | public string Result |
| | | 86 | | { |
| | | 87 | | get |
| | 759 | 88 | | { |
| | 759 | 89 | | _result ??= new StringBuilder(); |
| | | 90 | | |
| | 759 | 91 | | if (OutputStream != null && OutputStream.Length > 0) |
| | 343 | 92 | | { |
| | 343 | 93 | | using (var sr = new StreamReader(OutputStream, |
| | 343 | 94 | | _encoding, |
| | 343 | 95 | | detectEncodingFromByteOrderMarks: true, |
| | 343 | 96 | | bufferSize: 1024, |
| | 343 | 97 | | leaveOpen: true)) |
| | 343 | 98 | | { |
| | 343 | 99 | | _ = _result.Append(sr.ReadToEnd()); |
| | 343 | 100 | | } |
| | 343 | 101 | | } |
| | | 102 | | |
| | 759 | 103 | | return _result.ToString(); |
| | 759 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets the command execution error. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <example> |
| | | 111 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 112 | | /// </example> |
| | | 113 | | public string Error |
| | | 114 | | { |
| | | 115 | | get |
| | 62 | 116 | | { |
| | 62 | 117 | | if (_hasError) |
| | 22 | 118 | | { |
| | 22 | 119 | | _error ??= new StringBuilder(); |
| | | 120 | | |
| | 22 | 121 | | if (ExtendedOutputStream != null && ExtendedOutputStream.Length > 0) |
| | 21 | 122 | | { |
| | 21 | 123 | | using (var sr = new StreamReader(ExtendedOutputStream, |
| | 21 | 124 | | _encoding, |
| | 21 | 125 | | detectEncodingFromByteOrderMarks: true, |
| | 21 | 126 | | bufferSize: 1024, |
| | 21 | 127 | | leaveOpen: true)) |
| | 21 | 128 | | { |
| | 21 | 129 | | _ = _error.Append(sr.ReadToEnd()); |
| | 21 | 130 | | } |
| | 21 | 131 | | } |
| | | 132 | | |
| | 22 | 133 | | return _error.ToString(); |
| | | 134 | | } |
| | | 135 | | |
| | 40 | 136 | | return string.Empty; |
| | 62 | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Initializes a new instance of the <see cref="SshCommand"/> class. |
| | | 142 | | /// </summary> |
| | | 143 | | /// <param name="session">The session.</param> |
| | | 144 | | /// <param name="commandText">The command text.</param> |
| | | 145 | | /// <param name="encoding">The encoding to use for the results.</param> |
| | | 146 | | /// <exception cref="ArgumentNullException">Either <paramref name="session"/>, <paramref name="commandText"/> is |
| | 784 | 147 | | internal SshCommand(ISession session, string commandText, Encoding encoding) |
| | 784 | 148 | | { |
| | 784 | 149 | | if (session is null) |
| | 0 | 150 | | { |
| | 0 | 151 | | throw new ArgumentNullException(nameof(session)); |
| | | 152 | | } |
| | | 153 | | |
| | 784 | 154 | | if (commandText is null) |
| | 0 | 155 | | { |
| | 0 | 156 | | throw new ArgumentNullException(nameof(commandText)); |
| | | 157 | | } |
| | | 158 | | |
| | 784 | 159 | | if (encoding is null) |
| | 0 | 160 | | { |
| | 0 | 161 | | throw new ArgumentNullException(nameof(encoding)); |
| | | 162 | | } |
| | | 163 | | |
| | 784 | 164 | | _session = session; |
| | 784 | 165 | | CommandText = commandText; |
| | 784 | 166 | | _encoding = encoding; |
| | 784 | 167 | | CommandTimeout = Session.InfiniteTimeSpan; |
| | 784 | 168 | | _sessionErrorOccuredWaitHandle = new AutoResetEvent(initialState: false); |
| | | 169 | | |
| | 784 | 170 | | _session.Disconnected += Session_Disconnected; |
| | 784 | 171 | | _session.ErrorOccured += Session_ErrorOccured; |
| | 784 | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Begins an asynchronous command execution. |
| | | 176 | | /// </summary> |
| | | 177 | | /// <returns> |
| | | 178 | | /// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pend |
| | | 179 | | /// </returns> |
| | | 180 | | /// <example> |
| | | 181 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 182 | | /// </example> |
| | | 183 | | /// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception> |
| | | 184 | | /// <exception cref="SshException">Invalid operation.</exception> |
| | | 185 | | /// <exception cref="ArgumentException">CommandText property is empty.</exception> |
| | | 186 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 187 | | /// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception> |
| | | 188 | | public IAsyncResult BeginExecute() |
| | 77 | 189 | | { |
| | 77 | 190 | | return BeginExecute(callback: null, state: null); |
| | 74 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Begins an asynchronous command execution. |
| | | 195 | | /// </summary> |
| | | 196 | | /// <param name="callback">An optional asynchronous callback, to be called when the command execution is complet |
| | | 197 | | /// <returns> |
| | | 198 | | /// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pend |
| | | 199 | | /// </returns> |
| | | 200 | | /// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception> |
| | | 201 | | /// <exception cref="SshException">Invalid operation.</exception> |
| | | 202 | | /// <exception cref="ArgumentException">CommandText property is empty.</exception> |
| | | 203 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 204 | | /// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception> |
| | | 205 | | public IAsyncResult BeginExecute(AsyncCallback callback) |
| | 0 | 206 | | { |
| | 0 | 207 | | return BeginExecute(callback, state: null); |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Begins an asynchronous command execution. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="callback">An optional asynchronous callback, to be called when the command execution is complet |
| | | 214 | | /// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from |
| | | 215 | | /// <returns> |
| | | 216 | | /// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pend |
| | | 217 | | /// </returns> |
| | | 218 | | /// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception> |
| | | 219 | | /// <exception cref="SshException">Invalid operation.</exception> |
| | | 220 | | /// <exception cref="ArgumentException">CommandText property is empty.</exception> |
| | | 221 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 222 | | /// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception> |
| | | 223 | | #pragma warning disable CA1859 // Use concrete types when possible for improved performance |
| | | 224 | | public IAsyncResult BeginExecute(AsyncCallback callback, object state) |
| | | 225 | | #pragma warning restore CA1859 // Use concrete types when possible for improved performance |
| | 788 | 226 | | { |
| | | 227 | | // Prevent from executing BeginExecute before calling EndExecute |
| | 788 | 228 | | if (_asyncResult != null && !_asyncResult.EndCalled) |
| | 3 | 229 | | { |
| | 3 | 230 | | throw new InvalidOperationException("Asynchronous operation is already in progress."); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | // Create new AsyncResult object |
| | 785 | 234 | | _asyncResult = new CommandAsyncResult |
| | 785 | 235 | | { |
| | 785 | 236 | | AsyncWaitHandle = new ManualResetEvent(initialState: false), |
| | 785 | 237 | | IsCompleted = false, |
| | 785 | 238 | | AsyncState = state, |
| | 785 | 239 | | }; |
| | | 240 | | |
| | | 241 | | // When command re-executed again, create a new channel |
| | 785 | 242 | | if (_channel is not null) |
| | 0 | 243 | | { |
| | 0 | 244 | | throw new SshException("Invalid operation."); |
| | | 245 | | } |
| | | 246 | | |
| | 785 | 247 | | if (string.IsNullOrEmpty(CommandText)) |
| | 0 | 248 | | { |
| | 0 | 249 | | throw new ArgumentException("CommandText property is empty."); |
| | | 250 | | } |
| | | 251 | | |
| | 785 | 252 | | var outputStream = OutputStream; |
| | 785 | 253 | | if (outputStream is not null) |
| | 7 | 254 | | { |
| | 7 | 255 | | outputStream.Dispose(); |
| | 7 | 256 | | OutputStream = null; |
| | 7 | 257 | | } |
| | | 258 | | |
| | 785 | 259 | | var extendedOutputStream = ExtendedOutputStream; |
| | 785 | 260 | | if (extendedOutputStream is not null) |
| | 7 | 261 | | { |
| | 7 | 262 | | extendedOutputStream.Dispose(); |
| | 7 | 263 | | ExtendedOutputStream = null; |
| | 7 | 264 | | } |
| | | 265 | | |
| | | 266 | | // Initialize output streams |
| | 785 | 267 | | OutputStream = new PipeStream(); |
| | 785 | 268 | | ExtendedOutputStream = new PipeStream(); |
| | | 269 | | |
| | 785 | 270 | | _result = null; |
| | 785 | 271 | | _error = null; |
| | 785 | 272 | | _callback = callback; |
| | | 273 | | |
| | 785 | 274 | | _channel = CreateChannel(); |
| | 785 | 275 | | _channel.Open(); |
| | 785 | 276 | | _ = _channel.SendExecRequest(CommandText); |
| | | 277 | | |
| | 785 | 278 | | return _asyncResult; |
| | 785 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Begins an asynchronous command execution. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="commandText">The command text.</param> |
| | | 285 | | /// <param name="callback">An optional asynchronous callback, to be called when the command execution is complet |
| | | 286 | | /// <param name="state">A user-provided object that distinguishes this particular asynchronous read request from |
| | | 287 | | /// <returns> |
| | | 288 | | /// An <see cref="IAsyncResult" /> that represents the asynchronous command execution, which could still be pend |
| | | 289 | | /// </returns> |
| | | 290 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 291 | | /// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception> |
| | | 292 | | public IAsyncResult BeginExecute(string commandText, AsyncCallback callback, object state) |
| | 0 | 293 | | { |
| | 0 | 294 | | CommandText = commandText; |
| | | 295 | | |
| | 0 | 296 | | return BeginExecute(callback, state); |
| | 0 | 297 | | } |
| | | 298 | | |
| | | 299 | | /// <summary> |
| | | 300 | | /// Waits for the pending asynchronous command execution to complete. |
| | | 301 | | /// </summary> |
| | | 302 | | /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> |
| | | 303 | | /// <returns>Command execution result.</returns> |
| | | 304 | | /// <example> |
| | | 305 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 306 | | /// </example> |
| | | 307 | | /// <exception cref="ArgumentException">Either the IAsyncResult object did not come from the corresponding async |
| | | 308 | | /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception |
| | | 309 | | public string EndExecute(IAsyncResult asyncResult) |
| | 759 | 310 | | { |
| | 759 | 311 | | if (asyncResult is null) |
| | 4 | 312 | | { |
| | 4 | 313 | | throw new ArgumentNullException(nameof(asyncResult)); |
| | | 314 | | } |
| | | 315 | | |
| | 755 | 316 | | if (asyncResult is not CommandAsyncResult commandAsyncResult || _asyncResult != commandAsyncResult) |
| | 3 | 317 | | { |
| | 3 | 318 | | throw new ArgumentException(string.Format("The {0} object was not returned from the corresponding asynch |
| | | 319 | | } |
| | | 320 | | |
| | 752 | 321 | | lock (_endExecuteLock) |
| | 752 | 322 | | { |
| | 752 | 323 | | if (commandAsyncResult.EndCalled) |
| | 3 | 324 | | { |
| | 3 | 325 | | throw new ArgumentException("EndExecute can only be called once for each asynchronous operation."); |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | // wait for operation to complete (or time out) |
| | 749 | 329 | | WaitOnHandle(_asyncResult.AsyncWaitHandle); |
| | | 330 | | |
| | 748 | 331 | | UnsubscribeFromEventsAndDisposeChannel(_channel); |
| | 748 | 332 | | _channel = null; |
| | | 333 | | |
| | 748 | 334 | | commandAsyncResult.EndCalled = true; |
| | | 335 | | |
| | 748 | 336 | | return Result; |
| | | 337 | | } |
| | 748 | 338 | | } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Cancels command execution in asynchronous scenarios. |
| | | 342 | | /// </summary> |
| | | 343 | | public void CancelAsync() |
| | 0 | 344 | | { |
| | 0 | 345 | | if (_channel is not null && _channel.IsOpen && _asyncResult is not null) |
| | 0 | 346 | | { |
| | | 347 | | // TODO: check with Oleg if we shouldn't dispose the channel and uninitialize it ? |
| | 0 | 348 | | _channel.Dispose(); |
| | 0 | 349 | | } |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | /// <summary> |
| | | 353 | | /// Executes command specified by <see cref="CommandText"/> property. |
| | | 354 | | /// </summary> |
| | | 355 | | /// <returns> |
| | | 356 | | /// Command execution result. |
| | | 357 | | /// </returns> |
| | | 358 | | /// <example> |
| | | 359 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 360 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 361 | | /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand CreateC |
| | | 362 | | /// </example> |
| | | 363 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 364 | | /// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception> |
| | | 365 | | public string Execute() |
| | 707 | 366 | | { |
| | 707 | 367 | | return EndExecute(BeginExecute(callback: null, state: null)); |
| | 706 | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Executes the specified command text. |
| | | 372 | | /// </summary> |
| | | 373 | | /// <param name="commandText">The command text.</param> |
| | | 374 | | /// <returns> |
| | | 375 | | /// The result of the command execution. |
| | | 376 | | /// </returns> |
| | | 377 | | /// <exception cref="SshConnectionException">Client is not connected.</exception> |
| | | 378 | | /// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception> |
| | | 379 | | public string Execute(string commandText) |
| | 1 | 380 | | { |
| | 1 | 381 | | CommandText = commandText; |
| | | 382 | | |
| | 1 | 383 | | return Execute(); |
| | 1 | 384 | | } |
| | | 385 | | |
| | | 386 | | private IChannelSession CreateChannel() |
| | 785 | 387 | | { |
| | 785 | 388 | | var channel = _session.CreateChannelSession(); |
| | 785 | 389 | | channel.DataReceived += Channel_DataReceived; |
| | 785 | 390 | | channel.ExtendedDataReceived += Channel_ExtendedDataReceived; |
| | 785 | 391 | | channel.RequestReceived += Channel_RequestReceived; |
| | 785 | 392 | | channel.Closed += Channel_Closed; |
| | 785 | 393 | | return channel; |
| | 785 | 394 | | } |
| | | 395 | | |
| | | 396 | | private void Session_Disconnected(object sender, EventArgs e) |
| | 0 | 397 | | { |
| | | 398 | | // If objected is disposed or being disposed don't handle this event |
| | 0 | 399 | | if (_isDisposed) |
| | 0 | 400 | | { |
| | 0 | 401 | | return; |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | _exception = new SshConnectionException("An established connection was aborted by the software in your host |
| | | 405 | | |
| | 0 | 406 | | _ = _sessionErrorOccuredWaitHandle.Set(); |
| | 0 | 407 | | } |
| | | 408 | | |
| | | 409 | | private void Session_ErrorOccured(object sender, ExceptionEventArgs e) |
| | 0 | 410 | | { |
| | | 411 | | // If objected is disposed or being disposed don't handle this event |
| | 0 | 412 | | if (_isDisposed) |
| | 0 | 413 | | { |
| | 0 | 414 | | return; |
| | | 415 | | } |
| | | 416 | | |
| | 0 | 417 | | _exception = e.Exception; |
| | | 418 | | |
| | 0 | 419 | | _ = _sessionErrorOccuredWaitHandle.Set(); |
| | 0 | 420 | | } |
| | | 421 | | |
| | | 422 | | private void Channel_Closed(object sender, ChannelEventArgs e) |
| | 748 | 423 | | { |
| | 748 | 424 | | OutputStream?.Flush(); |
| | 748 | 425 | | ExtendedOutputStream?.Flush(); |
| | | 426 | | |
| | 748 | 427 | | _asyncResult.IsCompleted = true; |
| | | 428 | | |
| | 748 | 429 | | if (_callback is not null) |
| | 2 | 430 | | { |
| | | 431 | | // Execute callback on different thread |
| | 4 | 432 | | ThreadAbstraction.ExecuteThread(() => _callback(_asyncResult)); |
| | 2 | 433 | | } |
| | | 434 | | |
| | 748 | 435 | | _ = ((EventWaitHandle) _asyncResult.AsyncWaitHandle).Set(); |
| | 748 | 436 | | } |
| | | 437 | | |
| | | 438 | | private void Channel_RequestReceived(object sender, ChannelRequestEventArgs e) |
| | 739 | 439 | | { |
| | 739 | 440 | | if (e.Info is ExitStatusRequestInfo exitStatusInfo) |
| | 739 | 441 | | { |
| | 739 | 442 | | ExitStatus = (int) exitStatusInfo.ExitStatus; |
| | | 443 | | |
| | 739 | 444 | | if (exitStatusInfo.WantReply) |
| | 0 | 445 | | { |
| | 0 | 446 | | var replyMessage = new ChannelSuccessMessage(_channel.LocalChannelNumber); |
| | 0 | 447 | | _session.SendMessage(replyMessage); |
| | 0 | 448 | | } |
| | 739 | 449 | | } |
| | | 450 | | else |
| | 0 | 451 | | { |
| | 0 | 452 | | if (e.Info.WantReply) |
| | 0 | 453 | | { |
| | 0 | 454 | | var replyMessage = new ChannelFailureMessage(_channel.LocalChannelNumber); |
| | 0 | 455 | | _session.SendMessage(replyMessage); |
| | 0 | 456 | | } |
| | 0 | 457 | | } |
| | 739 | 458 | | } |
| | | 459 | | |
| | | 460 | | private void Channel_ExtendedDataReceived(object sender, ChannelExtendedDataEventArgs e) |
| | 88 | 461 | | { |
| | 88 | 462 | | if (ExtendedOutputStream != null) |
| | 88 | 463 | | { |
| | 88 | 464 | | ExtendedOutputStream.Write(e.Data, 0, e.Data.Length); |
| | 88 | 465 | | ExtendedOutputStream.Flush(); |
| | 88 | 466 | | } |
| | | 467 | | |
| | 88 | 468 | | if (e.DataTypeCode == 1) |
| | 40 | 469 | | { |
| | 40 | 470 | | _hasError = true; |
| | 40 | 471 | | } |
| | 88 | 472 | | } |
| | | 473 | | |
| | | 474 | | private void Channel_DataReceived(object sender, ChannelDataEventArgs e) |
| | 371 | 475 | | { |
| | 371 | 476 | | if (OutputStream != null) |
| | 371 | 477 | | { |
| | 371 | 478 | | OutputStream.Write(e.Data, 0, e.Data.Length); |
| | 371 | 479 | | OutputStream.Flush(); |
| | 371 | 480 | | } |
| | | 481 | | |
| | 371 | 482 | | if (_asyncResult != null) |
| | 371 | 483 | | { |
| | 371 | 484 | | lock (_asyncResult) |
| | 371 | 485 | | { |
| | 371 | 486 | | _asyncResult.BytesReceived += e.Data.Length; |
| | 371 | 487 | | } |
| | 371 | 488 | | } |
| | 371 | 489 | | } |
| | | 490 | | |
| | | 491 | | /// <exception cref="SshOperationTimeoutException">Command '{0}' has timed out.</exception> |
| | | 492 | | /// <remarks>The actual command will be included in the exception message.</remarks> |
| | | 493 | | private void WaitOnHandle(WaitHandle waitHandle) |
| | 749 | 494 | | { |
| | 749 | 495 | | var waitHandles = new[] |
| | 749 | 496 | | { |
| | 749 | 497 | | _sessionErrorOccuredWaitHandle, |
| | 749 | 498 | | waitHandle |
| | 749 | 499 | | }; |
| | | 500 | | |
| | 749 | 501 | | var signaledElement = WaitHandle.WaitAny(waitHandles, CommandTimeout); |
| | 749 | 502 | | switch (signaledElement) |
| | | 503 | | { |
| | | 504 | | case 0: |
| | 0 | 505 | | ExceptionDispatchInfo.Capture(_exception).Throw(); |
| | 0 | 506 | | break; |
| | | 507 | | case 1: |
| | | 508 | | // Specified waithandle was signaled |
| | 748 | 509 | | break; |
| | | 510 | | case WaitHandle.WaitTimeout: |
| | 1 | 511 | | throw new SshOperationTimeoutException(string.Format(CultureInfo.CurrentCulture, "Command '{0}' has |
| | | 512 | | default: |
| | 0 | 513 | | throw new SshException($"Unexpected element '{signaledElement.ToString(CultureInfo.InvariantCulture) |
| | | 514 | | } |
| | 748 | 515 | | } |
| | | 516 | | |
| | | 517 | | /// <summary> |
| | | 518 | | /// Unsubscribes the current <see cref="SshCommand"/> from channel events, and disposes |
| | | 519 | | /// the <see cref="IChannel"/>. |
| | | 520 | | /// </summary> |
| | | 521 | | /// <param name="channel">The channel.</param> |
| | | 522 | | /// <remarks> |
| | | 523 | | /// Does nothing when <paramref name="channel"/> is <see langword="null"/>. |
| | | 524 | | /// </remarks> |
| | | 525 | | private void UnsubscribeFromEventsAndDisposeChannel(IChannel channel) |
| | 772 | 526 | | { |
| | 772 | 527 | | if (channel is null) |
| | 0 | 528 | | { |
| | 0 | 529 | | return; |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | // unsubscribe from events as we do not want to be signaled should these get fired |
| | | 533 | | // during the dispose of the channel |
| | 772 | 534 | | channel.DataReceived -= Channel_DataReceived; |
| | 772 | 535 | | channel.ExtendedDataReceived -= Channel_ExtendedDataReceived; |
| | 772 | 536 | | channel.RequestReceived -= Channel_RequestReceived; |
| | 772 | 537 | | channel.Closed -= Channel_Closed; |
| | | 538 | | |
| | | 539 | | // actually dispose the channel |
| | 772 | 540 | | channel.Dispose(); |
| | 772 | 541 | | } |
| | | 542 | | |
| | | 543 | | /// <summary> |
| | | 544 | | /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| | | 545 | | /// </summary> |
| | | 546 | | public void Dispose() |
| | 92 | 547 | | { |
| | 92 | 548 | | Dispose(disposing: true); |
| | 92 | 549 | | GC.SuppressFinalize(this); |
| | 92 | 550 | | } |
| | | 551 | | |
| | | 552 | | /// <summary> |
| | | 553 | | /// Releases unmanaged and - optionally - managed resources. |
| | | 554 | | /// </summary> |
| | | 555 | | /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor |
| | | 556 | | protected virtual void Dispose(bool disposing) |
| | 677 | 557 | | { |
| | 677 | 558 | | if (_isDisposed) |
| | 3 | 559 | | { |
| | 3 | 560 | | return; |
| | | 561 | | } |
| | | 562 | | |
| | 674 | 563 | | if (disposing) |
| | 89 | 564 | | { |
| | | 565 | | // unsubscribe from session events to ensure other objects that we're going to dispose |
| | | 566 | | // are not accessed while disposing |
| | 89 | 567 | | var session = _session; |
| | 89 | 568 | | if (session != null) |
| | 89 | 569 | | { |
| | 89 | 570 | | session.Disconnected -= Session_Disconnected; |
| | 89 | 571 | | session.ErrorOccured -= Session_ErrorOccured; |
| | 89 | 572 | | _session = null; |
| | 89 | 573 | | } |
| | | 574 | | |
| | | 575 | | // unsubscribe from channel events to ensure other objects that we're going to dispose |
| | | 576 | | // are not accessed while disposing |
| | 89 | 577 | | var channel = _channel; |
| | 89 | 578 | | if (channel != null) |
| | 24 | 579 | | { |
| | 24 | 580 | | UnsubscribeFromEventsAndDisposeChannel(channel); |
| | 24 | 581 | | _channel = null; |
| | 24 | 582 | | } |
| | | 583 | | |
| | 89 | 584 | | var outputStream = OutputStream; |
| | 89 | 585 | | if (outputStream != null) |
| | 89 | 586 | | { |
| | 89 | 587 | | outputStream.Dispose(); |
| | 89 | 588 | | OutputStream = null; |
| | 89 | 589 | | } |
| | | 590 | | |
| | 89 | 591 | | var extendedOutputStream = ExtendedOutputStream; |
| | 89 | 592 | | if (extendedOutputStream != null) |
| | 89 | 593 | | { |
| | 89 | 594 | | extendedOutputStream.Dispose(); |
| | 89 | 595 | | ExtendedOutputStream = null; |
| | 89 | 596 | | } |
| | | 597 | | |
| | 89 | 598 | | var sessionErrorOccuredWaitHandle = _sessionErrorOccuredWaitHandle; |
| | 89 | 599 | | if (sessionErrorOccuredWaitHandle != null) |
| | 89 | 600 | | { |
| | 89 | 601 | | sessionErrorOccuredWaitHandle.Dispose(); |
| | 89 | 602 | | _sessionErrorOccuredWaitHandle = null; |
| | 89 | 603 | | } |
| | | 604 | | |
| | 89 | 605 | | _isDisposed = true; |
| | 89 | 606 | | } |
| | 677 | 607 | | } |
| | | 608 | | |
| | | 609 | | /// <summary> |
| | | 610 | | /// Finalizes an instance of the <see cref="SshCommand"/> class. |
| | | 611 | | /// Releases unmanaged resources and performs other cleanup operations before the |
| | | 612 | | /// <see cref="SshCommand"/> is reclaimed by garbage collection. |
| | | 613 | | /// </summary> |
| | | 614 | | ~SshCommand() |
| | 1170 | 615 | | { |
| | 585 | 616 | | Dispose(disposing: false); |
| | 1170 | 617 | | } |
| | | 618 | | } |
| | | 619 | | } |