netmonitor/NetMonitorServer/Actions/SetNetworks.cs

304 lines
12 KiB
C#
Raw Normal View History

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;
namespace NetMonitorServer
{
class SetNetworks : ActionClass
{
2015-03-04 23:21:34 +01:00
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;
}
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();
2015-03-04 23:21:34 +01:00
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)
{
2015-03-04 23:21:34 +01:00
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;
}
2015-03-04 23:21:34 +01:00
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)
{
2015-03-04 23:21:34 +01:00
//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();
2015-03-04 23:21:34 +01:00
this.netsh_output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
2015-03-04 23:21:34 +01:00
//Console.WriteLine(this.netsh_output.Trim());
if (p.ExitCode == 0)
{
return true;
}
2015-03-04 23:21:34 +01:00
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);
2015-03-04 23:21:34 +01:00
case "network": return this.getNetwork(arguments);
default: this.sendMessage("Wrong arguments for service on SetNetwork class"); return false;
}
}
2015-03-04 23:21:34 +01:00
}
}