152 lines
5.1 KiB
C#
152 lines
5.1 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
|
|||
|
namespace NetMonitorServer
|
|||
|
{
|
|||
|
class SetNetworks : ActionClass
|
|||
|
{
|
|||
|
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();
|
|||
|
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)
|
|||
|
{
|
|||
|
this.sendMessage("To less arguments for SetNetworks.setNetworkDns 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 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());
|
|||
|
if (p.ExitCode == 0)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
throw new Exception(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);
|
|||
|
default: this.sendMessage("Wrong arguments for service on SetNetwork class"); return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|