2019-08-13 09:48:47 -05:00
|
|
|
package main
|
|
|
|
|
2019-08-13 10:43:40 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-25 23:24:15 -05:00
|
|
|
"os"
|
2019-08-13 10:43:40 -05:00
|
|
|
|
|
|
|
"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
|
2019-08-18 17:12:47 -05:00
|
|
|
Port int
|
2019-08-13 10:10:18 -05:00
|
|
|
Hosts []string
|
2019-08-13 09:48:47 -05:00
|
|
|
}
|
2019-08-13 10:43:40 -05:00
|
|
|
|
2019-08-18 17:12:47 -05:00
|
|
|
type SSHClients []*ssh.Client
|
2019-08-14 17:31:13 -05:00
|
|
|
|
2019-08-16 18:45:01 -05:00
|
|
|
// TODO: I've got two slices of the same type. To take one slice out, some refactoring is needed
|
2019-08-14 17:57:03 -05:00
|
|
|
type SSHSessions []*ssh.Session
|
|
|
|
|
2019-08-16 18:45:01 -05:00
|
|
|
type SSHTarFile []*ssh.Session
|
|
|
|
|
|
|
|
type SSHPush []*ssh.Client
|
|
|
|
|
2019-08-18 17:12:47 -05:00
|
|
|
type SSHSFTP []string
|
|
|
|
|
2019-08-16 18:45:01 -05:00
|
|
|
type SSHSuccess []error
|
|
|
|
|
2019-08-16 22:21:26 -05:00
|
|
|
func initializeConnection(config Configuration) (*SSHConnection, *ssh.ClientConfig) {
|
|
|
|
sshConn := &SSHConnection{
|
2019-08-16 21:00:36 -05:00
|
|
|
Username: config.Username,
|
|
|
|
Password: config.Password,
|
|
|
|
Port: config.Port,
|
|
|
|
Hosts: config.Hosts,
|
|
|
|
}
|
2019-08-13 10:43:40 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-08-18 17:12:47 -05:00
|
|
|
func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHClients {
|
|
|
|
clientConn := SSHClients{}
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-18 17:12:47 -05:00
|
|
|
func (s SSHConnection) openSession(client SSHClients) SSHSessions {
|
2019-08-14 17:57:03 -05:00
|
|
|
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
|
|
|
}
|
2019-08-16 18:45:01 -05:00
|
|
|
|
2019-08-18 17:12:47 -05:00
|
|
|
func (s SSHConnection) executeSFTP(execute SSHClients) SSHSFTP {
|
2019-08-16 18:45:01 -05:00
|
|
|
// execute order 66 lol
|
2019-08-18 17:12:47 -05:00
|
|
|
sftp := SSHSFTP{}
|
2019-09-25 23:24:15 -05:00
|
|
|
homedir := os.Getenv("HOME")
|
2019-08-16 18:45:01 -05:00
|
|
|
|
|
|
|
for _, j := range execute {
|
2019-08-18 10:17:06 -05:00
|
|
|
// TODO: this is just a placeholder, change to the actual tarring executable
|
2019-08-18 17:12:47 -05:00
|
|
|
getFile(j)
|
|
|
|
|
2019-09-25 23:24:15 -05:00
|
|
|
err := gzipit(homedir+filename, ".")
|
2019-08-16 18:45:01 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2019-08-18 17:12:47 -05:00
|
|
|
fmt.Errorf("Cannot gzip file(s)", err)
|
2019-08-16 18:45:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-18 17:12:47 -05:00
|
|
|
return sftp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SSHConnection) gzipItUp() {
|
|
|
|
// TODO: placeholder function??
|
2019-08-16 18:45:01 -05:00
|
|
|
}
|