I am trying to replicate this command
ssh user@host /bin/bash < dummy.sh
using golang's crypto library.
I am opening a connection the remote host using ssh-agent. Then I am trying to run the command. Below is the snippet.
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)
}
This seems to give out empty output. Anyone have an idea how this can be achieved?