Question: When I execute ping command from the command line, it keeps sending the packets until I hit CTRL-C to terminate the ping command output. How can I execute ping command only for N number of packets and terminate the output automatically?
Answer: Use ping option -c to specify the number of packets. After sending N number of packets, ping command will terminate automatically as explained below.
Ping Command – Interactive Mode
In the following example, you have to press CTRL-C to terminate the ping command output.
$ ping 0
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.006 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.004 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.005 ms
....
Note: Press CTRL-C to terminate.
--- 0 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.004/0.008/0.023/0.007 ms
- Note: ping 0 — pings the local host.
Ping Command – Non Interactive Mode (Specify number of packets to be sent)
In the following example, ping command will send only 2 packets and you don’t need to press CTRL-C to terminate the output.
$ ping 0 -c 2
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.006 ms
--- 0 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.006/0.015/0.024/0.009 ms
Ping Command Option -c Usages
Redirect the ping command output to a file
$ ping 127.0.0.1 -c 2 > ping-output.txt
$ cat ping-output.txt
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.005 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.005/0.010/0.015/0.005 ms
Redirect to another process. This example shows only the ping output summary (last two lines).
$ ping 127.0.0.1 -c 10 | tail -2
10 packets transmitted, 10 received, 0% packet loss, time 8999ms
rtt min/avg/max/mdev = 0.005/0.006/0.014/0.003 ms
Hiç yorum yok:
Yorum Gönder