Detect the time difference between two Linux hosts
Timestamp data can be placed in the header of IP packets and ICMP packets. The clockdiff program uses timestamps to calculate the system time difference between the destination host and the local host.
-o: Use the IP timestamp option to measure system time differences. Only use 3 timestamps.
-o1: Use the IP timestamp option to measure system time differences. Use 4 timestamps. If neither -o nor -o1 is set, the ICMP timestamp is used to test the system time difference.
lixi@lixi-desktop:~$ ping -T tsandaddr www.ustc.edu.cn -c 1
PING www.ustc.edu.cn (202.38.64.9) 56(124) bytes of data.
64 bytes from 202.38.64.9: icmp_seq=1 ttl=62 time=0.823 ms
TS: lixi-desktop.local (210.45.74.25) 12522473 absolute
210.45.74.1-251
local-gw.ustc.edu.cn (202.38.64.126) 248
202.38.64.9-857514
Unrecorded hops: 3
--- www.ustc.edu.cn ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.823/0.823/0.823/0.000 ms
First, from the above, we can draw the relationship between several ICMP timestamps when the RRT is not large. The time difference between local host and 202.38.64.9 is approximately: -857514+248-251=-857517. Test the system time of the above route using -o (timestamp in IP option) and without option (ICMP route timestamp). The results obtained:
lixi@lixi-desktop:~# ./clockdiff -o 202.38.64.9
.................................................................
host=202.38.64.9 rtt=1(0)ms/1ms delta=-857517ms/-857517ms Wed Dec 17 11:28:30 2008
lixi@lixi-desktop:~# ./clockdiff 202.38.64.9
.
host=202.38.64.9 rtt=750(187)ms/0ms delta=-857517ms/-857517ms Wed Dec 17 11:28:35 2008
Both methods of testing are relatively accurate.
lixi@lixi-desktop:~#./clockdiff gigagate1.Princeton.EDU
.................................................................
host=gigagate1.Princeton.EDU rtt=307(21)ms/271ms delta=-5ms/-5ms Wed Dec 17 11:50:16 2008
The above is to test the system time difference between a destination host with a large RTT and the local host. However, when using clockdiff, you need a little luck, because many routes ignore ICMP or IP timestamps.