doubao7287
2014-06-12 09:08
浏览 135

使用多个参数运行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条)

相关推荐 更多相似问题