Theodore Baschak

BOFH. Open Source Guru. Founder/Operator of Hextet Systems, AS395089 and Network Architect for Daemon Defense Systems, AS55101.

pinglog

Mon, 16 Jun 2014 00:12:17 -0500 » Networking, Nerd Projects, Network Monitoring, CLI, IPv6, Programming, ISP, System Administration

I’ve often wished for a way of logging pings when troubleshooting connectivity problems. Tonight I wrote a few shell scripts to help with this. The first set pings a host (ipv4 or ipv6) and runs date when ping fails to respond within 1 second. This can be handy to show connectivity loss periods.

This is the IPv4 version of the script:

pinglog.bashlink
#!/bin/bash
function usage ()
{
echo "Usage:"
echo " $0 <IP>"
echo "OR"
echo " $0 <IP> | tee mylog.txt"
}
if [ $# -ne 1 ]; then
usage
exit 127
fi
IP=$1
while :; do ping -c 1 -W 1 $IP >/dev/null || date; sleep 1; done
The other set of scripts simply prepends date: on the beginning of the output piped from ping. Here is the IPv6 version of that script.
ping6timestamp.bashlink
#!/bin/bash
function usage ()
{
echo "Usage:"
echo " $0 <IP6 | hostname>"
echo "OR"
echo " $0 <IP6 | hostname> | tee mylog.txt"
}
if [ $# -ne 1 ]; then
usage
exit 127
fi
IP=$1
ping6 $IP | while read pong; do echo "$(date): $pong"; done
Output from the timestamp prepender:
| => ./pingtimestamp.bash www.google.ca
Mon 16 Jun 2014 00:24:43 CDT: PING www.google.ca (74.125.70.94): 56 data bytes
Mon 16 Jun 2014 00:24:43 CDT: 64 bytes from 74.125.70.94: icmp_seq=0 ttl=39 time=45.593 ms
Mon 16 Jun 2014 00:24:44 CDT: 64 bytes from 74.125.70.94: icmp_seq=1 ttl=39 time=46.633 ms
Mon 16 Jun 2014 00:24:45 CDT: 64 bytes from 74.125.70.94: icmp_seq=2 ttl=39 time=46.760 ms
Mon 16 Jun 2014 00:24:46 CDT: 64 bytes from 74.125.70.94: icmp_seq=3 ttl=39 time=45.417 ms
Mon 16 Jun 2014 00:24:47 CDT: 64 bytes from 74.125.70.94: icmp_seq=4 ttl=39 time=47.271 ms
Mon 16 Jun 2014 00:24:48 CDT: 64 bytes from 74.125.70.94: icmp_seq=5 ttl=39 time=44.297 ms
Mon 16 Jun 2014 00:24:49 CDT: 64 bytes from 74.125.70.94: icmp_seq=6 ttl=39 time=46.806 ms
Mon 16 Jun 2014 00:24:50 CDT: 64 bytes from 74.125.70.94: icmp_seq=7 ttl=39 time=46.765 ms
Mon 16 Jun 2014 00:24:51 CDT: 64 bytes from 74.125.70.94: icmp_seq=8 ttl=39 time=48.634 ms
Mon 16 Jun 2014 00:24:52 CDT: 64 bytes from 74.125.70.94: icmp_seq=9 ttl=39 time=43.894 ms
Mon 16 Jun 2014 00:24:53 CDT: 64 bytes from 74.125.70.94: icmp_seq=10 ttl=39 time=45.460 ms
Mon 16 Jun 2014 00:24:54 CDT: 64 bytes from 74.125.70.94: icmp_seq=11 ttl=39 time=44.406 ms
Mon 16 Jun 2014 00:24:55 CDT: 64 bytes from 74.125.70.94: icmp_seq=12 ttl=39 time=44.899 ms
^C
© Theodore Baschak - https://github.com/tbaschak - Powered by Jekyll.
Powered by Les.net.
CiscoDude.net is a personal website. Opinions expressed are not necessarily those of his employer.