masscheckin ganz viel kram
This commit is contained in:
@@ -7,297 +7,284 @@ using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
class SetNetworks : ActionClass
|
||||
{
|
||||
private string netsh_output = "";
|
||||
private bool setNetworkWins(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 2)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkWins class");
|
||||
return false;
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
string netsh = "interface ipv4 set winsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip);
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh(netsh);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
namespace NetMonitorServer {
|
||||
class SetNetworks : ActionClass {
|
||||
private string netsh_output = "";
|
||||
|
||||
private bool setNetworkDns(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 2)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkDns class");
|
||||
return false;
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + " primary");
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh(netsh);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setNetworkIp(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 2 && data.Count != 4)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkIp class");
|
||||
return false;
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string netsh = "";
|
||||
if (ip == "auto")
|
||||
{
|
||||
string name = data.Dequeue();
|
||||
netsh = "interface ipv4 set address name=\"" + name + "\" source=dhcp";
|
||||
}
|
||||
else
|
||||
{
|
||||
string subnet = data.Dequeue();
|
||||
string gw = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
netsh = "interface ipv4 set address \"" + name + "\" static " + ip + " " + subnet + " " + gw;
|
||||
}
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh(netsh);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setNetworkAuto(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 1)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkAuto class");
|
||||
return false;
|
||||
}
|
||||
string name = data.Dequeue();
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh("interface ipv4 set address name=\"" + name + "\" source=dhcp");
|
||||
this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + name + "\" source=dhcp");
|
||||
this.runProgramNetsh("interface ipv4 set winsservers name=\"" + name + "\" source=dhcp");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool getNetwork(Queue<string> arguments)
|
||||
{
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetNetworks.getNetwork class");
|
||||
return false;
|
||||
}
|
||||
string adapter = arguments.Dequeue();
|
||||
string ip = "";
|
||||
string subnet = "";
|
||||
string gateway = "";
|
||||
string dns = "";
|
||||
string wins = "";
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh("interface ipv4 show interfaces");
|
||||
string[] text = this.netsh_output.Trim().Split('\n');
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "disconnected[ ]+"+adapter+"$", RegexOptions.IgnoreCase))
|
||||
{
|
||||
this.sendMessage("disconnected");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show addresses");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
bool match = false;
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^[a-z0-9 ]+\""+adapter+"\"", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = true;
|
||||
}
|
||||
if (match)
|
||||
{
|
||||
if (Regex.IsMatch(l, "DHCP aktiviert:\\s+Ja", RegexOptions.IgnoreCase))
|
||||
{
|
||||
ip = "auto";
|
||||
break;
|
||||
}
|
||||
if (Regex.IsMatch(l, "IP-Adresse:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
ip = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
if (Regex.IsMatch(l, "Subnetzpräfix:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\/[0-9]+ \\(Maske [0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)", RegexOptions.IgnoreCase))
|
||||
{
|
||||
subnet = Regex.Match(l, "\\(Maske ([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)\\)").Groups[1].ToString();
|
||||
}
|
||||
if (Regex.IsMatch(l, "Standardgateway:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
gateway = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show dnsservers");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
match = false;
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = true;
|
||||
}
|
||||
if (match)
|
||||
{
|
||||
if (Regex.IsMatch(l, "Über DHCP konfigurierte DNS-Server:\\s+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
dns = "auto";
|
||||
}
|
||||
if (Regex.IsMatch(l, "Statisch konfigurierte DNS-Server:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
dns = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show winsservers");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
match = false;
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = true;
|
||||
}
|
||||
if (match)
|
||||
{
|
||||
if (Regex.IsMatch(l, "Über DHCP konfigurierte WINS-Server:\\s+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
wins = "auto";
|
||||
}
|
||||
if (Regex.IsMatch(l, "Statisch konfigurierte WINS-Server:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
wins = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if (ip == "")
|
||||
{
|
||||
this.sendMessage("unknown");
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("[" + ip + "|" + subnet + "|" + gateway + "|" + dns + "|" + wins + "]");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool runProgramNetsh(string args)
|
||||
{
|
||||
//Console.WriteLine("netsh args: "+args);
|
||||
Process p = new Process();
|
||||
p.StartInfo.Arguments = args;
|
||||
p.StartInfo.FileName = "netsh";
|
||||
p.StartInfo.CreateNoWindow = true;
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.Start();
|
||||
this.netsh_output = p.StandardOutput.ReadToEnd();
|
||||
p.WaitForExit();
|
||||
//Console.WriteLine(this.netsh_output.Trim());
|
||||
if (p.ExitCode == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
throw new Exception(this.netsh_output.Trim());
|
||||
}
|
||||
internal override bool Run(Queue<string> arguments)
|
||||
{
|
||||
if (arguments.Count < 2)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetNetwork class");
|
||||
return false;
|
||||
}
|
||||
switch (arguments.Dequeue().ToLower())
|
||||
{
|
||||
case "auto": return this.setNetworkAuto(arguments);
|
||||
case "ip": return this.setNetworkIp(arguments);
|
||||
case "dns": return this.setNetworkDns(arguments);
|
||||
case "wins": return this.setNetworkWins(arguments);
|
||||
case "network": return this.getNetwork(arguments);
|
||||
default: this.sendMessage("Wrong arguments for service on SetNetwork class"); return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal override bool Run(Queue<string> arguments) {
|
||||
this.addLog("SetNetworks.Run", "Setze Netzwerk Befehl", LogLevel.Notice);
|
||||
if(arguments.Count < 1) {
|
||||
this.sendMessage("To less arguments for SetNetwork class");
|
||||
return false;
|
||||
}
|
||||
switch(arguments.Dequeue().ToLower()) {
|
||||
case "auto":
|
||||
return this.setNetworkAuto(arguments);
|
||||
case "ip":
|
||||
return this.setNetworkIp(arguments);
|
||||
case "dns":
|
||||
return this.setNetworkDns(arguments);
|
||||
case "wins":
|
||||
return this.setNetworkWins(arguments);
|
||||
case "network":
|
||||
return this.getNetwork(arguments);
|
||||
case "adapters":
|
||||
return this.getAdapters(arguments);
|
||||
default:
|
||||
this.sendMessage("Wrong arguments for service on SetNetwork class");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool getAdapters(Queue<string> arguments) {
|
||||
if(arguments.Count != 0) {
|
||||
this.sendMessage("To less arguments for SetNetworks.getAdapters class");
|
||||
return false;
|
||||
}
|
||||
this.addLog("SetNetworks.getNetwork", "Lese Liste Adapter", LogLevel.Notice);
|
||||
List<string> ret = new List<string>();
|
||||
try {
|
||||
this.runProgramNetsh("interface ipv4 show interfaces");
|
||||
string[] text = this.netsh_output.Trim().Split('\n');
|
||||
foreach(string line in text) {
|
||||
string l = line.Trim();
|
||||
if(l == "") {
|
||||
continue;
|
||||
}
|
||||
if(Regex.IsMatch(l, "(disconnected|connected) [ ]+([^ ].*)$", RegexOptions.IgnoreCase)) {
|
||||
ret.Add(Regex.Match(l, "(disconnected|connected) [ ]+([^ ].*)$").Groups[2].ToString());
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if(ret.Count == 0) {
|
||||
this.sendMessage("unknown");
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("[" + String.Join("|", ret.ToArray()) + "]");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setNetworkWins(Queue<string> data) {
|
||||
if(data.Count != 2) {
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkWins class");
|
||||
return false;
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
this.addLog("SetNetworks.setNetworkWins", "Setzte Wins auf " + name + " mit " + ip, LogLevel.Notice);
|
||||
string netsh = "interface ipv4 set winsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip);
|
||||
try {
|
||||
this.runProgramNetsh(netsh);
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setNetworkDns(Queue<string> data) {
|
||||
if(data.Count != 2) {
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkDns class");
|
||||
return false;
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
this.addLog("SetNetworks.setNetworkDns", "Setzte Dns auf " + name + " mit " + ip, LogLevel.Notice);
|
||||
string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + " primary");
|
||||
try {
|
||||
this.runProgramNetsh(netsh);
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setNetworkIp(Queue<string> data) {
|
||||
if(data.Count != 2 && data.Count != 4) {
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkIp class");
|
||||
return false;
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string netsh = "";
|
||||
if(ip == "auto") {
|
||||
string name = data.Dequeue();
|
||||
this.addLog("SetNetworks.setNetworkIp", "Setzte Ip auf " + name + " mit " + ip, LogLevel.Notice);
|
||||
netsh = "interface ipv4 set address name=\"" + name + "\" source=dhcp";
|
||||
} else {
|
||||
string subnet = data.Dequeue();
|
||||
string gw = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
this.addLog("SetNetworks.setNetworkIp", "Setzte Ip auf " + name + " mit " + ip, LogLevel.Notice);
|
||||
netsh = "interface ipv4 set address \"" + name + "\" static " + ip + " " + subnet + " " + gw;
|
||||
}
|
||||
try {
|
||||
this.runProgramNetsh(netsh);
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setNetworkAuto(Queue<string> data) {
|
||||
if(data.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkAuto class");
|
||||
return false;
|
||||
}
|
||||
string name = data.Dequeue();
|
||||
this.addLog("SetNetworks.setNetworkAuto", "Setzte DHCP auf " + name, LogLevel.Notice);
|
||||
try {
|
||||
this.runProgramNetsh("interface ipv4 set address name=\"" + name + "\" source=dhcp");
|
||||
this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + name + "\" source=dhcp");
|
||||
this.runProgramNetsh("interface ipv4 set winsservers name=\"" + name + "\" source=dhcp");
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool getNetwork(Queue<string> arguments) {
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetNetworks.getNetwork class");
|
||||
return false;
|
||||
}
|
||||
string adapter = arguments.Dequeue();
|
||||
this.addLog("SetNetworks.getNetwork", "Lese Netzwerk auf " + adapter, LogLevel.Notice);
|
||||
string ip = "";
|
||||
string subnet = "";
|
||||
string gateway = "";
|
||||
string dns = "";
|
||||
string wins = "";
|
||||
try {
|
||||
this.runProgramNetsh("interface ipv4 show interfaces");
|
||||
string[] text = this.netsh_output.Trim().Split('\n');
|
||||
foreach(string line in text) {
|
||||
string l = line.Trim();
|
||||
if(l == "") {
|
||||
continue;
|
||||
}
|
||||
if(Regex.IsMatch(l, "disconnected[ ]+" + adapter + "$", RegexOptions.IgnoreCase)) {
|
||||
this.sendMessage("disconnected");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show addresses");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
bool match = false;
|
||||
foreach(string line in text) {
|
||||
string l = line.Trim();
|
||||
if(l == "") {
|
||||
continue;
|
||||
}
|
||||
if(Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase)) {
|
||||
match = false;
|
||||
}
|
||||
if(Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase)) {
|
||||
match = true;
|
||||
}
|
||||
if(match) {
|
||||
if(Regex.IsMatch(l, "DHCP aktiviert:\\s+Ja", RegexOptions.IgnoreCase)) {
|
||||
ip = "auto";
|
||||
break;
|
||||
}
|
||||
if(Regex.IsMatch(l, "IP-Adresse:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase)) {
|
||||
ip = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
if(Regex.IsMatch(l, "Subnetzpr.fix:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\/[0-9]+ \\(Maske [0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)", RegexOptions.IgnoreCase)) {
|
||||
subnet = Regex.Match(l, "\\(Maske ([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)\\)").Groups[1].ToString();
|
||||
}
|
||||
if(Regex.IsMatch(l, "Standardgateway:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase)) {
|
||||
gateway = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show dnsservers");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
match = false;
|
||||
foreach(string line in text) {
|
||||
string l = line.Trim();
|
||||
if(l == "") {
|
||||
continue;
|
||||
}
|
||||
if(Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase)) {
|
||||
match = false;
|
||||
}
|
||||
if(Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase)) {
|
||||
match = true;
|
||||
}
|
||||
if(match) {
|
||||
if(Regex.IsMatch(l, "Über DHCP konfigurierte DNS-Server:\\s+", RegexOptions.IgnoreCase)) {
|
||||
dns = "auto";
|
||||
}
|
||||
if(Regex.IsMatch(l, "Statisch konfigurierte DNS-Server:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase)) {
|
||||
dns = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show winsservers");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
match = false;
|
||||
foreach(string line in text) {
|
||||
string l = line.Trim();
|
||||
if(l == "") {
|
||||
continue;
|
||||
}
|
||||
if(Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase)) {
|
||||
match = false;
|
||||
}
|
||||
if(Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase)) {
|
||||
match = true;
|
||||
}
|
||||
if(match) {
|
||||
if(Regex.IsMatch(l, "Über DHCP konfigurierte WINS-Server:\\s+", RegexOptions.IgnoreCase)) {
|
||||
wins = "auto";
|
||||
}
|
||||
if(Regex.IsMatch(l, "Statisch konfigurierte WINS-Server:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase)) {
|
||||
wins = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if(ip == "") {
|
||||
this.sendMessage("unknown");
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("[" + ip + "|" + subnet + "|" + gateway + "|" + dns + "|" + wins + "]");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool runProgramNetsh(string args) {
|
||||
this.addLog("SetNetworks.runProgramNetsh", "Netsh: " + args, LogLevel.Info);
|
||||
//Console.WriteLine("netsh args: "+args);
|
||||
Process p = new Process();
|
||||
p.StartInfo.Arguments = args;
|
||||
p.StartInfo.FileName = "netsh";
|
||||
p.StartInfo.CreateNoWindow = true;
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.Start();
|
||||
this.netsh_output = p.StandardOutput.ReadToEnd();
|
||||
p.WaitForExit();
|
||||
//Console.WriteLine(this.netsh_output.Trim());
|
||||
if(p.ExitCode == 0) {
|
||||
return true;
|
||||
}
|
||||
throw new Exception(this.netsh_output.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,13 +10,14 @@ namespace NetMonitorServer {
|
||||
class SetOpenVPNService : ActionClass {
|
||||
|
||||
internal override bool Run(Queue<string> arguments) {
|
||||
this.addLog("SetOpenVPNService.Run", "Starte OpenVpn Befehl", LogLevel.Notice);
|
||||
if(arguments.Count < 2) {
|
||||
this.sendMessage("To less arguments for SetOpenVpnService class");
|
||||
return false;
|
||||
}
|
||||
switch(arguments.Dequeue().ToLower()) {
|
||||
case "status":
|
||||
return this.getVpn(arguments);
|
||||
return this.getVpnStatus(arguments);
|
||||
case"start":
|
||||
return this.getVpnStart(arguments);
|
||||
case"stop":
|
||||
@@ -28,7 +29,12 @@ namespace NetMonitorServer {
|
||||
}
|
||||
|
||||
private bool getVpnStop(Queue<string> arguments) {
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetOpenVpnService.getVpnStop");
|
||||
return false;
|
||||
}
|
||||
string config = arguments.Dequeue();
|
||||
this.addLog("SetOpenVPNService.getVpnStop", "Stoppe OpenVPN: "+config, LogLevel.Notice);
|
||||
if(Model.openvpnInstances.Keys.Contains(config)) { //schon in der Liste
|
||||
OpenVpnController o = Model.openvpnInstances[config];
|
||||
if(o.HasExited) { //läuft nicht! Fehler!
|
||||
@@ -41,11 +47,16 @@ namespace NetMonitorServer {
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool getVpnStart(Queue<string> arguments) {
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetOpenVpnService.getVpnStart");
|
||||
return false;
|
||||
}
|
||||
string config = arguments.Dequeue();
|
||||
this.addLog("SetOpenVPNService.getVpnStart", "Starte OpenVPN: " + config, LogLevel.Notice);
|
||||
if(Model.openvpnInstances.Keys.Contains(config)) { //schon in der Liste
|
||||
OpenVpnController o = Model.openvpnInstances[config];
|
||||
if(!o.HasExited) { //läuft noch! Fehler!
|
||||
@@ -58,32 +69,30 @@ namespace NetMonitorServer {
|
||||
this.sendMessage("Fehler beim Starten von OpenVPN: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
} else { //Noch nicht in der liste
|
||||
string ovpn = InIReader.getInstance("config.ini").getValue("program", "openvpn");
|
||||
string ovpn = Factory.getSettings().getValue("program", "openvpn");
|
||||
ovpn = (ovpn.EndsWith("\\")) ? ovpn : ovpn + "\\";
|
||||
try {
|
||||
OpenVpnController o = new OpenVpnController(ovpn, config);
|
||||
o.EventLog += this.addLog;
|
||||
Model.openvpnInstances.Add(config, o);
|
||||
o.Start();
|
||||
} catch(Exception e) {
|
||||
this.sendMessage("Fehler beim Starten von OpenVPN: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private bool getVpn(Queue<string> arguments) {
|
||||
private bool getVpnStatus(Queue<string> arguments) {
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetOpenVpnService.getVpn class");
|
||||
this.sendMessage("To less arguments for SetOpenVpnService.getVpnStatus");
|
||||
return false;
|
||||
}
|
||||
string config = arguments.Dequeue();
|
||||
this.addLog("SetOpenVPNService.getVpnStatus", "Status des OpenVPN: " + config, LogLevel.Notice);
|
||||
string run = "";
|
||||
try {
|
||||
if(Model.openvpnInstances.Keys.Contains(config)) {
|
||||
|
||||
@@ -7,109 +7,93 @@ using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.ServiceProcess;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
class SetService : ActionClass
|
||||
{
|
||||
ServiceController serviceController;
|
||||
namespace NetMonitorServer {
|
||||
class SetService : ActionClass {
|
||||
ServiceController serviceController;
|
||||
|
||||
private bool setServiceStart(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 1)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetService.setServiceStart class");
|
||||
return false;
|
||||
}
|
||||
this.serviceController.ServiceName = data.Dequeue();
|
||||
try
|
||||
{
|
||||
this.serviceController.Start();
|
||||
this.serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if (this.serviceController.Status == ServiceControllerStatus.Running)
|
||||
{
|
||||
this.sendMessage("true");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sendMessage("The service isn't started");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setServiceStop(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 1)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetService.setServiceStop class");
|
||||
return false;
|
||||
}
|
||||
this.serviceController.ServiceName = data.Dequeue();
|
||||
try
|
||||
{
|
||||
this.serviceController.Stop();
|
||||
this.serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if (this.serviceController.Status == ServiceControllerStatus.Stopped)
|
||||
{
|
||||
this.sendMessage("true");
|
||||
}
|
||||
else
|
||||
{
|
||||
this.sendMessage("The service isn't stopped");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool getServiceStatus(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 1)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetService.setServiceStatus class");
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
this.serviceController.ServiceName = data.Dequeue();
|
||||
if (this.serviceController.Status == ServiceControllerStatus.Running)
|
||||
this.sendMessage("running");
|
||||
else
|
||||
this.sendMessage("stopped");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
internal override bool Run(Queue<string> arguments)
|
||||
{
|
||||
if (arguments.Count != 2)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetService class");
|
||||
return false;
|
||||
}
|
||||
this.serviceController = new ServiceController();
|
||||
switch (arguments.Dequeue().ToLower())
|
||||
{
|
||||
case "start": return this.setServiceStart(arguments);
|
||||
case "stop": return this.setServiceStop(arguments);
|
||||
case "status": return this.getServiceStatus(arguments);
|
||||
default: this.sendMessage("Wrong arguments for service on SetService class"); return false;
|
||||
}
|
||||
}
|
||||
internal override bool Run(Queue<string> arguments) {
|
||||
this.addLog("SetService.Run", "Starte Service Befehl", LogLevel.Notice);
|
||||
if(arguments.Count != 2) {
|
||||
this.sendMessage("To less arguments for SetService class");
|
||||
return false;
|
||||
}
|
||||
this.serviceController = new ServiceController();
|
||||
switch(arguments.Dequeue().ToLower()) {
|
||||
case "start":
|
||||
return this.setServiceStart(arguments);
|
||||
case "stop":
|
||||
return this.setServiceStop(arguments);
|
||||
case "status":
|
||||
return this.getServiceStatus(arguments);
|
||||
default:
|
||||
this.sendMessage("Wrong arguments for service on SetService class");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool setServiceStart(Queue<string> data) {
|
||||
if(data.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetService.setServiceStart class");
|
||||
return false;
|
||||
}
|
||||
this.serviceController.ServiceName = data.Dequeue();
|
||||
this.addLog("SetService.setServiceStart", "Starte Service: " + this.serviceController.ServiceName, LogLevel.Notice);
|
||||
try {
|
||||
this.serviceController.Start();
|
||||
this.serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if(this.serviceController.Status == ServiceControllerStatus.Running) {
|
||||
this.sendMessage("true");
|
||||
} else {
|
||||
this.sendMessage("The service isn't started");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool setServiceStop(Queue<string> data) {
|
||||
if(data.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetService.setServiceStop class");
|
||||
return false;
|
||||
}
|
||||
this.serviceController.ServiceName = data.Dequeue();
|
||||
this.addLog("SetService.setServiceStop", "Stoppe Service: " + this.serviceController.ServiceName, LogLevel.Notice);
|
||||
try {
|
||||
this.serviceController.Stop();
|
||||
this.serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if(this.serviceController.Status == ServiceControllerStatus.Stopped) {
|
||||
this.sendMessage("true");
|
||||
} else {
|
||||
this.sendMessage("The service isn't stopped");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool getServiceStatus(Queue<string> data) {
|
||||
if(data.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetService.setServiceStatus class");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
this.serviceController.ServiceName = data.Dequeue();
|
||||
this.addLog("SetService.setServiceStatus", "Status des Service: " + this.serviceController.ServiceName, LogLevel.Notice);
|
||||
if(this.serviceController.Status == ServiceControllerStatus.Running)
|
||||
this.sendMessage("running");
|
||||
else
|
||||
this.sendMessage("stopped");
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user