using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace setip
{
internal class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: setip <InterfaceType> <Action> [IP] [SubnetMask] [GatewayLastOctet]");
Console.WriteLine("Usage: setip 1 s 10.1.1.11 24 1");
Console.WriteLine("Actions:");
Console.WriteLine(" s <IP> <SubnetMask> <GatewayLastOctet>: Configure static IP.");
Console.WriteLine(" d: Set to DHCP.");
Console.WriteLine(" c: Close the network interface.");
Console.WriteLine(" u: Up the network interface.");
Console.WriteLine("注意:有线网卡需要命名为lan,无线网卡需要命名为WLAN,");
Console.WriteLine("InterfaceType为1对应lan,为2对应WLAN");
return;
}
int interfaceType = int.Parse(args[0]);
string mode = args[1].ToLower();
switch (mode)
{
case "s":
if (args.Length != 5)
{
Console.WriteLine("Usage: setip <InterfaceType> s <IP> <SubnetMask> <GatewayLastOctet>");
return;
}
string ipAddress = args[2];
int subnetMask = int.Parse(args[3]);
string gatewayLastOctet = args[4];
string gateway = ConstructGateway(ipAddress, gatewayLastOctet);
string subnetMaskDecimal = CalculateSubnetMask(subnetMask);
SetIPAddress(ipAddress, subnetMaskDecimal, gateway, interfaceType);
SetDnsServers(gateway, interfaceType);
break;
case "d":
SetToDhcp(interfaceType);
break;
case "c":
DisableInterface(interfaceType);
break;
case "u":
EnableInterface(interfaceType);
break;
default:
Console.WriteLine("Invalid mode. Use 's', 'd', 'c', or 'u'.");
return;
}
}
private static void DisableInterface(int interfaceType)
{
string interfaceName = GetInterfaceName(interfaceType);
string disableCmd = $"netsh interface set interface name=\"{interfaceName}\" admin=DISABLED";
ExecuteCommand(disableCmd);
}
private static void EnableInterface(int interfaceType)
{
string interfaceName = GetInterfaceName(interfaceType);
string enableCmd = $"netsh interface set interface name=\"{interfaceName}\" admin=ENABLED";
ExecuteCommand(enableCmd);
}
private static void SetToDhcp(int interfaceType)
{
string interfaceName = GetInterfaceName(interfaceType);
string setAddressCmd = $"echo netsh interface ip set address name=\"{interfaceName}\" source=dhcp";
string setDnsCmd = $"netsh interface ip set dns name=\"{interfaceName}\" source=dhcp";
ExecuteCommand(setAddressCmd);
ExecuteCommand(setDnsCmd);
}
private static string ConstructGateway(string ipAddress, string lastOctet)
{
string[] octets = ipAddress.Split('.');
if (octets.Length != 4)
{
throw new ArgumentException("Invalid IP address format.");
}
return $"{octets[0]}.{octets[1]}.{octets[2]}.{lastOctet}";
}
private static void SetIPAddress(string ipAddress, string subnetMask, string gateway, int interfaceType)
{
string command = $"netsh interface ip set address name=\"{GetInterfaceName(interfaceType)}\" static {ipAddress} {subnetMask} {gateway}";
ExecuteCommand(command);
}
private static void SetDnsServers( string dnsServers, int interfaceType)
{
string setDnsCmd = $"netsh interface ip set dns name=\"{GetInterfaceName(interfaceType)}\" source=dhcp";
ExecuteCommand(setDnsCmd);
string command = $"netsh interface ip add dns name=\"{GetInterfaceName(interfaceType)}\" addr={dnsServers} index=1";
string command1 = $"netsh interface ip add dns name=\"{GetInterfaceName(interfaceType)}\" addr=114.114.114.114 index=2";
ExecuteCommand(command);
ExecuteCommand(command1);
}
private static void ExecuteCommand(string command)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/C {command}",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
//Console.WriteLine(result);
}
}
}
private static string GetInterfaceName(int interfaceType)
{
if (interfaceType == 1) {
return "lan"; }
if (interfaceType == 2)
{
return "WLAN";
}
throw new Exception("No active Ethernet or Wireless80211 interface found.");
}
private static string CalculateSubnetMask(int subnetMask)
{
byte[] maskBytes = new byte[4];
// 计算每个字节中应该设置为1的位数
int byte1Bits = Math.Min(8, subnetMask);
int byte2Bits = Math.Min(8, subnetMask - byte1Bits);
int byte3Bits = Math.Min(8, subnetMask - byte1Bits - byte2Bits);
int byte4Bits = subnetMask - byte1Bits - byte2Bits - byte3Bits;
// 构建每个字节的值
maskBytes[0] = (byte)((1 << byte1Bits) - 1);
maskBytes[1] = (byte)((1 << byte2Bits) - 1);
maskBytes[2] = (byte)((1 << byte3Bits) - 1);
maskBytes[3] = (byte)((1 << byte4Bits) - 1);
// 将每个字节的值左移以填充高位
maskBytes[1] <<= 8 - byte2Bits;
maskBytes[2] <<= 8 - byte3Bits;
maskBytes[3] <<= 8 - byte4Bits;
return $"{maskBytes[0]}.{maskBytes[1]}.{maskBytes[2]}.{maskBytes[3]}";
}
}
}
程序下载地址,可以增加path全局有效。