doubao7287 2014-06-12 09:08
浏览 137
已采纳

使用多个参数运行Linux命令

After some digging I can run Linux commands from go like this:

func main() {
    lsCmd := exec.Command("ls")
    lsOut, err := lsCmd.Output()
    if err != nil {
        panic(err)
    }
    fmt.Println(">ls")
    fmt.Println(string(lsOut))
}

What I want to do is, running following command in remote machine:

ssh -p $someport $someuser@$someip 'ls'

I can do this successfully from my terminal, but when I try to run it within Go, I get following error:

panic: exec: "ssh -p $someport $someuser@$someip 'ls'": executable file not found in $PATH

Update: I updated the question for convenience.

  • 写回答

2条回答 默认 最新

  • duankuai6991 2014-06-12 09:16
    关注

    As per the doc about the exec package, program name and arguments are differents parameters of the Command method. You should do something like that :

    exec.Command("ssh", "-p port", "user@ip", "'ls'")
    

    If you need something more elaborate, you could also look at the go.crypto/ssh package.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突