-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-drift.py
executable file
·55 lines (39 loc) · 1.19 KB
/
time-drift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
import struct,time,sys,socket
if (len(sys.argv) < 2) or (sys.argv[1] == '--help'):
print """
...........................:: TIME-DRIFT ::.............................
Synopsis:
Time-drift.py shows the difference between time on local machine
and time provided by NTP server.
usage:
time-drift.py ntp-server
.............................................:: Asazello, 16.02.15 ::...
"""
exit(1)
NTPserv = sys.argv[1]
# Time server and its port
time_server = (NTPserv, 123)
# Epoch in Unix format
epoch = 2208988800L
try:
client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
# Prepare data for server
data = '\x1b' + 47 * '\0'
# Send data to the server
client.sendto(data, time_server)
# Recieve data back from the server
data, address = client.recvfrom( 1024 )
if data:
# Process the data
time_now = struct.unpack( '!12I', data )[10]
# An exception if the time is 0
if time_now == 0:
# Invalid response
print '0'
# Calculate current time
print ((time_now - epoch) - time.time())
else:
print '0'
except:
print '0'