From 890cfc839ab770d4a7345a515ffb249487cd88d7 Mon Sep 17 00:00:00 2001 From: BlubbFish Date: Wed, 28 Apr 2010 14:34:35 +0000 Subject: [PATCH] =?UTF-8?q?SVN=20Ignore=20ge=C3=A4ndert,=20erste=20Funktio?= =?UTF-8?q?nierende=20Komunikation=20so=20das=20Befehle=20ausgef=C3=BChrt?= =?UTF-8?q?=20werden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetMonitorClient/Form1.cs | 3 +- NetMonitorClient/ServiceControl.cs | 9 +- NetMonitorClient/network.ini | 3 +- NetMonitorServer/NetMonitorServer.csproj | 8 +- NetMonitorServer/NetMonitorServer.csproj.user | 6 + NetMonitorServer/Program.cs | 49 ++++++- NetMonitorServer/Service1.cs | 8 +- NetMonitorServer/Service1.resx | 126 ++++++++++++++++++ NetMonitorServer/SetNetworks.cs | 44 ++++++ 9 files changed, 242 insertions(+), 14 deletions(-) create mode 100644 NetMonitorServer/NetMonitorServer.csproj.user create mode 100644 NetMonitorServer/Service1.resx create mode 100644 NetMonitorServer/SetNetworks.cs diff --git a/NetMonitorClient/Form1.cs b/NetMonitorClient/Form1.cs index 2fa52cb..85ce1d6 100644 --- a/NetMonitorClient/Form1.cs +++ b/NetMonitorClient/Form1.cs @@ -32,9 +32,10 @@ namespace NetMonitorClient String gateway = networkini.getValue(Item.Name, "Gateway"); String dns = networkini.getValue(Item.Name, "Dns"); String wins = networkini.getValue(Item.Name, "Wins"); + String adapter = networkini.getValue(Item.Name, "Adapter"); if (ip == "auto" && dns == "auto" && wins == "auto") - this.sc.setNetworkAuto(); + this.sc.setNetworkAuto(adapter); else { this.sc.setNetworkIp(ip, subnet, gateway); diff --git a/NetMonitorClient/ServiceControl.cs b/NetMonitorClient/ServiceControl.cs index cf64a60..027e9ea 100644 --- a/NetMonitorClient/ServiceControl.cs +++ b/NetMonitorClient/ServiceControl.cs @@ -24,9 +24,14 @@ namespace NetMonitorClient this.rep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523); } - internal void setNetworkAuto() + internal void setNetworkAuto(string networkcard) { - byte[] data = Encoding.UTF8.GetBytes("NET AUTO"); + if (networkcard == null) + { + System.Windows.Forms.MessageBox.Show("Adapter ist not Set!"); + return; + } + byte[] data = Encoding.UTF8.GetBytes("NET AUTO "+networkcard); this.client.Send(data, data.Length, this.rep); //throw new NotImplementedException(); } diff --git a/NetMonitorClient/network.ini b/NetMonitorClient/network.ini index 5e9f1a4..fc415b3 100644 --- a/NetMonitorClient/network.ini +++ b/NetMonitorClient/network.ini @@ -2,4 +2,5 @@ Name=Home Ip=auto Dns=auto -Wins=auto \ No newline at end of file +Wins=auto +Adapter=LAN-Verbindung \ No newline at end of file diff --git a/NetMonitorServer/NetMonitorServer.csproj b/NetMonitorServer/NetMonitorServer.csproj index 12b0609..9e24aaa 100644 --- a/NetMonitorServer/NetMonitorServer.csproj +++ b/NetMonitorServer/NetMonitorServer.csproj @@ -19,7 +19,7 @@ true full false - bin\Debug\ + E:\Programme\NetMonitor\ DEBUG;TRACE prompt 4 @@ -35,6 +35,7 @@ + @@ -51,8 +52,11 @@ Service1.cs - + + Component + + diff --git a/NetMonitorServer/NetMonitorServer.csproj.user b/NetMonitorServer/NetMonitorServer.csproj.user new file mode 100644 index 0000000..63df725 --- /dev/null +++ b/NetMonitorServer/NetMonitorServer.csproj.user @@ -0,0 +1,6 @@ + + + + -r + + \ No newline at end of file diff --git a/NetMonitorServer/Program.cs b/NetMonitorServer/Program.cs index e6532af..5a7899e 100644 --- a/NetMonitorServer/Program.cs +++ b/NetMonitorServer/Program.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; +using System.Configuration.Install; +using System.ComponentModel; namespace NetMonitorServer { @@ -11,14 +13,47 @@ namespace NetMonitorServer /// /// The main entry point for the application. /// - static void Main() + static void Main(string[] args) { - ServiceBase[] ServicesToRun; - ServicesToRun = new ServiceBase[] - { - new Service1() - }; - ServiceBase.Run(ServicesToRun); + if (args.Length == 0) + { + ServiceBase[] ServicesToRun; + ServicesToRun = new ServiceBase[] + { + new Service1() + }; + ServiceBase.Run(ServicesToRun); + } + else if (args.Length == 1 || args[0] == "-r") + { + Service1 s = new Service1(); + s.StartServiceConsole(args); + while (true) + { + System.Threading.Thread.Sleep(100); + } + } + } + } + [RunInstaller(true)] + public class TestWinInstaller : Installer + { + private ServiceInstaller m_ThisService; + private ServiceProcessInstaller m_ThisServiceProcess; + + public TestWinInstaller() + { + m_ThisService = new ServiceInstaller(); + m_ThisServiceProcess = new ServiceProcessInstaller(); + + m_ThisServiceProcess.Account = ServiceAccount.LocalSystem; + m_ThisService.ServiceName = "NetMonitorServer"; + m_ThisService.StartType = ServiceStartMode.Automatic; + m_ThisService.Description = "Teil von NetMonitor der die Systemnahen Befele ausführt"; + m_ThisService.DisplayName = "Netzwerk Settings Tool"; + + Installers.Add(m_ThisService); + Installers.Add(m_ThisServiceProcess); } } } diff --git a/NetMonitorServer/Service1.cs b/NetMonitorServer/Service1.cs index 052c938..2ebcfa6 100644 --- a/NetMonitorServer/Service1.cs +++ b/NetMonitorServer/Service1.cs @@ -20,7 +20,6 @@ namespace NetMonitorServer InitializeComponent(); this.ot = new Thread(new ThreadStart(overwatch)); this.ot.IsBackground = true; - this.ot.Start(); } private static void overwatch() { @@ -41,6 +40,8 @@ namespace NetMonitorServer private static void switchCommand(string data) { + if (data.Substring(0, 3).ToLower() == "net") + new SetNetworks(data); Console.WriteLine(data); } @@ -53,5 +54,10 @@ namespace NetMonitorServer { this.ot.Abort(); } + + public void StartServiceConsole(string[] args) + { + this.OnStart(args); + } } } diff --git a/NetMonitorServer/Service1.resx b/NetMonitorServer/Service1.resx new file mode 100644 index 0000000..18934bd --- /dev/null +++ b/NetMonitorServer/Service1.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 143, 71 + + + False + + \ No newline at end of file diff --git a/NetMonitorServer/SetNetworks.cs b/NetMonitorServer/SetNetworks.cs new file mode 100644 index 0000000..0f0bae8 --- /dev/null +++ b/NetMonitorServer/SetNetworks.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Diagnostics; + +namespace NetMonitorServer +{ + class SetNetworks + { + public SetNetworks(string data) + { + if (data.Substring(0,8).ToLower() == "net auto") + setNetworkAuto(data); + } + + private void setNetworkAuto(string data) + { + 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); + + 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); + } + } +}