SshClient.cs 25 KB

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