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