英文:
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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论