If you are not on the same network segment, that is, you have to go through the gateway, there are several situations. If there is a record in the ARP record of the ping host, the ICMP message will be sent to the switch, and the switch will send it to the gateway, which will forward it according to the route. If the switch has no MAC record, it will flood, and the gateway will reply to the source with its own MAC address and the IP address of the target computer according to the proxy ARP. At this time, the switch will write the destination MAC, that is, the MAC of the gateway and the corresponding port number into the MAC address table and forward it.
If the ICMP message is unreachable, an unreachable return will be echoed.
You may feel sorry if I say so. This problem should consider the working principle of three-layer ARP and proxy ARP under OSI and the MAC address table of the switch.
The above is my personal understanding. If not, please advise.