From a3e1ca6b2b61f914540b0536cee1dbb0770c10ec Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Wed, 14 Aug 2019 18:57:03 -0400 Subject: [PATCH] opening new sessions works now it's time to execute commands and working on tarring directories --- main.go | 9 +++------ ssh.go | 25 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 37077aa..5028384 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,6 @@ package main -import ( - "fmt" -) +import "fmt" func main() { //fmt.Println("Hello!") @@ -10,9 +8,8 @@ func main() { //var config Configuration config := initializeConfig("config.yaml") - //fmt.Println(config.Hosts) sshConn, sshConfig := initializeConnection(config) - //fmt.Println(sshConn.Hosts) clientConns := sshConn.dialConnection(sshConfig) - fmt.Println(clientConns) + clientSessions := sshConn.openSession(clientConns) + fmt.Println(clientSessions) } diff --git a/ssh.go b/ssh.go index de76cba..d67309d 100644 --- a/ssh.go +++ b/ssh.go @@ -21,6 +21,8 @@ type SSHConnection struct { type SSHCleints []*ssh.Client +type SSHSessions []*ssh.Session + func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) { var sshConn SSHConnection sshConn.Username = config.Username @@ -44,7 +46,7 @@ func (s SSHConnection) getHostKeys() ssh.HostKeyCallback { } func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { - ClientConn := SSHCleints{} + clientConn := SSHCleints{} for _, j := range s.Hosts { hostPort := fmt.Sprintf("%s:22", j) @@ -54,8 +56,25 @@ func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { fmt.Errorf("Can't connect", err) } - ClientConn = append(ClientConn, connection) + clientConn = append(clientConn, connection) } - return ClientConn + return clientConn +} + +func (s SSHConnection) openSession(client SSHCleints) SSHSessions { + // don't forget there's a return type here + clientSessions := SSHSessions{} + + for _, j := range client { + session, err := j.NewSession() + + if err != nil { + fmt.Errorf("Can't open session", err) + } + + clientSessions = append(clientSessions, session) + } + + return clientSessions }