zhangzengfei
2019-10-21 e871758bd82d9a4fbce42e43c9665d869c240dfb
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
105
106
107
108
109
110
111
112
113
package sys
 
import (
    "errors"
    "os/exec"
 
    "fmt"
    "net"
    "strings"
    "time"
)
 
// 检查 root权限
func CheckRootPermissions() bool {
    showRootCMD := exec.Command("/bin/sh", "-c", "ls /root/")
    if _, err := showRootCMD.Output(); err != nil {
        return false
    }
 
    return true
}
 
// 配置时区
func TimeZone() (string, int64) {
    cmd := exec.Command("/bin/sh", "-c", "echo -n $TZ")
    tz, _ := cmd.Output()
    if tzstr := string(tz); tzstr != "" {
        return tzstr, time.Now().Unix()
    }
 
    zone, _ := time.Now().Zone()
    return zone, time.Now().Unix()
}
 
func NTPConfig() (bool, string, string) {
    status, server, interval := false, "", ""
 
    cmd := exec.Command("/bin/sh", "-c", "crontab -l | grep ntpdate | tr -d '\n'")
    cron, _ := cmd.Output()
    if task := string(cron); task != "" {
        status = true
        slice := strings.Split(task, " ")
        interval, server = slice[0][2:], slice[len(slice)-1]
    }
 
    return status, server, interval
}
 
// 设置时区
func SetTimeZone(tz string) bool {
    if _, err := time.LoadLocation(tz); err != nil {
        return false
    }
 
    // set env
    envCMD := exec.Command("/bin/sh", "-c", "TZ=%s", tz)
    envCMD.Run()
    // change permanent to the file '.profile'
 
    cleanTZ := exec.Command("/bin/sh", "-c", "sed -i '/^TZ=.*$/d' ~/.profile")
    cleanTZ.Run()
 
    TZ := "TZ='" + tz + "'; export TZ"
    appendTZCMD := fmt.Sprintf("echo \"%s\" >> ~/.profile;", TZ)
    appendTZ := exec.Command("/bin/sh", "-c", appendTZCMD)
    appendTZ.Run()
 
    return true
}
 
// 配置系统时间
func SetLocalTime(newTime string) bool {
    const TimeLayout = "2006-01-02 15:04:05"
    _, err := time.Parse(TimeLayout, newTime)
    if err != nil {
        return false
    }
 
    args := []string{"-s", newTime}
    exec.Command("date", args...).Run()
    stopNTPCron()
 
    return true
}
 
const NTPCRONTABFILE = "~/.webServer.crontab"
 
func EnableNTPCron(server string, interval int) bool {
    stopNTPCron()
 
    if ip := net.ParseIP(server); ip == nil {
        return false
    }
 
    addTask := fmt.Sprintf("echo \"*/%d * * * * /usr/sbin/ntpdate %s\" >> %s; crontab %s", interval, server, NTPCRONTABFILE, NTPCRONTABFILE)
    exec.Command("/bin/sh", "-c", addTask).Run()
 
    return true
}
 
func stopNTPCron() {
    cleanTask := fmt.Sprintf("crontab -l | grep -v /usr/sbin/ntpdate > %s; crontab %s", NTPCRONTABFILE, NTPCRONTABFILE)
    exec.Command("/bin/sh", "-c", cleanTask).Run()
}
 
func RunNTPDate(server string) (bool, error) {
    if ip := net.ParseIP(server); ip == nil {
        return false, errors.New("参数错误")
    }
 
    ntpdate := fmt.Sprintf("/usr/sbin/ntpdate %s", server)
    return true, exec.Command("/bin/sh", "-c", ntpdate).Run()
}