如何在 Windows XP 中以编程方式创建静态 ARP 缓存条目

发布于 2024-09-26 09:25:28 字数 413 浏览 0 评论 0原文

有没有办法在C#中设置ARP缓存条目?

我发现的唯一方法与使用 arp 实用程序 地址解析协议

使用 arp -s 命令添加的静态条目不会在缓存中过期。

有关于 如何通过以下方式访问 ARP 协议信息的相关帖子.NET?

Is there are way to set ARP cache entry in C#?

The only way I have found is related to use of arp utility Address Resolution Protocol

Static entries added with the arp -s command are not expired from the cache.

There are related post about How do I access ARP-protocol information through .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北斗星光 2024-10-03 09:25:28

显然,它不是纯粹的 .NET,但您应该能够通过 IP Helper API 库中的 Win32 API(即 CreateIpNetEntry 和 SetIpNetEntry 方法)来完成此操作。您可能希望通过 P/Invoke 或包装的托管 C++ 库来执行此操作。

http://msdn.microsoft.com/en-我们/library/aa366071(v=vs.85).aspx

It wouldn't be purely .NET obviously, but you should be able to do it via the Win32 API in the IP Helper API library - namely the CreateIpNetEntry and SetIpNetEntry methods. You would likely want to do this via P/Invoke or a wrapped managed C++ library.

http://msdn.microsoft.com/en-us/library/aa366071(v=vs.85).aspx

心房敞 2024-10-03 09:25:28

我现在使用的简单解决方案是运行批处理命令,将该静态条目添加到 ARP 表中。在 Vista 及更高版本上,这将需要管理员用户权限。

' arp -s 192.168.1.12 01-02-03-04-05-06
Public Sub UpdateArpTable(IpAddress as string, MacAddress as string)
    Dim outputMessage As string = ""
    Dim errorMessage As string = ""
    Dim command As String = String.Format("-s {0} {1}", Address, MacAddress)
    ExecuteShellCommand("arp", command, outputMessage, errorMessage)
End Sub


Public Shared Sub ExecuteShellCommand(FileToExecute As String, CommandLine As String)
    Dim Process As System.Diagnostics.Process = Nothing
    Try
        Process = New System.Diagnostics.Process()
        Dim CMDProcess As String = String.Format("{0}\cmd.exe", Environment.SystemDirectory)

        Dim Arguments As String = String.Format("/C {0}", FileToExecute)

        If CommandLine IsNot Nothing AndAlso CommandLine.Length > 0 Then
            Arguments += String.Format(" {0}", CommandLine)
        End If
        Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo(CMDProcess, Arguments)
        ProcessStartInfo.CreateNoWindow = True
        ProcessStartInfo.UseShellExecute = False
        ProcessStartInfo.RedirectStandardOutput = True
        ProcessStartInfo.RedirectStandardInput = True
        ProcessStartInfo.RedirectStandardError = True
        Process.StartInfo = ProcessStartInfo

        Process.Start()
        Process.WaitForExit()
        Process.WaitForExit()
    Finally
        ' close process and do cleanup
        Process.Close()
        Process.Dispose()
        Process = Nothing
    End Try
End Sub

The simple solution I am using right now is to run batch command that will add this static entry into ARP table. On Vista and up this will require administrator user rights.

' arp -s 192.168.1.12 01-02-03-04-05-06
Public Sub UpdateArpTable(IpAddress as string, MacAddress as string)
    Dim outputMessage As string = ""
    Dim errorMessage As string = ""
    Dim command As String = String.Format("-s {0} {1}", Address, MacAddress)
    ExecuteShellCommand("arp", command, outputMessage, errorMessage)
End Sub


Public Shared Sub ExecuteShellCommand(FileToExecute As String, CommandLine As String)
    Dim Process As System.Diagnostics.Process = Nothing
    Try
        Process = New System.Diagnostics.Process()
        Dim CMDProcess As String = String.Format("{0}\cmd.exe", Environment.SystemDirectory)

        Dim Arguments As String = String.Format("/C {0}", FileToExecute)

        If CommandLine IsNot Nothing AndAlso CommandLine.Length > 0 Then
            Arguments += String.Format(" {0}", CommandLine)
        End If
        Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo(CMDProcess, Arguments)
        ProcessStartInfo.CreateNoWindow = True
        ProcessStartInfo.UseShellExecute = False
        ProcessStartInfo.RedirectStandardOutput = True
        ProcessStartInfo.RedirectStandardInput = True
        ProcessStartInfo.RedirectStandardError = True
        Process.StartInfo = ProcessStartInfo

        Process.Start()
        Process.WaitForExit()
        Process.WaitForExit()
    Finally
        ' close process and do cleanup
        Process.Close()
        Process.Dispose()
        Process = Nothing
    End Try
