473|0

34

帖子

4

TA的资源

一粒金砂(中级)

楼主
 

【得捷电子Follow me第4期】基础任务一:静态IP配置、Ping、抓包分析 [复制链接]

  本帖最后由 zygalaxy 于 2024-2-22 12:56 编辑

基础任务一:完成主控板W5500初始化(静态IP配置),并能使用局域网电脑ping通,同时W5500可以ping通互联网站点;通过抓包软件(Wireshark、Sniffer等)抓取本地PC的ping报文,展示并分析。

首先安装网络模块的库

 

一、静态IP配置

Ethernet库安装完成,可以编写代码来进行静态IP、DNS服务器、网关、子网掩码的设置了,除此以外还可以使用DHCP进行动态的 IP 的获取

设置静态 IP 代码如下:

// #include <SPI.h>
#include <Ethernet.h>

// 网卡mac地址
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// 静态ip地址、DNS服务、网关、子网掩码
IPAddress ip(192, 168, 0, 122);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

//为了能 ping通,选择创建一个http服务
EthernetServer server(80);

void setup() {
  // 配置串口
  Serial.begin(9600);
  while (!Serial) {
    ;  // 等待串口连接
  }
  //使用 CSPIN来初始化网卡
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  // Ethernet.init(17);

  // 静态IP设置
  Serial.println("Ethernet Begin");
  Ethernet.begin(mac, ip, myDns, gateway, subnet);

  //启动服务
  server.begin();
  // 输出网卡mac地址、IP地址、子网掩码、DNS、网关
  Serial.print("My Mac address: ");
  byte macBuffer[6];               // 设置mac地址存储buff
  Ethernet.MACAddress(macBuffer);  // 读取mac地址
  for (byte octet = 0; octet < 6; octet++) {
    Serial.print(macBuffer[octet], HEX);
    if (octet < 5) {
      Serial.print('-');
    }
  }
  Serial.println("");

  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

  Serial.print("My subnet: ");
  Serial.println(Ethernet.subnetMask());

  Serial.print("My DNS address: ");
  Serial.println(Ethernet.dnsServerIP());

  Serial.print("My GateWay address: ");
  Serial.println(Ethernet.gatewayIP());
}

void loop() {
 
}

上传上述代码到到开发板,串口输出如下:

 

为了使局域网电脑ping通W5500,所以使用EthernetServer server(80);手动创建一个 http 服务,并在 setup 函数中使用server.begin();启动服务

 

此时可以在电脑上,Ping通开发板的IP地址,得到如下结果:

 

二、ping 通互联网站点

在这开始之前需要安装一个 ICMP 的库用来使用 ping 功能 ,选择红框内的选项,点击下载

链接已隐藏,如需查看请登录或者注册

   

需要手动安装这个库,我是 Mac 系统,可以点击访达按钮进入 arduino 的libraries,将解压后的文件夹放入即可

1、点击访达

 

2、选择前往文件

 

3、输入这个地址

 

4、选择 arduino

 

5、进入选择 libraries 里面

 

 

6、拖动解压后的压缩包到这个目录即可完成安装

  接下来进入正题:

要Ping通互联网站点前提就是解析域名为 IP 地址,因为网站都是使用域名的,这个时候就需要使用 DNS 来进行域名解析

Ethernet 库自带了 dns 解析库,只需要在代码种中引用即可

// DNS
#include <Dns.h>

然后在上面的代码中结合使用即可

//#include <SPI.h>
#include <Ethernet.h>
// DNS
#include <Dns.h>
// 网卡mac地址
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// 静态ip地址、DNS服务、网关、子网掩码
IPAddress ip(192, 168, 0, 122);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);



// 创建 DNS服务
DNSClient dnClient;

// 定义DNS解析用的ip
IPAddress dstip;

void setup() {
  // 配置串口
  Serial.begin(9600);
  while (!Serial) {
    ;  // 等待串口连接
  }
  //使用 CSPIN来初始化网卡
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  Ethernet.init(17);

  // 静态IP设置
  Serial.println("Ethernet Begin");
  Ethernet.begin(mac, ip, myDns, gateway, subnet);


  // 输出网卡mac地址、IP地址、子网掩码、DNS、网关
  Serial.print("My Mac address: ");
  byte macBuffer[6];               // 设置mac地址存储buff
  Ethernet.MACAddress(macBuffer);  // 读取mac地址
  for (byte octet = 0; octet < 6; octet++) {
    Serial.print(macBuffer[octet], HEX);
    if (octet < 5) {
      Serial.print('-');
    }
  }
  Serial.println("");

  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

  Serial.print("My subnet: ");
  Serial.println(Ethernet.subnetMask());

  Serial.print("My DNS address: ");
  Serial.println(Ethernet.dnsServerIP());

  Serial.print("My GateWay address: ");
  Serial.println(Ethernet.gatewayIP());

  dnClient.begin(Ethernet.dnsServerIP());

  const char domains[] = "www.eeworld.com.cn";
    if (dnClient.getHostByName(domains, dstip) == 1) {
    Serial.print(domains);
    Serial.print(" = ");
    Serial.println(dstip);
  }
  else Serial.println(F("dns lookup failed"));
}

