2014-07-11 16:38:10 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
2015-03-04 23:21:34 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
namespace NetMonitorServer {
|
|
|
|
|
class SetNetworks : ActionClass {
|
|
|
|
|
private string netsh_output = "";
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
} 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;
|
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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");
|
2014-07-11 16:38:10 +02:00
|
|
|
|
return true;
|
2015-11-16 01:10:59 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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;
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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();
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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();
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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();
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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";
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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();
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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";
|
2015-03-04 23:21:34 +01:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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();
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
} 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;
|
|
|
|
|
}
|
2015-03-04 23:21:34 +01:00
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
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());
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
}
|
2014-07-11 16:38:10 +02:00
|
|
|
|
}
|