首页 > 编程语言 >编程调用arp命令,进阶高手必看

编程调用arp命令,进阶高手必看

时间:2023-01-29 17:55:26浏览次数:42  
标签:arp ARP 进阶 必看 pipe 命令 ff 使用

  ARP (Address Resolution Protocol) 命令是一种网络协议,用于在网络中将 IP 地址映射到对应的物理 (MAC) 地址。通常在局域网 (LAN) 中使用。

  ARP 命令通过发送 ARP 请求报文来查询网络中的主机的 MAC 地址。当一台计算机要与另一台计算机通信时,首先要知道对方的 MAC 地址。

  在操作系统中,通常可以使用 arp 命令来查看和管理 ARP 缓存表。例如:

  显示当前 ARP 缓存表:arp -a
  添加一个 ARP 条目:arp -s <IP> <MAC>
  删除一个 ARP 条目:arp -d <IP>
  需要注意的是, ARP 协议存在安全漏洞,如 ARP 欺骗攻击。在使用arp命令时应该格外小心。

 

  在各编程语言中实现arp命令,可能需要使用第三方库,例如 scapy 库在Python中可以使用。

  在使用系统调用时,可以使用如 subprocess 模块在 Python 中执行命令行命令。例如,在 Python 中执行 arp -a 命令可以使用如下代码:

  import subprocess

  result = subprocess.run([‘arp’, ‘-a’], stdout=subprocess.PIPE)
  print(result.stdout.decode())

  在使用第三方库时,可以使用如 scapy 库在 Python 中操作 ARP 协议。例如,在 Python 中发送一个 ARP 请求报文可以使用如下代码:

  from scapy.all import ARP, Ether, srp

  target_ip = “192.168.1.1”

  # Create ARP packet
  arp = ARP(pdst=target_ip)
  ether = Ether(dst=”ff:ff:ff:ff:ff:ff”)
  packet = ether/arp

  result = srp(packet, timeout=3, verbose=0)[0]

  # Print out the result
  for sent, received in result:
  print(“IP: ” + received.psrc + ” MAC: ” + received.hwsrc)

 

  在 C++ 中调用 arp 命令可以使用系统函数 system() 来实现。

  下面是一个示例代码,展示了如何使用 system() 函数调用 arp 命令并获取其输出:

  #include <iostream>
  #include <cstdlib>
  using namespace std;

  int main()
  {
  string arpCommand = “arp -a”;
  string arpOutput = “”;

  // 使用 system() 函数调用 arp 命令
  char buffer[1024];
  FILE* pipe = popen(arpCommand.c_str(), “r”);
  if (!pipe) throw runtime_error(“popen() failed!”);
  try {
  while (!feof(pipe)) {
  if (fgets(buffer, 1024, pipe) != NULL)
  arpOutput += buffer;
  }
  } catch (…) {
  pclose(pipe);
  throw;
  }
  pclose(pipe);

  // 输出 arp 命令的输出
  cout << arpOutput << endl;

  return 0;
  }
转载说明:本文部分内容引用自电脑监控软件https://www.vipshare.com/archives/39852,转载请提供出处

标签:arp,ARP,进阶,必看,pipe,命令,ff,使用
From: https://www.cnblogs.com/llllaaaaiiii1234/p/17073403.html

相关文章

  • 【原型设计】进阶玩法:试试引入EChart
    【原型设计】进阶玩法:试试引入EChart......
  • 牛客算法进阶——树形dp
    1.小G有一个大树(求树的重心)删除该点后最大连通块的节点数最小设f[x]表示以x为根的子树大小,那么删除x之后的各子树大小为f[to]和n-f[x]求max(max(f[to]),n-f[x])的最小......
  • CSharp: Relational mapping in donet using Entity Framework
     sqlscript--dbo.Students--Idintidentity--Name--Agebyte--CreatedTimedate--ModifiedTimedateIFEXISTS(select*fromsysobjectswhereid=object......
  • Java程序员必看:Solr vip竞价排名
    功能介绍本文将使用solr完成vip等级排名,这里并不是简单的按照vip等级排序。而是在使用solr条件查询(不使用排序)的时候将符合条件并且具有vip等级的数据显示在前面,这个时候......
  • CSharp: Sort
     BubbleSort冒泡排序SelectionSort选择排序InsertionSort插入排序QuickSort快速排序ShellSort希尔排序MergeSort归并排序HeapSort堆排序BucketSort桶排序又......
  • CSharp: Collection
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceCSharpDataStructuresAlgorithms......
  • CSharp: Add,Edit,Del,Select in donet using Entity Framework
     usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Runtime.Remoting.Contexts;usingSystem.Text;usi......
  • CS144-Lab5-ARP
    lab地址:lab5-doc代码实现:lab5-code完整目录:0.ByteStream1.StreamReassembler2.TCPReceiver3.TCPSender4.TCPConnection5.ARP6.IP-Router1.目标lab......
  • 人工智能算法进阶:SOM聚类的应用
    OM即自组织映射,是一种用于特征检测的无监督学习神经网络。它模拟人脑中处于不同区域的神经细胞分工不同的特点,即不同区域具有不同的响应特征,而且这一过程是自动完成的。SO......
  • CSharp: emojione
     ///<summary>///mysql数据库用编码类型utf8mb4///向sqlserver数据库插入emoji表情包///插入emoji的数据时,值value需要......