void loop() {
 
}

上传代码就可以看到串口已经打印相关信息了

   

然后根据之前安装的 ping库的示例魔改即可

#include <SPI.h>         
#include <Ethernet.h>
#include <EthernetICMP.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // max address for ethernet shield
byte ip[] = {192,168,2,177}; // ip address for ethernet shield
IPAddress pingAddr(1,1,1,1); // ip address to ping

SOCKET pingSocket = 0;

char buffer [256];
EthernetICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup() 
{
  // start Ethernet
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
}

void loop()
{
  EthernetICMPEchoReply echoReply = ping(pingAddr, 4);
  if (echoReply.status == SUCCESS)
  {
    sprintf(buffer,
            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
            REQ_DATASIZE,
            millis() - echoReply.data.time,
            echoReply.ttl);
  }
  else
  {
    sprintf(buffer, "Echo request failed; %d", echoReply.status);
  }
  Serial.println(buffer);
  delay(500);
}

最终ping通互联网站点代码如下:

//#include <SPI.h>
#include <Ethernet.h>
// DNS
#include <Dns.h>

#include <EthernetICMP.h>
// 网卡mac地址
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


// 创建 DNS服务
DNSClient dnClient;

// 定义DNS解析用的ip
IPAddress dstip;
// 静态ip地址、DNS服务、网关、子网掩码
IPAddress ip(192, 168, 0, 122);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

SOCKET pingSocket = 0;

char buffer[256];
EthernetICMPPing ping(pingSocket, (uint16_t)random(0, 255));

void setup() {
  // 配置串口
  Serial.begin(9600);
  while (!Serial) {
    ;  // 等待串口连接
  }
  //使用 CSPIN来初始化网卡
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet

  Ethernet.init(17);

  // 静态IP设置
  Serial.println("Ethernet Begin");
  Ethernet.begin(mac, ip, myDns, gateway, subnet);


  // 输出网卡mac地址、IP地址、子网掩码、DNS、网关
  Serial.print("My Mac address: ");
  byte macBuffer[6];               // 设置mac地址存储buff
  Ethernet.MACAddress(macBuffer);  // 读取mac地址
  for (byte octet = 0; octet < 6; octet++) {
    Serial.print(macBuffer[octet], HEX);
    if (octet < 5) {
      Serial.print('-');
    }
  }
  Serial.println("");

  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());

  Serial.print("My subnet: ");
  Serial.println(Ethernet.subnetMask());

  Serial.print("My DNS address: ");
  Serial.println(Ethernet.dnsServerIP());

  Serial.print("My GateWay address: ");
  Serial.println(Ethernet.gatewayIP());

  dnClient.begin(Ethernet.dnsServerIP());

  const char domains[] = "www.eeworld.com.cn";
    if (dnClient.getHostByName(domains, dstip) == 1) {
    Serial.print(domains);
    Serial.print(" = ");
    Serial.println(dstip);
  }
  else Serial.println(F("dns lookup failed"));

  
}

void loop() {
  EthernetICMPEchoReply echoReply = ping(dstip, 4);
  if (echoReply.status == SUCCESS)
  {
    sprintf(buffer,
            "Reply[%d] from: %d.%d.%d.%d: bytes=%d time=%ldms TTL=%d",
            echoReply.data.seq,
            echoReply.addr[0],
            echoReply.addr[1],
            echoReply.addr[2],
            echoReply.addr[3],
            REQ_DATASIZE,
            millis() - echoReply.data.time,
            echoReply.ttl);
  }
  else
  {
    sprintf(buffer, "Echo request failed; %d", echoReply.status);
  }
  Serial.println(buffer);
  delay(500);
}

上传代码后串口显示如下:

 

三、抓包分析

使用Wireshark 进行抓包分析

使用 bing 搜索下载即可

 

下载安装完成后打开软件,由于我是有线网卡,所以选择第一个即可

 

默认情况下Wireshark抓取所有的数据,为了精确查看,可以在输入框输入以下字符,然后点击右侧箭头进行应用进行过滤

ip.dst==192.168.0.122 and ip.src==192.168.0.114

 

过滤后的数据如下

  点击 一条即可查看相关的信息

  其中包括协议Protocol以及原地址目标地址等信息

 

四、总结

此任务完成了W5500_Evb_Pico静态IP配置、Ping、抓包分析,为后续完成其他任务打下了基础。

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表