From 90e2e053d25feabd2dcf384dfc262a8bcdbe3ab8 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Sat, 5 Mar 2022 12:53:15 -0700 Subject: Initial Commit --- termshare/internal/client.go_ | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 termshare/internal/client.go_ (limited to 'termshare/internal/client.go_') 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()) + +} -- cgit v1.2.3