// 验证IP合法性
if (ipParts.Length != 4 || !IPAddress.TryParse(inputIP, out _))
{
MessageBox.Show("IP地址格式无效");
return;
}
}
else
{
MessageBox.Show("输入IP不能为空");
return;
}
// 根据输入IP的最后一段确定port取值,取最后两位
string lastPart = ipParts[3];
if (lastPart.Length > 2)
{
lastPart = lastPart.Substring(lastPart.Length - 2);
}
if (!int.TryParse(lastPart, out int portCount))
{
MessageBox.Show("末段值需为有效的整数");
return;
}
// 验证末段值范围
if (portCount < 1 || portCount > 155)
{
MessageBox.Show("末段值需在1~155之间");
return;
}
// 拼接IP前缀
string ipPrefix = $"{ipParts[0]}.{ipParts[1]}.{ipParts[2]}";
int startIP = 101;
StringBuilder commands = new StringBuilder();
// 生成命令
for (int i = 1; i <= portCount; i++)
{
int currentIP = startIP + i - 1;
commands.AppendLine($"netsh interface ip set address \"port{i}\" static {ipPrefix}.{currentIP} 255.255.255.0");
}