2
0

SshClient.cs 25 KB

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