Linux command: ping

Olarik Surinta
olarik
Published in
2 min readFeb 12, 2019

--

ping เป็นคำสั่งสำหรับใช้ส่งแพ็กเกจ (Packet) ผ่านเน็ตเวิร์ก (Network) ด้วยโปรโตคอล ICMP (Internet Control Message Protocol) ไปยังเครื่องต้นทาง (Host) ดังนั้น ping จึงเป็นวิธีที่ง่ายที่สามารถส่งและรับข้อมูลไปยังเครื่องคอมพิวเตอร์อื่นบนเครือข่ายคอมพิวเตอร์ โดยทั่วไปแล้วคำสั่ง ping ใช้เพื่อทดสอบสถานะของเครื่องคอมพิวเตอร์ปลายทาง

การเรียกดูตัวเลือก (Option) ของคำสั่ง ping

$ ping
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination

การใช้คำสั่ง ping ไปยังเครื่องคอมพิวเตอร์ปลายทาง

  • ตัวอย่างต่อไปนี้ทำการทดสอบการใช้คำสั่ง ping ไปยังเครื่องคอมพิวเตอร์โดย ping ไปยังหมายเลข ip address ของเครื่องคอมพิวเตอร์ แสดงดังตัวอย่างต่อไปนี้
$ ping 10.33.1.118
PING 10.33.1.118 (10.33.1.118) 56(84) bytes of data.
64 bytes from 10.33.1.118: icmp_seq=1 ttl=64 time=0.501 ms
64 bytes from 10.33.1.118: icmp_seq=2 ttl=64 time=0.312 ms
64 bytes from 10.33.1.118: icmp_seq=3 ttl=64 time=0.258 ms
64 bytes from 10.33.1.118: icmp_seq=4 ttl=64 time=0.259 ms
64 bytes from 10.33.1.118: icmp_seq=5 ttl=64 time=0.319 ms
64 bytes from 10.33.1.118: icmp_seq=6 ttl=64 time=0.489 ms
64 bytes from 10.33.1.118: icmp_seq=7 ttl=64 time=0.312 ms
^C
--- 10.33.1.118 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6151ms
rtt min/avg/max/mdev = 0.258/0.350/0.501/0.094 ms

จากตัวอย่างข้างต้น หากเครื่องคอมพิวเตอร์ปลายทาง ในกรณีนี้คือ 10.33.1.118 หากเครื่องคอมพิวเตอร์นั้นสามารถให้บริการ หรือเปิดเครื่องอยู่จะส่งข้อความตอบกลับ และ packet ที่ส่งไปทดสอบจะไม่สูญหาย (loss)

  • ตัวอย่าง การเพิ่มตัวเลือกในการใช้คำสั่ง ping โดยใช้ตัวเลือก -c ซึ่งหมายถึง การนับ (count) จำนวน packet ที่ส่งออกไป ตัวอย่างเช่น
$ ping -c 5 10.33.1.118
PING 10.33.1.118 (10.33.1.118) 56(84) bytes of data.
64 bytes from 10.33.1.118: icmp_seq=1 ttl=64 time=0.522 ms
64 bytes from 10.33.1.118: icmp_seq=2 ttl=64 time=0.482 ms
64 bytes from 10.33.1.118: icmp_seq=3 ttl=64 time=0.508 ms
64 bytes from 10.33.1.118: icmp_seq=4 ttl=64 time=0.280 ms
64 bytes from 10.33.1.118: icmp_seq=5 ttl=64 time=0.336 ms
--- 10.33.1.118 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4093ms
rtt min/avg/max/mdev = 0.280/0.425/0.522/0.101 ms

จากตัวอย่างการใช้ตัวเลือก -c 5 หมายถึง ทำการ ping ไปยังเครื่อง 10.33.1.118 โดยส่งไปจำนวน 5 ครั้ง

  • ดังนั้น หากเครื่องคอมพิวเตอร์ปลายทางไม่สามารถให้บริการ จะแสดงข้อความดังต่อไปนี้
$ ping 10.33.1.115
PING 10.33.1.115 (10.33.1.115) 56(84) bytes of data.
From 10.33.1.98 icmp_seq=1 Destination Host Unreachable
From 10.33.1.98 icmp_seq=2 Destination Host Unreachable
From 10.33.1.98 icmp_seq=5 Destination Host Unreachable
From 10.33.1.98 icmp_seq=6 Destination Host Unreachable
From 10.33.1.98 icmp_seq=7 Destination Host Unreachable
From 10.33.1.98 icmp_seq=8 Destination Host Unreachable
From 10.33.1.98 icmp_seq=9 Destination Host Unreachable
^C
--- 10.33.1.115 ping statistics ---
10 packets transmitted, 0 received, +7 errors, 100% packet loss, time 9203ms
pipe 4

จากตัวอย่างข้างต้น ได้ทำการทดสอบโดยใช้คำสั่ง ping ไปยังเครื่องหมายเลข 10.33.1.115 โดยได้รับข้อความตอบกลับมาจากระบบว่าไม่สามารถเชื่อมต่อไปยังเครื่องปลายทางทำให้ packet ที่ส่งไปทั้งหมด 10 packet สูญหายทั้งหมด 100%

--

--