-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
86 lines (81 loc) · 2.87 KB
/
Program.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
using Expect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ExampleApp
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("ExampleApp");
ISession spawn = Expect.Expect.Spawn(new ProcessSpawnable("cmd.exe"));
spawn.Expect(">", s => Console.WriteLine("got: " + s));
spawn.Send("dir c:\\\n");
spawn.Expect("Program", (s) => Console.WriteLine("found: " + s));
spawn.Send("asdsdf\n");
spawn.Expect(">", (s) => Console.WriteLine("found: " + s));
spawn.Send("cd c:\\\n");
spawn.Expect(@">", s => spawn.Send("cd Users\n"));
spawn.Expect(@"c:\\Users>", s => Console.WriteLine("done\n" + s));
// Expect timeouts examples
spawn.Send("ping 8.8.8.8\n");
try
{
spawn.Expect("Ping statistics", s => Console.WriteLine(s));
}
catch (System.TimeoutException)
{
Console.WriteLine("Timeout 8.8.8.8!");
}
spawn.SetTimeout(5000);
spawn.Send("ping 8.8.4.4\n");
try
{
spawn.Expect("Ping statistics for 8.8.4.4", s => Console.WriteLine(s));
for (int i = 0; i < 6; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
catch (System.TimeoutException)
{
Console.WriteLine("Timeout 8.8.4.4!");
}
Console.WriteLine("Using ExpectAsync");
spawn.Send("ping 8.8.8.8\n");
spawn.Send("ping google.com\n");
try
{
spawn.ExpectAsync("Ping statistics for 8.8.8.8", s => Console.WriteLine(s));
for (int i = 0; i < 6; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
spawn.ExpectAsync("Ping statistics for", s => Console.WriteLine(s));
for (int i = 0; i < 6; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
catch (System.TimeoutException)
{
Console.WriteLine("Timeout 8.8.8.8!");
}
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
Console.ReadKey();
}
}
}