1、import
import (
“os/exec”
“syscall”
)
2、示例
// ExecCommand linux执行命令
func ExecCommandWin(script string) string {
// Windows c:\windows\system32\inetsrv\APPCMD
cmd := exec.Command(“exe路径”, “执行命令”)
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
return string(out)
}
// ExecCommand linux执行命令
func ExecCommandSh(script string) string {
result := exec.Command("/bin/sh", “-c”, script)
res, err := result.CombinedOutput()
if err != nil || string(res) == “” {
log.Errorf(“执行命令失败:%s”, script)
return “”
}
return string(res)
}
// ExecCommand linux执行命令
func ExecCommandBash(script string) string {
result := exec.Command("/bin/bash", “-c”, script)
res, err := result.CombinedOutput()
if err != nil || string(res) == “” {
log.Errorf(“执行命令失败:%s”, script)
return “”
}
return string(res)
}