dongren4147
2015-04-17 20:27
浏览 40
已采纳

通过外壳与SSH连接?

I am working with Go trying to automate keeping track of all of my ssh connections. I am having some issues running the command from Go. Here is my code:

cmd := exec.Command("ssh", string(c.Address))
        cmd.Stderr = os.Stderr
        cmd.Stdin = os.Stdin
        cmd.Stdout = os.Stdout
        err2 := cmd.Run()
        if err2 != nil {
            fmt.Print("Disconnected")
        }

c.Address is equivalent to "person@192.168.1.1" not using that ip obviously but when I run this I get the following error.

ssh: Could not resolve hostname 192.168.1.1
: nodename nor servname provided, or not known

I can connect just fine using ssh from my terminal.

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在与Go一起尝试自动跟踪所有ssh连接。 我在从Go运行命令时遇到一些问题。 这是我的代码:

  cmd:= exec.Command(“ ssh”,string(c.Address))
 cmd.Stderr = os.Stderr 
 cmd。  Stdin = os.Stdin 
 cmd.Stdout = os.Stdout 
 err2:= cmd.Run()
如果err2!= nil {
 fmt.Print(“ Disconnected”)
} 
   
 
 

c.Address显然不使用该ip相当于“ person@192.168.1.1”,但是当我运行此地址时,出现以下错误。

  ssh:无法解析主机名192.168.1.1 
:提供的节点名或服务名,或者未知
   
 
 

我可以从终端使用ssh进行正常连接。

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjunjie0196 2015-12-18 07:08
    已采纳

    Use the golang.org/x/crypto/ssh package.

    Package documentation.

    Nice tutorial to get you started.

    点赞 评论
  • duanbo2048 2015-06-26 07:29

    If you have

    cmd := exec.Command("ssh", string("root@192.168.1.1"))

    it works. As per the comment above if you have

    cmd := exec.Command("ssh", string("root@192.168.1.1 ")) - notice the extra space

    then you will get the error you you described.

    点赞 评论

相关推荐 更多相似问题