通过shell连接ssh?

huangapple go评论86阅读模式
英文:

Connecting with ssh through the shell?

问题

我正在使用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()
if err2 != nil {
    fmt.Print("Disconnected")
}

在这里,c.Address等同于"person@192.168.1.1"(实际上不使用该IP)。但是当我运行这段代码时,我得到以下错误:

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

我可以正常使用终端连接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!

答案1

得分: 2

使用golang.org/x/crypto/ssh包。

包文档。

一个很好的入门教程。

英文:

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

Package documentation.

Nice tutorial to get you started.

答案2

得分: 0

如果你有

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

它是可以工作的。根据上面的注释,如果你有

cmd := exec.Command("ssh", string("root@192.168.1.1 ")) - 注意多了一个空格

那么你将会得到你所描述的错误。

英文:

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.

huangapple
  • 本文由 发表于 2015年4月18日 04:27:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/29708760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定