ping-date.sh - A shell script to send a ping containing the current date/time as text

Blue Bar separator

Timestamps in a packet trace file can be tricky especially when the trace was collected on host A and you are looking at it on host B half way around the world. If you are trying to correlate a timestamp in a log file from host C the difficulty level goes up a notch. It is possible to correct for time zone differences but you need to be sure what the time zones are and even then sometimes it just doesn't seem to work correctly. If the times of both (or all three) systems have not been sync'ed correcting the time zone may not help. What I wanted to do was to insert a packet with the local time of host C displayed as text so that I could compare the time on the sending system with the time in the trace. However, for various reasons I could not use the Perl script How to make a trace file self documenting. I could however use ping so I wrote the following script.

Usage

ping-date.sh IP-ADDRESS SLEEP COUNT

Where:
IP-ADDRESS is the address or host name of the target of the ping. This host may not have anything to do with the problem for which you are collecting the trace. It doesn't even need to be answered since all we are really interested in is the data portion of the ping request

SLEEP Seconds between pings.

COUNT The number of pings to send. A count of 0 indicates that pings will be sent until the script is manually stopped. Standard Output is redirected to /dev/null.

Limitations

  1. The date command prints the time to the nearest second.
  2. This only works for Linux based systems, The ping command in MS Windows does not support the ability to insert a string into the ping payload

Examples

Figure 1 shows the message you get when you run the script with anything but 3 arguments

# sh ping-date.sh                                                                             
Usage:
        ping-date IP-ADDRESS SLEEP COUNT
                IP-ADDRESS is the IP ADDRESS or host name of the target        
                SLEEP is the time in seconds between pings
                COUNT number of pings to send, 0 means forever
                Example:
                  ping-date 172.16.1.11 1 0
#
Figure 1

Figure 2 shows shows what it looks like when you run the script with a 0 count - basically, it loops forever. You can exit the loop with a control-C.

# sh ping-date.sh 172.16.1.11 1 0                                                             
Figure 2

Figure 3 shows the packets generated by the command in figure 2, note that the time between the sending system and the receiving system seems to be out of sync by a 43 seconds.

tcpdump -X -i enp0s25 src 172.16.1.200
. . . . 
04:14:45.652449 IP 172.16.1.200 > 172.16.1.11: ICMP echo request, id 52535, seq 1, length 64  
        0x0000:  4500 0054 0000 4000 4001 dfb5 ac10 01c8  E..T..@.@.......
        0x0010:  ac10 010b 0800 92ab cd37 0001 d088 e555  .........7.....U
        0x0020:  0000 0000 1167 0b00 0000 0000 5365 7020  .....g......Sep.
        0x0030:  3120 3034 3a31 353a 3238 0a00 5365 7020  1.04:15:28..Sep.
        0x0040:  3120 3034 3a31 353a 3238 0a00 5365 7020  1.04:15:28..Sep.
        0x0050:  3120 3034                                1.04
04:14:46.662479 IP 172.16.1.200 > 172.16.1.11: ICMP echo request, id 55351, seq 1, length 64
        0x0000:  4500 0054 0000 4000 4001 dfb5 ac10 01c8  E..T..@.@.......
        0x0010:  ac10 010b 0800 6482 d837 0001 d188 e555  ......d..7.....U
        0x0020:  0000 0000 338e 0b00 0000 0000 5365 7020  ....3.......Sep.
        0x0030:  3120 3034 3a31 353a 3239 0a00 5365 7020  1.04:15:29..Sep.
        0x0040:  3120 3034 3a31 353a 3239 0a00 5365 7020  1.04:15:29..Sep.
        0x0050:  3120 3034                                1.04
Figure 3

ping-date.sh

 
# ping-date.sh                                                                             
#
# usage
#   ping-date.sh IP-ADDRESS SLEEP COUNT
#
if [ $# -ne 3 ]
   then echo -e "Usage:\n\tping-date IP-ADDRESS SLEEP COUNT"
        echo -e "\t\tIP-ADDRESS is the IP ADDRESS or host name of the target"
        echo -e "\t\tSLEEP is the time in seconds between pings"
        echo -e "\t\tCOUNT number of pings to send, 0 means forever"
        echo -e "\t\tExample:"
        echo -e "\t\t  ping-date 172.16.1.11 1 0"
        exit
fi

if [ $3 -eq 0 ]
   then while [ true ]
   do
     ping $1 -q -c 1 -p $(echo $(date | cut -c 5-20) | od -x | cut -c 9-47 | \
       head -1 | awk '{print substr($1,3,2) substr($1,1,2) substr($2,3,2) \
       substr($2,1,2) substr($3,3,2) substr($3,1,2) substr($4,3,2) \
       substr($4,1,2) substr($5,3,2) substr($5,1,2) substr($6,3,2) \
       substr($6,1,2) substr($7,3,2) substr($7,1,2) substr($8,3,2) \
       substr($8,1,2)}') > /dev/null
     sleep $2
   done
else
   COUNT=0
   while [ $COUNT -lt $3 ]
   do
     ping $1 -q -c 1 -p $(echo $(date | cut -c 5-20) | od -x | cut -c 9-47 | \
       head -1 | awk '{print substr($1,3,2) substr($1,1,2) substr($2,3,2) \
       substr($2,1,2) substr($3,3,2) substr($3,1,2) substr($4,3,2) \
       substr($4,1,2) substr($5,3,2) substr($5,1,2) substr($6,3,2) \
       substr($6,1,2) substr($7,3,2) substr($7,1,2) substr($8,3,2) \
       substr($8,1,2)}') > /dev/null
     COUNT=$(($COUNT+1))
     sleep $2
   done
fi



Blue Bar separator
This page was last modified on 15-9-06
mailbox Send comments and suggestions
to noah@noahdavids.org