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

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
config.yaml
.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'.
[[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",
]

View File

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

View File

@ -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)
}

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 {
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??
}