/root/digital_guard/headscale routes enable --route 1 set-Advertised=true set-Enabled=true 这样执行成功
时间: 2024-01-11 07:02:21 浏览: 29
根据您提供的信息,这段代码可能存在以下几个问题:
1. 变量 `command` 在代码中没有定义或者赋值,导致 `in.WriteString(command)` 失败。
2. `cmd.Run()` 执行失败,可能是因为 `sh` 命令执行异常或者没有正确退出。您可以尝试打印 `cmd` 的输出来查看具体的错误信息。
3. `Exec` 执行失败,您需要打印 `errs` 变量来查看具体的错误信息。
您可以根据上述提示逐步排查问题,如果还有疑问,请提供更多的上下文信息。
相关问题
func Exec(command string) error { in := bytes.NewBuffer(nil) cmd := exec.Command("sh") cmd.Stdin = in in.WriteString(command) in.WriteString("exit\n") if err := cmd.Run(); err != nil { return err } return nil } func (h *Headscale) GetRoutesIp(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") var req GetRoutesIpReq jsonerr := json.NewDecoder(r.Body).Decode(&req) if jsonerr != nil { http.Error(w, "Failed to decode JSON", http.StatusBadRequest) return } response := GetRoutesIpResp{} cmd := exec.Command("/root/digital_guard/headscale", "routes", "list") routesCmdString := cmd.String() output, cmderr := cmd.CombinedOutput() if cmderr != nil { log.Info().Msgf("Failed to execute shell command: %s", cmderr.Error()) response.Messge = append(response.Messge, routesCmdString) } data := string(output) lines := strings.Split(data, "\n") for _, ipToFinds := range req.Ip { for _, line := range lines { if strings.Contains(line, ipToFinds) && strings.Contains(line, req.Name) { fields := strings.Fields(line) if len(fields) >= 3 { id := fields[0] // s := []string{"/root/digital_guard/headscale routes enable --route ", "1", " set-Advertised=true set-Enabled=true"} // s[1] = id // strCmdbyte := strings.Join(s, "") // errs := Exec(strCmdbyte) errs := Exec("/root/digital_guard/headscale routes enable --route " + id + " set-Advertised=true set-Enabled=true") if errs != nil { // log.Info().Msg(fmt.Sprintf("-----------err------shell命令----------: %s", strCmdbyte)) // response.Messge = append(response.Messge, strCmdbyte) } else { response.Code = 1 } } } } } respJSON, err := json.Marshal(response) if err != nil { http.Error(w, "Failed to encode JSON", http.StatusInternalServerError) return } w.WriteHeader(http.StatusOK) w.Write(respJSON) } 这样执行的linux命令失败
根据您提供的代码,我看到您尝试执行的 Linux 命令是 `/root/digital_guard/headscale routes enable --route <id> set-Advertised=true set-Enabled=true`,您说该命令执行失败,但是没有提供具体的错误信息。如果您能够提供该命令执行失败的具体错误信息,我可以更好地帮助您解决问题。
除此之外,您的代码中使用了 `exec.Command` 来执行命令,但是并没有设置 `cmd.Dir` 来指定命令执行的工作目录。如果命令需要在特定的工作目录下执行,您可以设置 `cmd.Dir` 来指定。例如:
```
cmd := exec.Command("ls")
cmd.Dir = "/home/user"
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
```
另外,执行 Shell 命令时最好使用 `bash` 而不是 `sh`,因为 `sh` 的行为可能会与您预期的不同。如果您的系统上没有 `bash`,可以尝试安装一下。
https://github.com/juanfont/headscale 代码文档
Headscale 是一个用 Go 语言编写的开源软件,它实现了 WireGuard VPN 服务器的自动配置和管理。它使用了 Yggdrasil 网络的概念,通过将分散的节点连接起来,创建一个跨越多个物理位置的虚拟网络,从而实现了更好的隐私和安全性。
Headscale 支持多种平台,包括 Linux、MacOS、Windows 和 FreeBSD,同时也支持多种架构,如 amd64、arm64 和 armhf。
Headscale 的主要特点包括:
- 自动配置 WireGuard VPN 服务器
- 支持多节点
- 使用 Yggdrasil 网络的概念,实现跨位置的虚拟网络
- 端到端加密
- 支持多平台和多架构
- 开源免费
如果你想了解 Headscale 的详细信息,可以查看它的官方文档:https://github.com/juanfont/headscale/blob/master/docs/README.md
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)