-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntpTool.go
104 lines (77 loc) · 2.72 KB
/
ntpTool.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"net"
"os"
"time"
"github.com/JK19/ntp/ntpPacket"
)
func main() {
serverFlag := flag.String("s", "", "NTP server address")
zoneFlag := flag.String("tz", "Local", "Timezone from IANA timezone database")
leapFlag := flag.Uint("li", 0, "Leap indicator")
verFlag := flag.Uint("ver", 3, "NTP protocol version")
flag.Usage = func() {
fmt.Println("\nUsage: ntp [-s <server>] [-tz <timezone>] [-ver <version>] [-li <indicator>] ")
fmt.Println("\n\nOptions: ")
fmt.Println("-s <server> NTP server address")
fmt.Println("-tz <timezone> Timezone from IANA timezone database")
fmt.Println("-ver <version> NTP protocol version")
fmt.Println("-li <indicator> 0 -No leap second adjustment")
fmt.Println(" 1 -Last minute of the day has 61 seconds")
fmt.Println(" 2 -Last minute of the day has 59 seconds")
fmt.Println(" 3 -Clock is unsynchronized")
fmt.Println("\nDefaults: ")
fmt.Println("-tz Local")
fmt.Println("-ver 3")
fmt.Println("-li 0")
}
flag.Parse()
if *serverFlag == "" {
fmt.Fprint(os.Stderr, "ERROR: a server must be specified\n")
flag.Usage()
os.Exit(1)
}
loc, err := time.LoadLocation(*zoneFlag)
if err != nil {
fmt.Fprint(os.Stderr, "ERROR: could not find the timezone specified\n")
os.Exit(1)
}
address := *serverFlag + ":" + "123"
fmt.Println("\nRequesting time to: " + address + "\n")
socket, err := net.Dial("udp", address)
if err != nil {
fmt.Fprint(os.Stderr, "ERROR: could not create socket\n")
os.Exit(1)
}
defer socket.Close()
packet := ntpPacket.NewNtpPacket()
packet.SetLeap(uint8(*leapFlag))
packet.SetVersion(uint8(*verFlag))
err = packet.SendTo(socket)
if err != nil {
fmt.Fprint(os.Stderr, "ERROR: failed to send data to server\n")
os.Exit(1)
}
err = packet.ReadFrom(socket)
if err != nil {
fmt.Fprint(os.Stderr, "ERROR: failed to read data from server\n")
os.Exit(1)
}
fmt.Printf("--- Response from %s ---\n", *serverFlag)
fmt.Println("Leap indicator: ", packet.GetLeap())
fmt.Println("Version: ", packet.GetVersion())
fmt.Println("Mode: ", packet.GetMode())
fmt.Println("Stratum: ", packet.Getstratum())
fmt.Println("Poll interval: ", packet.GetPollInterval())
fmt.Println("Precision: ", packet.Getprecision())
fmt.Println("Root delay: ", packet.GetRootDelay())
fmt.Println("Root dispersion: ", packet.GetRootDispersion())
fmt.Println("Reference clock id: ", packet.GetRefClokId())
fmt.Println("Reference timestamp: ", packet.GetRefTimestamp())
fmt.Println("Originate timestamp: ", packet.GetOriginTimestamp())
fmt.Println("Receive timestamp: ", packet.GetRxTimestamp())
fmt.Println("Transmit timestamp: ", packet.GetTxTimestamp())
fmt.Println("\n\nTime from server: ", packet.GetTime().In(loc))
}