From c613dfff9f9369de71ee5e7b89f21a32044314c4 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 28 Apr 2010 21:09:00 +0000 Subject: [PATCH] Erste Funktionierende Version die Netzwerke einstellen kann --- NetMonitorClient/Form1.cs | 15 ++++- NetMonitorClient/ServiceControl.cs | 96 ++++++++++++++++++++++++++---- NetMonitorServer/Service1.cs | 2 +- NetMonitorServer/SetNetworks.cs | 78 +++++++++++++++++------- 4 files changed, 156 insertions(+), 35 deletions(-) diff --git a/NetMonitorClient/Form1.cs b/NetMonitorClient/Form1.cs index 85ce1d6..bb675c9 100644 --- a/NetMonitorClient/Form1.cs +++ b/NetMonitorClient/Form1.cs @@ -38,9 +38,18 @@ namespace NetMonitorClient this.sc.setNetworkAuto(adapter); else { - this.sc.setNetworkIp(ip, subnet, gateway); - this.sc.setNetworkDNS(dns); - this.sc.setNetworkWINS(wins); + if (ip == "auto") + this.sc.setNetworkIpAuto(adapter); + else + this.sc.setNetworkIp(ip, subnet, gateway, adapter); + if(dns == "auto") + this.sc.setNetworkDnsAuto(adapter); + else + this.sc.setNetworkDNS(dns, adapter); + if(wins == "auto") + this.sc.setNetworkWinsAuto(adapter); + else + this.sc.setNetworkWINS(wins, adapter); } } diff --git a/NetMonitorClient/ServiceControl.cs b/NetMonitorClient/ServiceControl.cs index 027e9ea..63bc897 100644 --- a/NetMonitorClient/ServiceControl.cs +++ b/NetMonitorClient/ServiceControl.cs @@ -24,31 +24,107 @@ namespace NetMonitorClient this.rep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523); } - internal void setNetworkAuto(string networkcard) + internal void setNetworkAuto(string adapter) { - if (networkcard == null) + if (adapter == null) { System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); return; } - byte[] data = Encoding.UTF8.GetBytes("NET AUTO "+networkcard); + byte[] data = Encoding.UTF8.GetBytes("NET AUTO "+adapter); this.client.Send(data, data.Length, this.rep); - //throw new NotImplementedException(); } - internal void setNetworkIp(string ip, string subnet, string gateway) + internal void setNetworkIp(string ip, string subnet, string gateway, string adapter) { - throw new NotImplementedException(); + if (ip == null) + { + System.Windows.Forms.MessageBox.Show("IP address not Set!"); + return; + } + if (subnet == null) + { + System.Windows.Forms.MessageBox.Show("Subnet address not Set!"); + return; + } + if (gateway == null) + { + System.Windows.Forms.MessageBox.Show("Gateway address not Set!"); + return; + } + if (adapter == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET IP " + ip + " " + subnet + " " + gateway + " " + adapter); + this.client.Send(data, data.Length, this.rep); } - internal void setNetworkDNS(string dns) + internal void setNetworkDNS(string dns, string adapter) { - throw new NotImplementedException(); + if (dns == null) + { + System.Windows.Forms.MessageBox.Show("DNS address not Set!"); + return; + } + if (adapter == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET DNS " + dns + " " + adapter); + this.client.Send(data, data.Length, this.rep); } - internal void setNetworkWINS(string wins) + internal void setNetworkWINS(string wins, string adapter) { - throw new NotImplementedException(); + if (wins == null) + { + System.Windows.Forms.MessageBox.Show("WINS address not Set!"); + return; + } + if (adapter == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET WINS " + wins + " " + adapter); + this.client.Send(data, data.Length, this.rep); + } + + internal void setNetworkIpAuto(string adapter) + { + if (adapter == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET IP AUTO "+adapter); + this.client.Send(data, data.Length, this.rep); + } + + + internal void setNetworkDnsAuto(string adapter) + { + if (adapter == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET DNS AUTO " + adapter); + this.client.Send(data, data.Length, this.rep); + } + + internal void setNetworkWinsAuto(string adapter) + { + if (adapter == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET WINS AUTO " + adapter); + this.client.Send(data, data.Length, this.rep); } } } diff --git a/NetMonitorServer/Service1.cs b/NetMonitorServer/Service1.cs index 2ebcfa6..f9fb78c 100644 --- a/NetMonitorServer/Service1.cs +++ b/NetMonitorServer/Service1.cs @@ -40,9 +40,9 @@ namespace NetMonitorServer private static void switchCommand(string data) { + Console.WriteLine(data); if (data.Substring(0, 3).ToLower() == "net") new SetNetworks(data); - Console.WriteLine(data); } protected override void OnStart(string[] args) diff --git a/NetMonitorServer/SetNetworks.cs b/NetMonitorServer/SetNetworks.cs index 0f0bae8..2549e74 100644 --- a/NetMonitorServer/SetNetworks.cs +++ b/NetMonitorServer/SetNetworks.cs @@ -10,8 +10,47 @@ namespace NetMonitorServer { public SetNetworks(string data) { - if (data.Substring(0,8).ToLower() == "net auto") - setNetworkAuto(data); + if (data.Substring(0, 8).ToLower() == "net auto") + this.setNetworkAuto(data); + if (data.Substring(0, 6).ToLower() == "net ip") + this.setNetworkIp(data); + if (data.Substring(0, 7).ToLower() == "net dns") + this.setNetworkDns(data); + if (data.Substring(0, 8).ToLower() == "net wins") + this.setNetworkWins(data); + } + + private void setNetworkWins(string data) + { + String[] par = data.Split(' '); + if (par.Length != 4) + return; + if (par[2].ToLower() == "auto") + this.runProgramNetsh("interface ipv4 set winsservers name=\"" + par[3] + "\" source=dhcp"); + else + this.runProgramNetsh("interface ipv4 set winsservers \"" + par[3] + "\" static " + par[2]); + } + + private void setNetworkDns(string data) + { + String[] par = data.Split(' '); + if (par.Length != 4) + return; + if (par[2].ToLower() == "auto") + this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + par[3] + "\" source=dhcp"); + else + this.runProgramNetsh("interface ipv4 set dnsservers \"" + par[3] + "\" static " + par[2] + " primary"); + } + + private void setNetworkIp(string data) + { + String[] par = data.Split(' '); + if (par.Length != 4 && par.Length != 6) + return; + if (par[2].ToLower() == "auto") + this.runProgramNetsh("interface ipv4 set address name=\"" + par[3] + "\" source=dhcp"); + else + this.runProgramNetsh("interface ipv4 set address \"" + par[5] + "\" static " + par[2] + " " + par[3] + " " + par[4]); } private void setNetworkAuto(string data) @@ -19,26 +58,23 @@ namespace NetMonitorServer String[] par = data.Split(' '); if (par.Length != 3) return; - Process p1 = new Process(); - p1.StartInfo.Arguments = "interface ipv4 set address name=\""+par[2]+"\" source=dhcp"; - p1.StartInfo.FileName = "netsh"; - p1.Start(); - p1.WaitForExit(); - Console.WriteLine(p1.ExitCode); + this.runProgramNetsh("interface ipv4 set address name=\""+par[2]+"\" source=dhcp"); + this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + par[2] + "\" source=dhcp"); + this.runProgramNetsh("interface ipv4 set winsservers name=\"" + par[2] + "\" source=dhcp"); + } - Process p2 = new Process(); - p2.StartInfo.Arguments = "interface ipv4 set dnsservers name=\"" + par[2] + "\" source=dhcp"; - p2.StartInfo.FileName = "netsh"; - p2.Start(); - p2.WaitForExit(); - Console.WriteLine(p2.ExitCode); - - Process p3 = new Process(); - p3.StartInfo.Arguments = "interface ipv4 set winsservers name=\"" + par[2] + "\" source=dhcp"; - p3.StartInfo.FileName = "netsh"; - p3.Start(); - p3.WaitForExit(); - Console.WriteLine(p3.ExitCode); + private void runProgramNetsh(string 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(); + string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + Console.WriteLine(output.Trim()); } } }