feat: 使用netsh指令设置arp
This commit is contained in:
@@ -119,46 +119,7 @@ public class NetConfigController : ControllerBase
|
||||
/// <returns>网络接口名称</returns>
|
||||
private string GetLocalNetworkInterface()
|
||||
{
|
||||
try
|
||||
{
|
||||
var boardIpSegments = BOARD_IP.Split('.').Take(3).ToArray();
|
||||
|
||||
// 优先选择与实验板IP前三段相同的网络接口
|
||||
var sameSegmentInterface = System.Net.NetworkInformation.NetworkInterface
|
||||
.GetAllNetworkInterfaces()
|
||||
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||
.FirstOrDefault(nic =>
|
||||
{
|
||||
var ipAddresses = nic.GetIPProperties().UnicastAddresses
|
||||
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
||||
.Select(addr => addr.Address);
|
||||
|
||||
return ipAddresses.Any(addr =>
|
||||
{
|
||||
var segments = addr.ToString().Split('.');
|
||||
return segments.Length == 4 &&
|
||||
segments[0] == boardIpSegments[0] &&
|
||||
segments[1] == boardIpSegments[1] &&
|
||||
segments[2] == boardIpSegments[2];
|
||||
});
|
||||
});
|
||||
|
||||
if (sameSegmentInterface != null)
|
||||
return sameSegmentInterface.Name;
|
||||
|
||||
// 如果没有找到同网段的接口,返回第一个可用的接口
|
||||
return System.Net.NetworkInformation.NetworkInterface
|
||||
.GetAllNetworkInterfaces()
|
||||
.Where(nic => nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up
|
||||
&& nic.NetworkInterfaceType != System.Net.NetworkInformation.NetworkInterfaceType.Loopback)
|
||||
.FirstOrDefault()?.Name ?? "未知";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "获取本机网络接口名称失败");
|
||||
return "未知";
|
||||
}
|
||||
return GetLocalIPAddress().ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -484,6 +445,50 @@ public class NetConfigController : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置板卡MAC地址
|
||||
/// </summary>
|
||||
/// <param name="boardMac">板卡MAC地址(格式:AA:BB:CC:DD:EE:FF)</param>
|
||||
/// <returns>操作结果</returns>
|
||||
[HttpPost("SetBoardMAC")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<IActionResult> SetBoardMAC(string boardMac)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(boardMac))
|
||||
return BadRequest("板卡MAC地址不能为空");
|
||||
|
||||
// 解析MAC地址
|
||||
if (!TryParseMacAddress(boardMac, out var macBytes))
|
||||
return BadRequest("MAC地址格式不正确,请使用格式:AA:BB:CC:DD:EE:FF");
|
||||
|
||||
try
|
||||
{
|
||||
if (!(await InitializeArpAsync()))
|
||||
{
|
||||
throw new Exception("无法配置ARP记录");
|
||||
}
|
||||
// 创建网络配置客户端
|
||||
var netConfig = new NetConfig(BOARD_IP, BOARD_PORT, 0);
|
||||
var result = await netConfig.SetBoardMAC(macBytes);
|
||||
|
||||
if (!result.IsSuccessful)
|
||||
{
|
||||
logger.Error($"设置板卡MAC地址失败: {result.Error}");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, $"设置失败: {result.Error}");
|
||||
}
|
||||
|
||||
return Ok(result.Value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex, "设置板卡MAC地址时发生异常");
|
||||
return StatusCode(StatusCodes.Status500InternalServerError, "设置失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置主机MAC地址
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user