- Raspberry Pi Zero Cookbook
- Edward Snajder
- 541字
- 2021-07-09 19:12:11
Pinging from another computer over same network
Now that we have our computer on the network, we want to make sure other computers are able to contact it. The ping
command is one of the first tests to tell whether your computer can be contacted. The command sends a small test packet of data to another computer's IP address or hostname and waits to hear a response. If it does, it tells you how long it took for the response to come back. If it doesn't, it tells you it is not receiving a response.
Note
Some computer systems have firewalls that do not allow ICMP ECHO attempts. This means that even though the computer is available on the network, attempts to ping it will fail. On the default configuration of Raspbian, ping
commands are allowed, so attempts to ping your Pi should succeed.
Getting ready
As long you as you know your IP address (whether it was dynamically assigned using DHCP or you assigned a static IP from the previous recipe), you should be able to contact it from your network. The easiest way to do this is by looking at ifconfig
. From your serial terminal connection, and if you followed one of the previous recipes to add Wi-Fi connectivity, ifconfig
should report the IP address of your Raspberry Pi Zero:
pi@rpz14101:~$ ifconfig lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:264 errors:0 dropped:0 overruns:0 frame:0 TX packets:264 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:21840 (21.3 KiB) TX bytes:21840 (21.3 KiB) wlan0 Link encap:Ethernet HWaddr 00:13:ef:80:0b:41 inet addr:192.168.2.119 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::a577:b1b7:a7a7:8a60/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:212682 errors:0 dropped:54847 overruns:0 frame:0 TX packets:19599 errors:0 dropped:1 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:34267586 (32.6 MiB) TX bytes:1911492 (1.8 MiB)
The wlan0
interface is the one I set up in the previous recipe. The inet addr
field shows my Raspberry Pi Zero's IP address. This is the one I want to try and contact.
How to do it...
ping <IP address>
, like this, for example:ping
192.168.2.119
- On a Windows machine, you can execute
ping
via the command line (Windows + R followed bycmd
). Below is a shot of a typical successful response: - Trying
192.168.2.120
, which isn't assigned to anything on my home network, will show a failed ping:
This means that there is nothing responding from that IP address, which could mean it is down, off, or blocked by a firewall. The command ping /?
will give you all available options for the Windows ping
command. By default, the Windows ping
tries four times and reports the results.
In an OSX or Linux terminal, the ping
utility will continue trying to reach the address and report back the response time (or whether it failed). Breaking the utility with Ctrl + C will stop it.
Running ping -help
will give you a list of all the options, and man ping
will give you a more detailed document of what is possible. A few favorite flags of administrators are the following:
ping -c 5 192.168.2.70
This will try to ping 5
times and then stop.
ping -i 2 192.168.2.70
This will send a ping command every 2
seconds.