jisaw
jisaw

Reputation: 200

Connecting with ssh through the shell?

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 "[email protected]" 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!

Upvotes: 5

Views: 475

Answers (2)

openwonk
openwonk

Reputation: 15567

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

Package documentation.

Nice tutorial to get you started.

Upvotes: 2

LenW
LenW

Reputation: 3096

If you have

cmd := exec.Command("ssh", string("[email protected]"))

it works. As per the comment above if you have

cmd := exec.Command("ssh", string("[email protected] ")) - notice the extra space

then you will get the error you you described.

Upvotes: 0

Related Questions