using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; namespace NetMonitorServer { class SetNetworks : ActionClass { private string netsh_output = ""; private bool setNetworkWins(Queue 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; } private bool setNetworkDns(Queue 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 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 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 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 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; } } } }