opening new sessions works

now it's time to execute commands and working on tarring directories
This commit is contained in:
Wyatt J. Miller 2019-08-14 18:57:03 -04:00
parent 996aef3c1e
commit a3e1ca6b2b
2 changed files with 25 additions and 9 deletions

View File

@ -1,8 +1,6 @@
package main package main
import ( import "fmt"
"fmt"
)
func main() { func main() {
//fmt.Println("Hello!") //fmt.Println("Hello!")
@ -10,9 +8,8 @@ func main() {
//var config Configuration //var config Configuration
config := initializeConfig("config.yaml") config := initializeConfig("config.yaml")
//fmt.Println(config.Hosts)
sshConn, sshConfig := initializeConnection(config) sshConn, sshConfig := initializeConnection(config)
//fmt.Println(sshConn.Hosts)
clientConns := sshConn.dialConnection(sshConfig) clientConns := sshConn.dialConnection(sshConfig)
fmt.Println(clientConns) clientSessions := sshConn.openSession(clientConns)
fmt.Println(clientSessions)
} }

25
ssh.go
View File

@ -21,6 +21,8 @@ type SSHConnection struct {
type SSHCleints []*ssh.Client type SSHCleints []*ssh.Client
type SSHSessions []*ssh.Session
func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) { func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) {
var sshConn SSHConnection var sshConn SSHConnection
sshConn.Username = config.Username sshConn.Username = config.Username
@ -44,7 +46,7 @@ func (s SSHConnection) getHostKeys() ssh.HostKeyCallback {
} }
func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints {
ClientConn := SSHCleints{} clientConn := SSHCleints{}
for _, j := range s.Hosts { for _, j := range s.Hosts {
hostPort := fmt.Sprintf("%s:22", j) hostPort := fmt.Sprintf("%s:22", j)
@ -54,8 +56,25 @@ func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints {
fmt.Errorf("Can't connect", err) 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
} }