golang: Execute shell commands on remote server

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

golang: Execute shell commands on remote server

问题

我有50多台Linux机器(RHEL)。我想从一台运行在中央机器上的Go脚本上运行一些命令在这些机器上。我已经在中央机器上设置了无密码的SSH身份验证到所有远程机器。虽然我也可以接受非SSH解决方案,但更倾向于安全的解决方案。

被运行的命令会随时间而变化。我还希望在运行在中央机器上的脚本中处理远程机器上运行的命令的输出和返回代码。

我只找到了这个ssh包,它只支持密码身份验证方法,这还不够好。

还有其他选项吗?

英文:

I have 50 something Linux machines (RHEL). I want to run some commands on these machines from a go script running on a central machine. I have setup password-less ssh authentication to all of them from the central machine to all the remote machines. Though I'm open to non-ssh solutions too, though something secure is preferred.

The commands being run would change over time. I would also want to process the output and return codes of commands being run on the remote machines in my script running on central machine.

I only found this ssh package, which supports only password authentication method, which is not good enough.

Any other options?

答案1

得分: 4

你可以使用这个包 https://github.com/hypersleep/easyssh

例如,你可以远程调用 ps ax

package main

import (
    "fmt"
    "github.com/hypersleep/easyssh"    
)

func main() {
    ssh := &easyssh.MakeConfig {
        User: "core",
        Server: "core",
        Key: "/.ssh/id_rsa",
        Port: "22",
    }

    response, err := ssh.Run("ps ax")
    if err != nil {
        panic("无法运行远程命令:" + err.Error())
    } else {
        fmt.Println(response)
    }
}
英文:

You just can use this package https://github.com/hypersleep/easyssh

For example you can call ps ax remotely:

package main

import (
    "fmt"
    "github.com/hypersleep/easyssh"    
)

func main() {
    ssh := &easyssh.MakeConfig {
        User: "core",
        Server: "core",
        Key: "/.ssh/id_rsa",
        Port: "22",
    }

    response, err := ssh.Run("ps ax")
    if err != nil {
        panic("Can't run remote command: " + err.Error())
    } else {
        fmt.Println(response)
    }
}

答案2

得分: 3

你可以使用go.crypto/ssh包中的PublicKeys来使用公钥。

repo中有一个详细的示例,大约在第114行附近。

另一个使用ssh代理的示例可以在这里找到。

英文:

You can use public keys with the go.crypto/ssh package using PublicKeys.

There's a detailed example in the repo around line 114.

Another example using ssh agent @ https://github.com/davecheney/socksie/blob/master/main.go.

huangapple
  • 本文由 发表于 2014年10月18日 07:28:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/26434759.html
匿名

发表评论

匿名网友

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

确定