SshClient.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using System.Diagnostics.CodeAnalysis;
  6. using Renci.SshNet.Common;
  7. namespace Renci.SshNet
  8. {
  9. /// <summary>
  10. /// Provides client connection to SSH server.
  11. /// </summary>
  12. public class SshClient : BaseClient
  13. {
  14. /// <summary>
  15. /// Holds the list of forwarded ports
  16. /// </summary>
  17. private readonly List<ForwardedPort> _forwardedPorts;
  18. /// <summary>
  19. /// Holds a value indicating whether the current instance is disposed.
  20. /// </summary>
  21. /// <value>
  22. /// <c>true</c> if the current instance is disposed; otherwise, <c>false</c>.
  23. /// </value>
  24. private bool _isDisposed;
  25. private Stream _inputStream;
  26. /// <summary>
  27. /// Gets the list of forwarded ports.
  28. /// </summary>
  29. public IEnumerable<ForwardedPort> ForwardedPorts
  30. {
  31. get
  32. {
  33. return _forwardedPorts.AsReadOnly();
  34. }
  35. }
  36. #region Constructors
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="SshClient" /> class.
  39. /// </summary>
  40. /// <param name="connectionInfo">The connection info.</param>
  41. /// <example>
  42. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo" language="C#" title="Connect using PasswordConnectionInfo object" />
  43. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PasswordConnectionInfoTest.cs" region="Example PasswordConnectionInfo PasswordExpired" language="C#" title="Connect using PasswordConnectionInfo object with passwod change option" />
  44. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\PrivateKeyConnectionInfoTest.cs" region="Example PrivateKeyConnectionInfo PrivateKeyFile" language="C#" title="Connect using PrivateKeyConnectionInfo" />
  45. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient Connect Timeout" language="C#" title="Specify connection timeout when connecting" />
  46. /// </example>
  47. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  48. public SshClient(ConnectionInfo connectionInfo)
  49. : this(connectionInfo, false)
  50. {
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="SshClient"/> class.
  54. /// </summary>
  55. /// <param name="host">Connection host.</param>
  56. /// <param name="port">Connection port.</param>
  57. /// <param name="username">Authentication username.</param>
  58. /// <param name="password">Authentication password.</param>
  59. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  60. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  61. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  62. [SuppressMessage("Microsoft.Reliability", "C2A000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  63. public SshClient(string host, int port, string username, string password)
  64. : this(new PasswordConnectionInfo(host, port, username, password), true)
  65. {
  66. }
  67. /// <summary>
  68. /// Initializes a new instance of the <see cref="SshClient"/> class.
  69. /// </summary>
  70. /// <param name="host">Connection host.</param>
  71. /// <param name="username">Authentication username.</param>
  72. /// <param name="password">Authentication password.</param>
  73. /// <example>
  74. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient(host, username) Connect" language="C#" title="Connect using username and password" />
  75. /// </example>
  76. /// <exception cref="ArgumentNullException"><paramref name="password"/> is null.</exception>
  77. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is null or contains whitespace characters.</exception>
  78. public SshClient(string host, string username, string password)
  79. : this(host, ConnectionInfo.DefaultPort, username, password)
  80. {
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="SshClient"/> class.
  84. /// </summary>
  85. /// <param name="host">Connection host.</param>
  86. /// <param name="port">Connection port.</param>
  87. /// <param name="username">Authentication username.</param>
  88. /// <param name="keyFiles">Authentication private key file(s) .</param>
  89. /// <example>
  90. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient(host, username) Connect PrivateKeyFile" language="C#" title="Connect using username and private key" />
  91. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient(host, username) Connect PrivateKeyFile PassPhrase" language="C#" title="Connect using username and private key and pass phrase" />
  92. /// </example>
  93. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is null.</exception>
  94. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is null or contains whitespace characters.</exception>
  95. /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="System.Net.IPEndPoint.MaxPort"/>.</exception>
  96. [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in Dispose(bool) method.")]
  97. public SshClient(string host, int port, string username, params PrivateKeyFile[] keyFiles)
  98. : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), true)
  99. {
  100. }
  101. /// <summary>
  102. /// Initializes a new instance of the <see cref="SshClient"/> class.
  103. /// </summary>
  104. /// <param name="host">Connection host.</param>
  105. /// <param name="username">Authentication username.</param>
  106. /// <param name="keyFiles">Authentication private key file(s) .</param>
  107. /// <example>
  108. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient(host, username) Connect PrivateKeyFile" language="C#" title="Connect using private key" />
  109. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshClientTest.cs" region="Example SshClient(host, username) Connect PrivateKeyFile PassPhrase" language="C#" title="Connect using private key and pass phrase" />
  110. /// </example>
  111. /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is null.</exception>
  112. /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is null or contains whitespace characters.</exception>
  113. public SshClient(string host, string username, params PrivateKeyFile[] keyFiles)
  114. : this(host, ConnectionInfo.DefaultPort, username, keyFiles)
  115. {
  116. }
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="SshClient"/> class.
  119. /// </summary>
  120. /// <param name="connectionInfo">The connection info.</param>
  121. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  122. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  123. /// <remarks>
  124. /// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
  125. /// connection info will be disposed when this instance is disposed.
  126. /// </remarks>
  127. private SshClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
  128. : this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
  129. {
  130. }
  131. /// <summary>
  132. /// Initializes a new instance of the <see cref="SshClient"/> class.
  133. /// </summary>
  134. /// <param name="connectionInfo">The connection info.</param>
  135. /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
  136. /// <param name="serviceFactory">The factory to use for creating new services.</param>
  137. /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is null.</exception>
  138. /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is null.</exception>
  139. /// <remarks>
  140. /// If <paramref name="ownsConnectionInfo"/> is <c>true</c>, then the
  141. /// connection info will be disposed when this instance is disposed.
  142. /// </remarks>
  143. internal SshClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
  144. : base(connectionInfo, ownsConnectionInfo, serviceFactory)
  145. {
  146. _forwardedPorts = new List<ForwardedPort>();
  147. }
  148. #endregion
  149. /// <summary>
  150. /// Called when client is disconnecting from the server.
  151. /// </summary>
  152. protected override void OnDisconnecting()
  153. {
  154. base.OnDisconnecting();
  155. foreach (var port in _forwardedPorts)
  156. {
  157. port.Stop();
  158. }
  159. }
  160. /// <summary>
  161. /// Adds the forwarded port.
  162. /// </summary>
  163. /// <param name="port">The port.</param>
  164. /// <example>
  165. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\ForwardedPortRemoteTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortRemote" language="C#" title="Remote port forwarding" />
  166. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\ForwardedPortLocalTest.cs" region="Example SshClient AddForwardedPort Start Stop ForwardedPortLocal" language="C#" title="Local port forwarding" />
  167. /// </example>
  168. /// <exception cref="InvalidOperationException">Forwarded port is already added to a different client.</exception>
  169. /// <exception cref="ArgumentNullException"><paramref name="port"/> is null.</exception>
  170. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  171. public void AddForwardedPort(ForwardedPort port)
  172. {
  173. if (port == null)
  174. throw new ArgumentNullException("port");
  175. EnsureSessionIsOpen();
  176. AttachForwardedPort(port);
  177. _forwardedPorts.Add(port);
  178. }
  179. /// <summary>
  180. /// Stops and removes the forwarded port from the list.
  181. /// </summary>
  182. /// <param name="port">Forwarded port.</param>
  183. /// <exception cref="ArgumentNullException"><paramref name="port"/> is null.</exception>
  184. public void RemoveForwardedPort(ForwardedPort port)
  185. {
  186. if (port == null)
  187. throw new ArgumentNullException("port");
  188. // Stop port forwarding before removing it
  189. port.Stop();
  190. DetachForwardedPort(port);
  191. _forwardedPorts.Remove(port);
  192. }
  193. private void AttachForwardedPort(ForwardedPort port)
  194. {
  195. if (port.Session != null && port.Session != Session)
  196. throw new InvalidOperationException("Forwarded port is already added to a different client.");
  197. port.Session = Session;
  198. }
  199. private static void DetachForwardedPort(ForwardedPort port)
  200. {
  201. port.Session = null;
  202. }
  203. /// <summary>
  204. /// Creates the command to be executed.
  205. /// </summary>
  206. /// <param name="commandText">The command text.</param>
  207. /// <returns><see cref="SshCommand"/> object.</returns>
  208. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  209. public SshCommand CreateCommand(string commandText)
  210. {
  211. return CreateCommand(commandText, ConnectionInfo.Encoding);
  212. }
  213. /// <summary>
  214. /// Creates the command to be executed with specified encoding.
  215. /// </summary>
  216. /// <param name="commandText">The command text.</param>
  217. /// <param name="encoding">The encoding to use for results.</param>
  218. /// <returns><see cref="SshCommand"/> object which uses specified encoding.</returns>
  219. /// <remarks>This method will change current default encoding.</remarks>
  220. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  221. /// <exception cref="ArgumentNullException"><paramref name="commandText"/> or <paramref name="encoding"/> is null.</exception>
  222. public SshCommand CreateCommand(string commandText, Encoding encoding)
  223. {
  224. EnsureSessionIsOpen();
  225. ConnectionInfo.Encoding = encoding;
  226. return new SshCommand(Session, commandText, encoding);
  227. }
  228. /// <summary>
  229. /// Creates and executes the command.
  230. /// </summary>
  231. /// <param name="commandText">The command text.</param>
  232. /// <returns>Returns an instance of <see cref="SshCommand"/> with execution results.</returns>
  233. /// <remarks>This method internally uses asynchronous calls.</remarks>
  234. /// <example>
  235. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.cs" region="Example SshCommand RunCommand Result" language="C#" title="Running simple command" />
  236. /// <code source="..\..\src\Renci.SshNet.Tests\Classes\SshCommandTest.NET40.cs" region="Example SshCommand RunCommand Parallel" language="C#" title="Run many commands in parallel" />
  237. /// </example>
  238. /// <exception cref="ArgumentException">CommandText property is empty.</exception>
  239. /// <exception cref="T:Renci.SshNet.Common.SshException">Invalid Operation - An existing channel was used to execute this command.</exception>
  240. /// <exception cref="InvalidOperationException">Asynchronous operation is already in progress.</exception>
  241. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  242. /// <exception cref="ArgumentNullException"><paramref name="commandText"/> is null.</exception>
  243. public SshCommand RunCommand(string commandText)
  244. {
  245. var cmd = CreateCommand(commandText);
  246. cmd.Execute();
  247. return cmd;
  248. }
  249. /// <summary>
  250. /// Creates the shell.
  251. /// </summary>
  252. /// <param name="input">The input.</param>
  253. /// <param name="output">The output.</param>
  254. /// <param name="extendedOutput">The extended output.</param>
  255. /// <param name="terminalName">Name of the terminal.</param>
  256. /// <param name="columns">The columns.</param>
  257. /// <param name="rows">The rows.</param>
  258. /// <param name="width">The width.</param>
  259. /// <param name="height">The height.</param>
  260. /// <param name="terminalModes">The terminal mode.</param>
  261. /// <param name="bufferSize">Size of the internal read buffer.</param>
  262. /// <returns>
  263. /// Returns a representation of a <see cref="Shell" /> object.
  264. /// </returns>
  265. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  266. public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes, int bufferSize)
  267. {
  268. EnsureSessionIsOpen();
  269. return new Shell(Session, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, bufferSize);
  270. }
  271. /// <summary>
  272. /// Creates the shell.
  273. /// </summary>
  274. /// <param name="input">The input.</param>
  275. /// <param name="output">The output.</param>
  276. /// <param name="extendedOutput">The extended output.</param>
  277. /// <param name="terminalName">Name of the terminal.</param>
  278. /// <param name="columns">The columns.</param>
  279. /// <param name="rows">The rows.</param>
  280. /// <param name="width">The width.</param>
  281. /// <param name="height">The height.</param>
  282. /// <param name="terminalModes">The terminal mode.</param>
  283. /// <returns>
  284. /// Returns a representation of a <see cref="Shell" /> object.
  285. /// </returns>
  286. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  287. public Shell CreateShell(Stream input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes)
  288. {
  289. return CreateShell(input, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, 1024);
  290. }
  291. /// <summary>
  292. /// Creates the shell.
  293. /// </summary>
  294. /// <param name="input">The input.</param>
  295. /// <param name="output">The output.</param>
  296. /// <param name="extendedOutput">The extended output.</param>
  297. /// <returns>
  298. /// Returns a representation of a <see cref="Shell" /> object.
  299. /// </returns>
  300. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  301. public Shell CreateShell(Stream input, Stream output, Stream extendedOutput)
  302. {
  303. return CreateShell(input, output, extendedOutput, string.Empty, 0, 0, 0, 0, null, 1024);
  304. }
  305. /// <summary>
  306. /// Creates the shell.
  307. /// </summary>
  308. /// <param name="encoding">The encoding to use to send the input.</param>
  309. /// <param name="input">The input.</param>
  310. /// <param name="output">The output.</param>
  311. /// <param name="extendedOutput">The extended output.</param>
  312. /// <param name="terminalName">Name of the terminal.</param>
  313. /// <param name="columns">The columns.</param>
  314. /// <param name="rows">The rows.</param>
  315. /// <param name="width">The width.</param>
  316. /// <param name="height">The height.</param>
  317. /// <param name="terminalModes">The terminal mode.</param>
  318. /// <param name="bufferSize">Size of the internal read buffer.</param>
  319. /// <returns>
  320. /// Returns a representation of a <see cref="Shell" /> object.
  321. /// </returns>
  322. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  323. public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes, int bufferSize)
  324. {
  325. // TODO let shell dispose of input stream when we own the stream!
  326. _inputStream = new MemoryStream();
  327. var writer = new StreamWriter(_inputStream, encoding);
  328. writer.Write(input);
  329. writer.Flush();
  330. _inputStream.Seek(0, SeekOrigin.Begin);
  331. return CreateShell(_inputStream, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, bufferSize);
  332. }
  333. /// <summary>
  334. /// Creates the shell.
  335. /// </summary>
  336. /// <param name="encoding">The encoding.</param>
  337. /// <param name="input">The input.</param>
  338. /// <param name="output">The output.</param>
  339. /// <param name="extendedOutput">The extended output.</param>
  340. /// <param name="terminalName">Name of the terminal.</param>
  341. /// <param name="columns">The columns.</param>
  342. /// <param name="rows">The rows.</param>
  343. /// <param name="width">The width.</param>
  344. /// <param name="height">The height.</param>
  345. /// <param name="terminalModes">The terminal modes.</param>
  346. /// <returns>
  347. /// Returns a representation of a <see cref="Shell" /> object.
  348. /// </returns>
  349. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  350. public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput, string terminalName, uint columns, uint rows, uint width, uint height, IDictionary<TerminalModes, uint> terminalModes)
  351. {
  352. return CreateShell(encoding, input, output, extendedOutput, terminalName, columns, rows, width, height, terminalModes, 1024);
  353. }
  354. /// <summary>
  355. /// Creates the shell.
  356. /// </summary>
  357. /// <param name="encoding">The encoding.</param>
  358. /// <param name="input">The input.</param>
  359. /// <param name="output">The output.</param>
  360. /// <param name="extendedOutput">The extended output.</param>
  361. /// <returns>
  362. /// Returns a representation of a <see cref="Shell" /> object.
  363. /// </returns>
  364. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  365. public Shell CreateShell(Encoding encoding, string input, Stream output, Stream extendedOutput)
  366. {
  367. return CreateShell(encoding, input, output, extendedOutput, string.Empty, 0, 0, 0, 0, null, 1024);
  368. }
  369. /// <summary>
  370. /// Creates the shell stream.
  371. /// </summary>
  372. /// <param name="terminalName">The <c>TERM</c> environment variable.</param>
  373. /// <param name="columns">The terminal width in columns.</param>
  374. /// <param name="rows">The terminal width in rows.</param>
  375. /// <param name="width">The terminal height in pixels.</param>
  376. /// <param name="height">The terminal height in pixels.</param>
  377. /// <param name="bufferSize">Size of the buffer.</param>
  378. /// <returns>
  379. /// The created <see cref="ShellStream"/> instance.
  380. /// </returns>
  381. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  382. /// <remarks>
  383. /// <para>
  384. /// The <c>TERM</c> environment variable contains an identifier for the text window's capabilities.
  385. /// You can get a detailed list of these cababilities by using the ‘infocmp’ command.
  386. /// </para>
  387. /// <para>
  388. /// The column/row dimensions override the pixel dimensions(when nonzero). Pixel dimensions refer
  389. /// to the drawable area of the window.
  390. /// </para>
  391. /// </remarks>
  392. public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize)
  393. {
  394. return CreateShellStream(terminalName, columns, rows, width, height, bufferSize, null);
  395. }
  396. /// <summary>
  397. /// Creates the shell stream.
  398. /// </summary>
  399. /// <param name="terminalName">The <c>TERM</c> environment variable.</param>
  400. /// <param name="columns">The terminal width in columns.</param>
  401. /// <param name="rows">The terminal width in rows.</param>
  402. /// <param name="width">The terminal height in pixels.</param>
  403. /// <param name="height">The terminal height in pixels.</param>
  404. /// <param name="bufferSize">Size of the buffer.</param>
  405. /// <param name="terminalModeValues">The terminal mode values.</param>
  406. /// <returns>
  407. /// The created <see cref="ShellStream"/> instance.
  408. /// </returns>
  409. /// <exception cref="SshConnectionException">Client is not connected.</exception>
  410. /// <remarks>
  411. /// <para>
  412. /// The <c>TERM</c> environment variable contains an identifier for the text window's capabilities.
  413. /// You can get a detailed list of these cababilities by using the ‘infocmp’ command.
  414. /// </para>
  415. /// <para>
  416. /// The column/row dimensions override the pixel dimensions(when nonzero). Pixel dimensions refer
  417. /// to the drawable area of the window.
  418. /// </para>
  419. /// </remarks>
  420. public ShellStream CreateShellStream(string terminalName, uint columns, uint rows, uint width, uint height, int bufferSize, IDictionary<TerminalModes, uint> terminalModeValues)
  421. {
  422. EnsureSessionIsOpen();
  423. return new ShellStream(Session, terminalName, columns, rows, width, height, terminalModeValues);
  424. }
  425. /// <summary>
  426. /// Stops forwarded ports.
  427. /// </summary>
  428. protected override void OnDisconnected()
  429. {
  430. base.OnDisconnected();
  431. for (var i = _forwardedPorts.Count - 1; i >= 0; i--)
  432. {
  433. var port = _forwardedPorts[i];
  434. DetachForwardedPort(port);
  435. _forwardedPorts.RemoveAt(i);
  436. }
  437. }
  438. /// <summary>
  439. /// Releases unmanaged and - optionally - managed resources
  440. /// </summary>
  441. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  442. protected override void Dispose(bool disposing)
  443. {
  444. base.Dispose(disposing);
  445. if (_isDisposed)
  446. return;
  447. if (disposing)
  448. {
  449. if (_inputStream != null)
  450. {
  451. _inputStream.Dispose();
  452. _inputStream = null;
  453. }
  454. _isDisposed = true;
  455. }
  456. }
  457. private void EnsureSessionIsOpen()
  458. {
  459. if (Session == null)
  460. throw new SshConnectionException("Client not connected.");
  461. }
  462. }
  463. }