-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmdShell.cs
340 lines (284 loc) · 13.2 KB
/
CmdShell.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace CmdShellProj
{
/// <summary>
/// it3xl/cmd-multiple-commands-from-under-csharp
/// https://github.com/it3xl/cmd-multiple-commands-from-under-csharp
/// </summary>
public class CmdShell
{
/// <summary>
/// This method is for evolving by you and combines main techniques at once.
/// It is an exact anamog of <see cref="ExecAndShow"/>.
/// Also consider to look at <see cref="ExecAndShowCatched"/>.
/// </summary>
/// <param name="cmdCommands">CMD commands to be executed separated. Multi or a single line.</param>
/// <param name="executionLimit">The maximum duration limit for the entire execution. Default is 15 minutes.</param>
/// <param name="throwExceptions">Throw an exceptions in case of a non-zero exit code or exceeding the duration limit.</param>
public int ExecExample(string cmdCommands, TimeSpan? executionLimit = null, bool throwExceptions = false)
{
var commandsList = cmdCommands
.Replace("\r", string.Empty)
.Split('\n')
.ToList();
var info = new ProcessStartInfo
{
FileName = "cmd.exe",
// The false allows to access IO streams.
UseShellExecute = false,
// Allows write commands directly to a CMD-shell.
RedirectStandardInput = true,
};
var proc = new Process { StartInfo = info };
using (proc)
{
proc.Start();
commandsList.ForEach(command => proc
.StandardInput.WriteLine(command));
proc.StandardInput.WriteLine("@REM Exiting by CmdShell App. The last command sent.");
// Allows exiting from a CMD side.
proc.StandardInput.WriteLine("EXIT");
var span = executionLimit ?? TimeSpan.FromMinutes(15);
var milliseconds = span.TotalMilliseconds;
var duration = milliseconds < int.MaxValue
? (int)milliseconds
: int.MaxValue;
var interrupted = !proc.WaitForExit(duration);
if (throwExceptions)
{
if (interrupted)
throw new Exception("Duration limit is exceeded");
if (proc.ExitCode != 0)
throw new Exception(string.Format("Error exit code {0} received.", proc.ExitCode));
}
return proc.ExitCode;
}
}
/// <summary>
/// Executes CMD commands and shows outputs (stdout, stderr) on a console window.
/// </summary>
/// <param name="cmdCommands">CMD commands to be executed separated. Multi or a single line.</param>
/// <param name="throwExceptions">Throw an exceptions in case of a non-zero exit code or exceeding the duration limit.</param>
/// <param name="executionLimit">The maximum duration limit for the entire execution. Default is 15 minutes.</param>
public int ExecAndShow(string cmdCommands, TimeSpan? executionLimit = null, bool throwExceptions = false)
{
var exitCode = new Demonstrating(cmdCommands, executionLimit, throwExceptions)
.Exec();
return exitCode;
}
/// <summary>
/// Executes CMD commands and shows outputs (stdout, stderr) on a console window.
/// </summary>
private sealed class Demonstrating : CmdShellBase
{
private readonly int _executionLimitMillisec;
internal Demonstrating(string cmdCommands, TimeSpan? executionLimit, bool throwExceptions)
: base(cmdCommands, throwExceptions)
{
_executionLimitMillisec = GetMilliseconds(executionLimit);
}
internal override int Exec()
{
InitProcess();
using (Proc)
{
RunCommands();
var interrupted = !Proc.WaitForExit(_executionLimitMillisec);
Throw(interrupted, Proc.ExitCode);
return Proc.ExitCode;
}
}
}
/// <summary>
/// In some cases you must catch huge outputs, otherwise you shell will fail.
/// This method executes CMD commands, captures all outputs (stdout and stderr, not only stdin)
/// and passes them to your shell.
/// </summary>
/// <param name="cmdCommands">CMD commands to be executed separated. Multi or a single line.</param>
/// <param name="throwExceptions">Throw an exceptions in case of a non-zero exit code or exceeding the duration limit.</param>
/// <param name="collectOutputs">Instructs to collect all console outputs.</param>
/// <param name="outputWaitLimit">The maximum duration limit for any output waiting from a CMD-shell. Default is 15 minutes.</param>
public int ExecAndShowCatched(string cmdCommands, bool throwExceptions = false, bool collectOutputs = false, TimeSpan? outputWaitLimit = null)
{
var exitCode = new OutputCatcher(cmdCommands, throwExceptions, collectOutputs, outputWaitLimit)
.Exec();
return exitCode;
}
/// <summary>
/// In some cases you must catch huge outputs, otherwise you shell will fail.
/// This method executes CMD commands, captures all outputs (stdout and stderr, not only stdin)
/// and passes them to your shell.
/// </summary>
/// <param name="cmdCommands">CMD commands to be executed separated. Multi or a single line.</param>
/// <param name="outputCombined">Combined CMD outputs of stdout and stderr.</param>
/// <param name="throwExceptions">Throw an exceptions in case of a non-zero exit code or exceeding the duration limit.</param>
/// <param name="outputWaitLimit">The maximum duration limit for any output waiting from a CMD-shell. Default is 15 minutes.</param>
public int ExecAndShowCatched(string cmdCommands, out StringBuilder outputCombined, bool throwExceptions = false, TimeSpan? outputWaitLimit = null)
{
var outputer = new OutputCatcher(cmdCommands, throwExceptions, true, outputWaitLimit);
var exitCode = outputer.Exec();
outputCombined = outputer.OutputCombined;
return exitCode;
}
/// <summary>
/// Executes CMD commands and catches outputs (stdout, stderr) from the CMD-console.
/// It is mostly for debug purposes, so you prefer to use the CMD Redirection to a log-file.
/// </summary>
private sealed class OutputCatcher : CmdShellBase
{
private readonly int _outputWaitingLimit;
private readonly bool _collectOutputs;
internal readonly StringBuilder OutputCombined = new StringBuilder();
internal OutputCatcher(string cmdCommands,
bool throwExceptions = false,
bool collectOutputs = false,
TimeSpan? outputWaitingLimit = null)
: base(cmdCommands, throwExceptions)
{
_outputWaitingLimit = GetMilliseconds(outputWaitingLimit);
_collectOutputs = collectOutputs;
}
internal override int Exec()
{
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.RedirectStandardError = true;
InitProcess();
using (Proc)
{
Proc.OutputDataReceived += DataReceived;
Proc.BeginOutputReadLine();
Proc.ErrorDataReceived += DataReceived;
Proc.BeginErrorReadLine();
RunCommands();
var interrupted = false;
while (!interrupted && !Proc.HasExited)
{
interrupted = !Proc.WaitForExit(_outputWaitingLimit);
}
if (Debugger.IsAttached)
{
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
}
Throw(interrupted, Proc.ExitCode);
return Proc.ExitCode;
}
}
private void DataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
return;
// Passes CMD's outputs to your process' console.
// It must be here, otherwise we'll get a mess from stdout & stderr in _outputCombined at the beginning.
Console.WriteLine(e.Data);
if (!_collectOutputs)
return;
lock (OutputCombined)
{
if (e.Data != null)
{
OutputCombined.AppendLine(e.Data);
}
}
}
protected override void Throw(bool interrupted, int exitCode)
{
if (!ThrowExceptions)
return;
if (!_collectOutputs)
{
base.Throw(interrupted, exitCode);
return;
}
string catchedOutput;
lock (OutputCombined)
{
catchedOutput = OutputCombined.ToString();
}
if (interrupted)
{
throw new Exception(string.Format("Duration limit is exceeded.\nOutput:\n{0}", catchedOutput));
}
if (exitCode != 0)
{
throw new Exception(string.Format("Error exit code {0} received.\nOutput:\n{1}", exitCode, catchedOutput));
}
}
}
private abstract class CmdShellBase
{
private List<string> CommandsList { get; set; }
protected bool ThrowExceptions { get; private set; }
protected ProcessStartInfo ProcStartInfo { get; private set; }
protected Process Proc { get; private set; }
protected CmdShellBase(string cmdCommands, bool throwExceptions)
{
CommandsList = cmdCommands
.Replace("\r", string.Empty)
.Split('\n')
.ToList();
ThrowExceptions = throwExceptions;
ProcStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
// The false allows to access IO streams.
UseShellExecute = false,
// Allows write commands directly to a CMD-shell.
RedirectStandardInput = true,
};
}
internal abstract int Exec();
protected virtual void Throw(bool interrupted, int exitCode)
{
if (!ThrowExceptions)
return;
if (interrupted)
throw new Exception("Duration limit is exceeded");
if (exitCode != 0)
// ReSharper disable once UseStringInterpolation
throw new Exception(string.Format("Error exit code {0} received.", exitCode));
}
protected void InitProcess()
{
Proc = new Process { StartInfo = ProcStartInfo };
try
{
Proc.Start();
}
catch (Exception)
{
Proc.Dispose();
throw;
}
}
protected void RunCommands()
{
CommandsList.ForEach(command => Proc.StandardInput.WriteLine(command));
FinishCmd(Proc.StandardInput);
}
protected int GetMilliseconds(TimeSpan? timeSpan)
{
var span = timeSpan ?? TimeSpan.FromMinutes(15);
var milliseconds = span.TotalMilliseconds;
var duration = milliseconds < int.MaxValue
? (int)milliseconds
: int.MaxValue;
return duration;
}
/// <summary>
/// Allows exiting from a CMD side. Required.
/// </summary>
/// <param name="cmdInput"></param>
private void FinishCmd(StreamWriter cmdInput)
{
cmdInput.WriteLine("@REM Exiting by CmdShell App. The last command sent.");
// Allows exiting from CMD side.
cmdInput.WriteLine("EXIT");
}
}
}
}