MCPcopy
hub / github.com/acepanel/panel / Execf

Function Execf

pkg/shell/exec.go:56–76  ·  view source on GitHub ↗

Execf 安全执行 shell 命令

(shell string, args ...any)

Source from the content-addressed store, hash-verified

54
55// Execf 安全执行 shell 命令
56func Execf(shell string, args ...any) (string, error) {
57 if !preCheckArg(args) {
58 return "", errors.New("command contains illegal characters")
59 }
60 if len(args) > 0 {
61 shell = fmt.Sprintf(shell, args...)
62 }
63
64 _ = os.Setenv("LC_ALL", "C")
65 cmd := exec.Command("bash", "-c", shell)
66
67 var stdout, stderr bytes.Buffer
68 cmd.Stdout = &stdout
69 cmd.Stderr = &stderr
70
71 if err := cmd.Run(); err != nil {
72 return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %w, stderr: %s", shell, err, strings.TrimSpace(stderr.String()))
73 }
74
75 return strings.TrimSpace(stdout.String()), nil
76}
77
78// ExecfAsync 异步执行 shell 命令
79func ExecfAsync(shell string, args ...any) error {

Callers 15

isNetplanAvailableFunction · 0.92
getDNSFromNetworkManagerFunction · 0.92
setDNSWithNetworkManagerFunction · 0.92
getActiveNMConnectionsFunction · 0.92
setDNSWithNetplanFunction · 0.92
detectActiveInterfaceFunction · 0.92
GetPortProcessFunction · 0.92
GetServiceInfoFunction · 0.92
StatusFunction · 0.92
IsEnabledFunction · 0.92
LogClearFunction · 0.92
LinkCLIBinariesFunction · 0.92

Calls 4

preCheckArgFunction · 0.85
ErrorfMethod · 0.80
RunMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected