diff options
author | Cody Hiar <cody@hiar.ca> | 2022-03-05 12:53:15 -0700 |
---|---|---|
committer | Cody Hiar <cody@hiar.ca> | 2022-03-05 12:53:15 -0700 |
commit | 90e2e053d25feabd2dcf384dfc262a8bcdbe3ab8 (patch) | |
tree | 8e2799822b4de29014b86ca708123c1dfc8f45f5 /termshare/internal/client.go_ |
Diffstat (limited to 'termshare/internal/client.go_')
-rw-r--r-- | termshare/internal/client.go_ | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/termshare/internal/client.go_ b/termshare/internal/client.go_ new file mode 100644 index 0000000..1845a15 --- /dev/null +++ b/termshare/internal/client.go_ @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "golang.org/x/crypto/ssh" + "log" + "os" + // Uncomment to store output in variable + //"bytes" +) + +func main() { + + username := "root" + password := "" + hostname := "127.0.0.1" + port := "2222" + + // SSH client config + config := &ssh.ClientConfig{ + User: username, + Auth: []ssh.AuthMethod{ + ssh.Password(password), + }, + // Non-production only + HostKeyCallback: ssh.InsecureIgnoreHostKey(), + } + + // Connect to host + client, err := ssh.Dial("tcp", hostname+":"+port, config) + if err != nil { + log.Fatal(err) + } + defer client.Close() + + // Create sesssion + sess, err := client.NewSession() + if err != nil { + log.Fatal("Failed to create session: ", err) + } + defer sess.Close() + + // StdinPipe for commands + stdin, err := sess.StdinPipe() + if err != nil { + log.Fatal(err) + } + + // Uncomment to store output in variable + //var b bytes.Buffer + //sess.Stdout = &b + //sess.Stderr = &b + + // Enable system stdout + // Comment these if you uncomment to store in variable + sess.Stdout = os.Stdout + sess.Stderr = os.Stderr + + // Start remote shell + err = sess.Shell() + if err != nil { + log.Fatal(err) + } + + // send the commands + commands := []string{ + "whoami", + "exit", + } + for _, cmd := range commands { + _, err = fmt.Fprintf(stdin, "%s\n", cmd) + if err != nil { + log.Fatal(err) + } + } + + // Wait for sess to finish + err = sess.Wait() + if err != nil { + log.Fatal(err) + } + + // Uncomment to store in variable + //fmt.Println(b.String()) + +} |