added sftp feature

i have the program reaching out to the remote machine and creating
the archive. however, it doesn't acutally grab the file and the
program creates an empty archive.
This commit is contained in:
Wyatt J. Miller 2019-08-18 18:12:47 -04:00
parent d4c5d1d596
commit 4595ca4a5e
6 changed files with 98 additions and 15 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
config.yaml config.yaml
.vscode/ .vscode/
vendor/

25
Gopkg.lock generated
View File

@ -1,6 +1,30 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
digest = "1:9cedee824c21326bd26950bd9e1ffe9dc4e7ca03dc8634d0e6f954ee6a383172"
name = "github.com/kr/fs"
packages = ["."]
pruneopts = "UT"
revision = "1455def202f6e05b95cc7bfc7e8ae67ae5141eba"
version = "v0.1.0"
[[projects]]
digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b"
name = "github.com/pkg/errors"
packages = ["."]
pruneopts = "UT"
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
version = "v0.8.1"
[[projects]]
digest = "1:6c187bc42f50a341b0dd25da7f5bcf84b4bbffa7193b92f3c983c1a9803a447a"
name = "github.com/pkg/sftp"
packages = ["."]
pruneopts = "UT"
revision = "a713b07e6d90e1831d7fefcb69f1310edb3783ae"
version = "v1.10.0"
[[projects]] [[projects]]
branch = "master" branch = "master"
digest = "1:ffebede762b00a7ffe84ef08efc4f4e87823b5298b4fd477313579f553db0c6a" digest = "1:ffebede762b00a7ffe84ef08efc4f4e87823b5298b4fd477313579f553db0c6a"
@ -37,6 +61,7 @@
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
input-imports = [ input-imports = [
"github.com/pkg/sftp",
"golang.org/x/crypto/ssh", "golang.org/x/crypto/ssh",
"gopkg.in/yaml.v2", "gopkg.in/yaml.v2",
] ]

View File

@ -9,7 +9,7 @@ import (
type Configuration struct { type Configuration struct {
Username string Username string
Password string Password string
Port string Port int
Hosts []string Hosts []string
} }

View File

@ -6,7 +6,7 @@ func main() {
config := initializeConfig("config.yaml") config := initializeConfig("config.yaml")
sshConn, sshConfig := initializeConnection(config) sshConn, sshConfig := initializeConnection(config)
clientConns := sshConn.dialConnection(sshConfig) clientConns := sshConn.dialConnection(sshConfig)
clientSessions := sshConn.openSession(clientConns) _ = sshConn.openSession(clientConns)
success := sshConn.executeTarFile(clientSessions) success := sshConn.executeSFTP(clientConns)
fmt.Println(success) fmt.Println(success)
} }

50
sftp.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"fmt"
"os"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
var (
dstPath string = "/root/"
srcPath string = "/var/log/"
filename string = "dmesg"
)
func getFile(client *ssh.Client) {
t := time.Now()
sftp, err := sftp.NewClient(client)
if err != nil {
fmt.Errorf("FUCK")
}
defer sftp.Close()
srcFile, err := sftp.Open(srcPath + filename)
if err != nil {
fmt.Errorf("FUCK")
}
defer srcFile.Close()
timeResult := timeToString(t)
dstFile, err := os.Create(dstPath + filename + timeResult)
if err != nil {
fmt.Errorf("FUCK")
}
defer dstFile.Close()
srcFile.WriteTo(dstFile)
}
func timeToString(currentTime time.Time) string {
return currentTime.String()
}

29
ssh.go
View File

@ -15,11 +15,11 @@ import (
type SSHConnection struct { type SSHConnection struct {
Username string Username string
Password string Password string
Port string Port int
Hosts []string Hosts []string
} }
type SSHCleints []*ssh.Client type SSHClients []*ssh.Client
// TODO: I've got two slices of the same type. To take one slice out, some refactoring is needed // TODO: I've got two slices of the same type. To take one slice out, some refactoring is needed
type SSHSessions []*ssh.Session type SSHSessions []*ssh.Session
@ -28,6 +28,8 @@ type SSHTarFile []*ssh.Session
type SSHPush []*ssh.Client type SSHPush []*ssh.Client
type SSHSFTP []string
type SSHSuccess []error type SSHSuccess []error
func initializeConnection(config Configuration) (*SSHConnection, *ssh.ClientConfig) { func initializeConnection(config Configuration) (*SSHConnection, *ssh.ClientConfig) {
@ -53,8 +55,8 @@ func (s SSHConnection) getHostKeys() ssh.HostKeyCallback {
return ssh.InsecureIgnoreHostKey() return ssh.InsecureIgnoreHostKey()
} }
func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHClients {
clientConn := SSHCleints{} clientConn := SSHClients{}
for _, j := range s.Hosts { for _, j := range s.Hosts {
hostPort := fmt.Sprintf("%s:22", j) hostPort := fmt.Sprintf("%s:22", j)
@ -70,7 +72,7 @@ func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints {
return clientConn return clientConn
} }
func (s SSHConnection) openSession(client SSHCleints) SSHSessions { func (s SSHConnection) openSession(client SSHClients) SSHSessions {
clientSessions := SSHSessions{} clientSessions := SSHSessions{}
for _, j := range client { for _, j := range client {
@ -86,20 +88,25 @@ func (s SSHConnection) openSession(client SSHCleints) SSHSessions {
return clientSessions return clientSessions
} }
func (s SSHConnection) executeTarFile(execute SSHSessions) SSHSuccess { func (s SSHConnection) executeSFTP(execute SSHClients) SSHSFTP {
// execute order 66 lol // execute order 66 lol
success := SSHSuccess{} sftp := SSHSFTP{}
for _, j := range execute { for _, j := range execute {
// TODO: this is just a placeholder, change to the actual tarring executable // TODO: this is just a placeholder, change to the actual tarring executable
err := j.Run("echo 'hello world' > test.txt") getFile(j)
err := gzipit("/root/"+filename, ".")
if err != nil { if err != nil {
fmt.Errorf("Can't execute program", err) fmt.Errorf("Cannot gzip file(s)", err)
} }
success = append(success, err)
} }
return success return sftp
}
func (s SSHConnection) gzipItUp() {
// TODO: placeholder function??
} }