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