-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
107 lines (89 loc) · 2.5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.IO;
using System.Diagnostics;
using System.Timers;
namespace vlanhops
{
class MainClass
{
const string networkInterface = "eth0";
public static void Main (string[] args)
{
for (int i = 1; i < 903; i++) {
if (createVlan (i)) {
getIP (networkInterface + "." + i);
removeVlan (i);
}
}
}
private static bool createVlan(int vlanID){
Console.WriteLine ("CREATE VLAN:" + vlanID);
VLANConfig vlc = new VLANConfig ();
return (vlc.add ("eth0", vlanID.ToString()));
ProcessStartInfo psi = new ProcessStartInfo ();
psi.FileName = "/opt/vlan/vconfig";
psi.Arguments = "add " + networkInterface + " " + vlanID;
Process p = CreateProcAndWait (psi, 1);
Console.WriteLine (p.StandardOutput.ReadToEnd ());
if (p.HasExited) {
return true;
}
return false;
}
private static bool removeVlan(int vlanID){
Console.WriteLine ("REMOVE VLAN:" + vlanID);
VLANConfig vlc = new VLANConfig ();
return (vlc.rem ("eth0." + vlanID));
ProcessStartInfo psi = new ProcessStartInfo ();
psi.FileName = "/opt/vlan/vconfig";
psi.Arguments = "rem " + networkInterface + "." + vlanID;
Process p = CreateProcAndWait (psi, 1);
if (p.HasExited) {
return true;
}
return false;
}
private static bool getIP(string interfaceName){
Console.WriteLine ("RUNNING DHCLIENT ON " + interfaceName);
ProcessStartInfo psi = new ProcessStartInfo ();
psi.FileName = "/sbin/dhclient";
psi.Arguments = interfaceName;
Process p = CreateProcAndWait (psi,10);
if (p.HasExited) {
//success
Console.WriteLine(String.Format("Got IP on interface: {0}",interfaceName));
getIPForInterfaceName(interfaceName);
return true;
}
Console.WriteLine ("NO IP");
return false;
}
private static void getIPForInterfaceName(string interfaceName){
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/sbin/ifconfig";
Process p = CreateProcAndWait(psi,1);
if (p.HasExited){
Console.Write(p.StandardOutput.ReadToEnd());
}
}
private static Process CreateProcAndWait(ProcessStartInfo psi, int sleeps){
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
int sleepCounter = 0;
while (!p.HasExited) {
System.Threading.Thread.Sleep (1000);
if (sleepCounter < sleeps) {
Console.Write ("*");
sleepCounter++;
} else {
break;
}
}
Console.WriteLine ("");
return p;
}
}
}