sunty
2019-11-19 d9e01c51a525adf4f2393a95f87604e3b6e79ad2
extend/sys/system.go
@@ -1,11 +1,7 @@
package sys
import (
   "encoding/json"
   "errors"
   "io/ioutil"
   "net/http"
   "net/url"
   "os"
   "os/exec"
   "strconv"
@@ -18,40 +14,11 @@
   "time"
)
func execRootCommand(cmd string) (bool, string) {
   serverPort := config.Server.SysServerPort
   if serverPort == "" {
      serverPort = "18081"
   }
   sysConfigServer := fmt.Sprintf("http://127.0.0.1:%s/api/v1/command/exec", serverPort)
   resp, err := http.PostForm(sysConfigServer, url.Values{"command": {cmd}})
func execRootCommand(cmd string) ([]byte, error) {
   pwd := config.Server.SudoPassword
   cmdStr := fmt.Sprintf("echo %s | sudo -S %s", pwd, cmd)
   if err != nil {
      return false, err.Error()
   }
   defer resp.Body.Close()
   body, err := ioutil.ReadAll(resp.Body)
   if err != nil {
      return false, err.Error()
   }
   type response struct {
      Code    int    `json:"code"`
      Success bool   `json:"success"`
      Msg     string `json:"msg"`
      CMD     string `json:"cmd"`
      Data    string `json:"data"`
   }
   var rsp response
   if err := json.Unmarshal(body, &rsp); err != nil {
      return false, err.Error()
   }
   fmt.Println(body)
   fmt.Println(rsp)
   return rsp.Success, rsp.Data
   return exec.Command("/bin/sh", "-c", cmdStr).Output()
}
// 检查 root权限
@@ -132,8 +99,9 @@
   }
   // # netconfig enp59s0f0 192.168.1.2 255.255.255.0 192.168.1.1 192.168.100.1
   cmdStr := fmt.Sprintf("%s %s %s %s %s %s", networkConfigScript, ifname, ipv4, netmask, gateway, dns)
   // return exec.Command("/bin/sh", "-c", cmdStr).Run() == nil
   return execRootCommand(cmdStr)
   stdout, err := execRootCommand(cmdStr)
   return err == nil, string(stdout)
}
// 配置时区
@@ -151,8 +119,7 @@
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()
   cron, _ := execRootCommand("crontab -l | grep ntpdate | tr -d '\n'")
   if task := string(cron); task != "" {
      status = true
      slice := strings.Split(task, " ")
@@ -192,14 +159,14 @@
      return false
   }
   args := []string{"-s", newTime}
   exec.Command("date", args...).Run()
   dateCMD := fmt.Sprintf("date -s \"%s\"", newTime)
   execRootCommand(dateCMD)
   stopNTPCron()
   return true
}
const NTPCRONTABFILE = "~/.webServer.crontab"
const NTPCRONTABFILE = "/tmp/.webServer.crontab"
func EnableNTPCron(server string, interval int) bool {
   stopNTPCron()
@@ -208,22 +175,62 @@
      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()
   update := fmt.Sprintf("echo \"*/%d * * * * /usr/sbin/ntpdate %s\" >> %s", interval, server, NTPCRONTABFILE)
   execRootCommand(update)
   addNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
   execRootCommand(addNTPTask)
   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()
   update := fmt.Sprintf("crontab -l | grep -v /usr/sbin/ntpdate > %s", NTPCRONTABFILE)
   execRootCommand(update)
   cleanNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
   execRootCommand(cleanNTPTask)
}
func RunNTPDate(server string) (bool, error) {
func RunNTPDate(server string) bool {
   if ip := net.ParseIP(server); ip == nil {
      return false, errors.New("参数错误")
      return false
   }
   ntpdate := fmt.Sprintf("/usr/sbin/ntpdate %s", server)
   return true, exec.Command("/bin/sh", "-c", ntpdate).Run()
   _, err := execRootCommand(ntpdate)
   return err == nil
}
func Reboot() (bool, string) {
   stdout, err := execRootCommand("reboot")
   return err == nil, string(stdout)
}
// * * * * * /bin/echo "$(date) Perform basic-reboot-task" >> /tmp/webserver.crontab.log;/sbin/reboot & >> /tmp/webserver.crontab.log
func ReadRebootTaskInCrontab() (bool, string) {
   stdout, err := execRootCommand("crontab -l | grep basic-reboot-task | sed -z -r 's/([^0-9* ]+)(.+)//g'")
   return err == nil, string(stdout)
}
func CleanRebootTask() {
   update := fmt.Sprintf("crontab -l | grep -v basic-reboot-task > %s", NTPCRONTABFILE)
   execRootCommand(update)
   crontab := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
   execRootCommand(crontab)
}
func UpdateRebootTask(task string) bool {
   CleanRebootTask()
   tasks := fmt.Sprintf("%s /bin/echo \"$(date) Perform basic-reboot-task\" >> /tmp/webserver.crontab.log;/sbin/reboot & >> /tmp/webserver.crontab.log", task)
   update := fmt.Sprintf("echo '%s' >> %s", tasks, NTPCRONTABFILE)
   execRootCommand(update)
   addNTPTask := fmt.Sprintf("crontab %s", NTPCRONTABFILE)
   _, err := execRootCommand(addNTPTask)
   return err == nil
}