我正在尝试复制此命令
ssh user@host /bin/bash < dummy.sh
使用golang的加密library。
我正在使用ssh-agent打开远程主机的连接。然后我试图运行命令。以下是摘录。
func executeSSHCmdToBuffer(cmd, hostname string, config *ssh.ClientConfig) (string, error) {
conn, err := getSSHConnection(hostname, sshPort, config)
if err != nil {
return "", err
}
session, err := conn.NewSession()
if err != nil {
return "", err
}
defer session.Close()
var stdoutBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Run(cmd)
output := stdoutBuf.String()
log.Printf("SSH Output %s", output)
return output, nil
}
func ExecuteSSHCommand(cmd, hostname string) (string, error) {
sshAuth, err := sshAgent()
log.Printf("Executing %s on %s", cmd, hostname)
if err != nil {
log.Fatalf("SSH Agent Failed to init: %s", err)
}
sshConfig := &ssh.ClientConfig{
User: sshUser,
Auth: []ssh.AuthMethod{
sshAuth,
},
Timeout: time.Duration(1 * time.Minute),
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return executeSSHCmdToBuffer(cmd, hostname, sshConfig)
}
func main() {
out, err := ExecuteSSHCommand("/bin/sh < dummy.sh", "test.example.com")
if err != nil {
log.Print("Cannot execute the command")
log.Print(err)
}
log.Print(out)
}
这似乎给出了空输出。任何人都知道如何实现这一目标?