End Sub
∞琼窗梦回ˉ 2024-10-03 09:25:28

您可以简单地运行命令 ARP -s inet_addr eth_adr,其中 inet_addr 是 IP 地址,eth_adr 是硬件地址。

  Process process = new Process();
  process.StartInfo.FileName = "arp -s 220.0.0.161 00-50-04-62-F7-23";
  process.StartInfo.CreateNoWindow = true; //Don't show window
  process.Start();

You can simply run the command ARP -s inet_addr eth_adr where inet_addr is the IP address and eth_adr is the hardware address.

  Process process = new Process();
  process.StartInfo.FileName = "arp -s 220.0.0.161 00-50-04-62-F7-23";
  process.StartInfo.CreateNoWindow = true; //Don't show window
  process.Start();
绮烟 2024-10-03 09:25:28

试试这个。 (归功于扎克)
确保使用 try/catch 保护敏感部分

using System;
using System.Net;
using System.Runtime.InteropServices;

namespace CreateArpEntry
{
    class Program
    {
        [DllImport("iphlpapi.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U4)]
        public static extern int CreateIpNetEntry(ref MIB_IPNETROW tableEntry);

        static void Main(string[] args)
        {
            IPAddress.TryParse("192.168.1.2", out IPAddress parsedIpAddress);
            byte[] macBytes = {0,0x0b,0x3b,0,0,1,0,0}; // pad the MAC address with zeros

            MIB_IPNETROW arpEntry = new MIB_IPNETROW
            {
                Index = 20,
                Addr = BitConverter.ToInt32(parsedIpAddress.GetAddressBytes(), 0),
                PhysAddr = macBytes,
                PhysAddrLen = 8,
                Type = 4, // it can only be static
            };

            int a = CreateIpNetEntry(ref arpEntry);
        }

    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MIB_IPNETROW
    {
        /// <summary>
        /// The index of the adapter (Not constant may change for example by disable / enable  
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int Index;

        /// <summary>
        /// The length of the physical address ( MAC Address) 
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int PhysAddrLen;

        /// <summary>
        /// The physical / MAC Address
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] PhysAddr;

        /// <summary>
        /// The IP Address
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int Addr;

        /// <summary>
        /// The IP Address Type
        /// Other = 1, Invalid = 2, Dynamic = 3, Static = 4 
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int Type;
    }
}

Try this one. (credits to Zach)
Make sure to guard the sensible parts with try/catch

using System;
using System.Net;
using System.Runtime.InteropServices;

namespace CreateArpEntry
{
    class Program
    {
        [DllImport("iphlpapi.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.U4)]
        public static extern int CreateIpNetEntry(ref MIB_IPNETROW tableEntry);

        static void Main(string[] args)
        {
            IPAddress.TryParse("192.168.1.2", out IPAddress parsedIpAddress);
            byte[] macBytes = {0,0x0b,0x3b,0,0,1,0,0}; // pad the MAC address with zeros

            MIB_IPNETROW arpEntry = new MIB_IPNETROW
            {
                Index = 20,
                Addr = BitConverter.ToInt32(parsedIpAddress.GetAddressBytes(), 0),
                PhysAddr = macBytes,
                PhysAddrLen = 8,
                Type = 4, // it can only be static
            };

            int a = CreateIpNetEntry(ref arpEntry);
        }

    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MIB_IPNETROW
    {
        /// <summary>
        /// The index of the adapter (Not constant may change for example by disable / enable  
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int Index;

        /// <summary>
        /// The length of the physical address ( MAC Address) 
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int PhysAddrLen;

        /// <summary>
        /// The physical / MAC Address
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] PhysAddr;

        /// <summary>
        /// The IP Address
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int Addr;

        /// <summary>
        /// The IP Address Type
        /// Other = 1, Invalid = 2, Dynamic = 3, Static = 4 
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        public int Type;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文