| | |
| | | pwd := config.Server.SudoPassword |
| | | cmdStr := fmt.Sprintf("echo %s | sudo -S %s", pwd, cmd) |
| | | |
| | | return exec.Command("/bin/sh", "-c", cmdStr).Output() |
| | | return exec.Command("/bin/bash", "-c", cmdStr).Output() |
| | | } |
| | | |
| | | // 检查 root权限 |
| | | func CheckRootPermissions() bool { |
| | | showRootCMD := exec.Command("/bin/sh", "-c", "ls /root/") |
| | | showRootCMD := exec.Command("/bin/bash", "-c", "ls /root/") |
| | | if _, err := showRootCMD.Output(); err != nil { |
| | | return false |
| | | } |
| | |
| | | |
| | | // 获取本机网关ip |
| | | func GetDefaultRoute(networkName string) (route string, err error) { |
| | | cmdStr := fmt.Sprintf("ip route show | grep -P \"^default.*%s\" | awk '{printf $3}'", networkName) |
| | | cmdStr := fmt.Sprintf("ip route show | grep -P \"^default.*%s onlink\" | awk '{printf $3}'", networkName) |
| | | cmd := exec.Command("/bin/sh", "-c", cmdStr) |
| | | b, err := cmd.Output() |
| | | if err != nil { |
| | |
| | | |
| | | // 配置服务器网络 |
| | | func SetNetworkConfig(ipv4, netmask, gateway, dns string) (bool, string) { |
| | | networkConfigScript := "/opt/vasystem/bin/netconfig" |
| | | networkConfigScript := "/opt/vasystem/script/netconfig" |
| | | ifname := config.Server.NetworkAdapter |
| | | localIPv4, localNetMask, _ := GetLocalIP(ifname) |
| | | localGW, _ := GetDefaultRoute(ifname) |
| | |
| | | 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, " ") |
| | |
| | | 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() |
| | |
| | | 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) { |
| | |
| | | |
| | | 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 |
| | | } |
| | | |
| | | const ngConfig = "/opt/vasystem/config/system-2.0.conf" |
| | | func GetNginxListenPort() string { |
| | | _, err := os.Stat(ngConfig) |
| | | if err != nil { |
| | | return "7003" |
| | | } |
| | | |
| | | cmdStr := fmt.Sprintf("cat %s | grep listen | awk '{printf $2}' | sed 's/;//g'", ngConfig) |
| | | cmd := exec.Command("/bin/sh", "-c", cmdStr) |
| | | port, err := cmd.Output() |
| | | if err != nil { |
| | | return "" |
| | | } |
| | | |
| | | return string(port) |
| | | } |
| | | |
| | | func SetNginxListenPort(port string) bool { |
| | | _, err := os.Stat(ngConfig) |
| | | if err != nil { |
| | | return false |
| | | } |
| | | |
| | | // 修改配置文件端口 |
| | | cmdStr := fmt.Sprintf("sed -i 's/listen.*/listen %s;/' %s", port, ngConfig) |
| | | cmd := exec.Command("/bin/sh", "-c", cmdStr) |
| | | _, err = cmd.Output() |
| | | if err != nil { |
| | | return false |
| | | } |
| | | |
| | | // 重启nginx |
| | | _, err = execRootCommand("service nginx reload") |
| | | |
| | | return err == nil |
| | | } |