Bash Basics: Ping, Date and While

Ping is one of the most handy and basic tools for network administration. Simply put a ping will test whether or not a host is reachable over IP. It does so by sending an echo request via the Internet Control Message Protocol or ICMP. ICMP is pretty neat, being a core protocol of the Internet Protocol or IP suite. Unline TCP or UDP it isn’t meant to be used for exchanging data, rather diagnosing problems and verifying connections.

Technically speaking the ICMP protocol is used for more than just pings or “echo requests” and “echo replies” — there are several control messages like time exceeded, address mask queries and traceroutes. But for right now we’re focusing on the practical with IPv4 and a little scripting to make our lives easier.

The basic ping command consists of just one value, the host. For example: ping 8.8.8.8. When a host responds we’ll get a message saying how many bytes were received along with the IP, an ICMP sequence number, a time to live and how long it took for the host to reply in milliseconds. On Linux this will go on forever so use the key combo CTRL+C to stop.

Now unfortunately the ping command doesn’t by default tell us when the replies, or lack there of, came in. To remedy this we’ll be using the date variable.

Issuing date will provide us with a date and time stamp. As such the date can be used as a variable by issuing echo $(date). There are many display options supported by date. Issue man date for a complete list. I’m a fan of %F and %T as they provide a nicely formated numeric date and time stamp. To use these display options we’ll echo $(date +%F_%T).

Now let’s bring it all together. We’ll start by issuing ping 8.8.8.8 then pipe | it to a loop using while and read the input into a variable that we’ll call pong using the command read. So far this will continue pinging our host and turning the ping replies into pong variables. We’ll separate the while statement with a semicolon ; and tell the loop what to do with the aptly named do command. In this case we want to echo the date “$(date +%F_%T), give it a separater, I’ll use two dashes and finally our newly created variable $pong”. Once again separate with a semicolon ; and end the loop with done. Your command should look like ping 8.8.8.8 | while read pong; do echo “$(date +%F_%T) — $pong”;done

There we go, a constant ping complete with the date and time — perfect for debugging all sorts of borked network bits.

Article source: http://revision3.com/haktip/icmpyouknowme

View full post on National Cyber Security

Gregory Evans | LinkedIn

Interview With Gregory Evans

Gregory Evans Security Expert

Gregory Evans on Cyber Crime

Leave a Reply