From d0869861ad02e6a6a1c1400d40d750a0da3fd0b5 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Wed, 14 Aug 2019 18:31:13 -0400 Subject: [PATCH] golang's ssh dialing is working --- .gitignore | 1 + main.go | 19 ++++++++++++++++++- {tar => remote}/main.go | 0 {tar => remote}/tar.go | 0 ssh.go | 32 ++++++++++++++++++++++++++++---- 5 files changed, 47 insertions(+), 5 deletions(-) rename {tar => remote}/main.go (100%) rename {tar => remote}/tar.go (100%) diff --git a/.gitignore b/.gitignore index 5b6b072..11a7e4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ config.yaml +.vscode/ \ No newline at end of file diff --git a/main.go b/main.go index 26414bf..1afea17 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,28 @@ package main +import ( + "fmt" + + "golang.org/x/crypto/ssh" +) + func main() { //fmt.Println("Hello!") //var config Configuration config := initializeConfig("config.yaml") + //fmt.Println(config.Hosts) sshConn, sshConfig := initializeConnection(config) - sshConn.DialConnection(sshConfig) + //fmt.Println(sshConn.Hosts) + clientConns := sshConn.dialConnection(sshConfig) + fmt.Println(clientConns) + + sshSuccess, err := ssh.Dial("tcp", "crash.local:22", sshConfig) + + if err != nil { + panic(err) + } + + fmt.Println(sshSuccess) } diff --git a/tar/main.go b/remote/main.go similarity index 100% rename from tar/main.go rename to remote/main.go diff --git a/tar/tar.go b/remote/tar.go similarity index 100% rename from tar/tar.go rename to remote/tar.go diff --git a/ssh.go b/ssh.go index 22e5939..de76cba 100644 --- a/ssh.go +++ b/ssh.go @@ -6,6 +6,12 @@ import ( "golang.org/x/crypto/ssh" ) +// modes := ssh.TerminalModes{ +// ssh.ECHO: 0, +// ssh.TTY_OP_ISPEED: 14400, +// ssh.TTY_OP_OSPEED: 14400, +// } + type SSHConnection struct { Username string Password string @@ -13,6 +19,8 @@ type SSHConnection struct { Hosts []string } +type SSHCleints []*ssh.Client + func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) { var sshConn SSHConnection sshConn.Username = config.Username @@ -25,13 +33,29 @@ func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfi Auth: []ssh.AuthMethod{ ssh.Password(sshConn.Password), }, + HostKeyCallback: sshConn.getHostKeys(), } return sshConn, sshConfig } -func (sshConn SSHConnection) DialConnection(sshConnec *ssh.ClientConfig) { - for _, j := range sshConn.Hosts { - fmt.Println(j) - } +func (s SSHConnection) getHostKeys() ssh.HostKeyCallback { + return ssh.InsecureIgnoreHostKey() +} + +func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { + ClientConn := SSHCleints{} + + for _, j := range s.Hosts { + hostPort := fmt.Sprintf("%s:22", j) + connection, err := ssh.Dial("tcp", hostPort, sshConfig) + + if err != nil { + fmt.Errorf("Can't connect", err) + } + + ClientConn = append(ClientConn, connection) + } + + return ClientConn }