2019-08-13 09:48:47 -05:00
|
|
|
package main
|
|
|
|
|
2019-08-13 10:43:40 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2019-08-14 17:31:13 -05:00
|
|
|
// modes := ssh.TerminalModes{
|
|
|
|
// ssh.ECHO: 0,
|
|
|
|
// ssh.TTY_OP_ISPEED: 14400,
|
|
|
|
// ssh.TTY_OP_OSPEED: 14400,
|
|
|
|
// }
|
|
|
|
|
2019-08-13 10:43:40 -05:00
|
|
|
type SSHConnection struct {
|
2019-08-13 10:10:18 -05:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Port string
|
|
|
|
Hosts []string
|
2019-08-13 09:48:47 -05:00
|
|
|
}
|
2019-08-13 10:43:40 -05:00
|
|
|
|
2019-08-14 17:31:13 -05:00
|
|
|
type SSHCleints []*ssh.Client
|
|
|
|
|
2019-08-14 17:57:03 -05:00
|
|
|
type SSHSessions []*ssh.Session
|
|
|
|
|
2019-08-13 10:43:40 -05:00
|
|
|
func initializeConnection(config Configuration) (SSHConnection, *ssh.ClientConfig) {
|
|
|
|
var sshConn SSHConnection
|
|
|
|
sshConn.Username = config.Username
|
|
|
|
sshConn.Password = config.Password
|
|
|
|
sshConn.Port = config.Port
|
|
|
|
sshConn.Hosts = config.Hosts
|
|
|
|
|
|
|
|
sshConfig := &ssh.ClientConfig{
|
|
|
|
User: sshConn.Username,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.Password(sshConn.Password),
|
|
|
|
},
|
2019-08-14 17:31:13 -05:00
|
|
|
HostKeyCallback: sshConn.getHostKeys(),
|
2019-08-13 10:43:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return sshConn, sshConfig
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:31:13 -05:00
|
|
|
func (s SSHConnection) getHostKeys() ssh.HostKeyCallback {
|
|
|
|
return ssh.InsecureIgnoreHostKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints {
|
2019-08-14 17:57:03 -05:00
|
|
|
clientConn := SSHCleints{}
|
2019-08-14 17:31:13 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-14 17:57:03 -05:00
|
|
|
clientConn = append(clientConn, connection)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-08-13 10:43:40 -05:00
|
|
|
}
|
2019-08-14 17:31:13 -05:00
|
|
|
|
2019-08-14 17:57:03 -05:00
|
|
|
return clientSessions
|
2019-08-13 10:43:40 -05:00
|
|
|
}
|