From 4595ca4a5e6b28a3565af949231d4012e1424c63 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sun, 18 Aug 2019 18:12:47 -0400 Subject: [PATCH] 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. --- .gitignore | 3 ++- Gopkg.lock | 25 +++++++++++++++++++++++++ config.go | 2 +- main.go | 4 ++-- sftp.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ ssh.go | 29 ++++++++++++++++++----------- 6 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 sftp.go diff --git a/.gitignore b/.gitignore index 11a7e4c..9133085 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.yaml -.vscode/ \ No newline at end of file +.vscode/ +vendor/ \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock index 266d82f..0ad1417 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1,6 +1,30 @@ # 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]] branch = "master" digest = "1:ffebede762b00a7ffe84ef08efc4f4e87823b5298b4fd477313579f553db0c6a" @@ -37,6 +61,7 @@ analyzer-name = "dep" analyzer-version = 1 input-imports = [ + "github.com/pkg/sftp", "golang.org/x/crypto/ssh", "gopkg.in/yaml.v2", ] diff --git a/config.go b/config.go index 9077636..8cb380d 100644 --- a/config.go +++ b/config.go @@ -9,7 +9,7 @@ import ( type Configuration struct { Username string Password string - Port string + Port int Hosts []string } diff --git a/main.go b/main.go index e8350e3..294d103 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ func main() { config := initializeConfig("config.yaml") sshConn, sshConfig := initializeConnection(config) clientConns := sshConn.dialConnection(sshConfig) - clientSessions := sshConn.openSession(clientConns) - success := sshConn.executeTarFile(clientSessions) + _ = sshConn.openSession(clientConns) + success := sshConn.executeSFTP(clientConns) fmt.Println(success) } diff --git a/sftp.go b/sftp.go new file mode 100644 index 0000000..26b1abe --- /dev/null +++ b/sftp.go @@ -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() +} diff --git a/ssh.go b/ssh.go index aca09ae..d640448 100644 --- a/ssh.go +++ b/ssh.go @@ -15,11 +15,11 @@ import ( type SSHConnection struct { Username string Password string - Port string + Port int 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 type SSHSessions []*ssh.Session @@ -28,6 +28,8 @@ type SSHTarFile []*ssh.Session type SSHPush []*ssh.Client +type SSHSFTP []string + type SSHSuccess []error func initializeConnection(config Configuration) (*SSHConnection, *ssh.ClientConfig) { @@ -53,8 +55,8 @@ func (s SSHConnection) getHostKeys() ssh.HostKeyCallback { return ssh.InsecureIgnoreHostKey() } -func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { - clientConn := SSHCleints{} +func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHClients { + clientConn := SSHClients{} for _, j := range s.Hosts { hostPort := fmt.Sprintf("%s:22", j) @@ -70,7 +72,7 @@ func (s SSHConnection) dialConnection(sshConfig *ssh.ClientConfig) SSHCleints { return clientConn } -func (s SSHConnection) openSession(client SSHCleints) SSHSessions { +func (s SSHConnection) openSession(client SSHClients) SSHSessions { clientSessions := SSHSessions{} for _, j := range client { @@ -86,20 +88,25 @@ func (s SSHConnection) openSession(client SSHCleints) SSHSessions { return clientSessions } -func (s SSHConnection) executeTarFile(execute SSHSessions) SSHSuccess { +func (s SSHConnection) executeSFTP(execute SSHClients) SSHSFTP { // execute order 66 lol - success := SSHSuccess{} + sftp := SSHSFTP{} for _, j := range execute { // 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 { - 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?? }