package main

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"regexp"
	"strings"

	"golang.org/x/crypto/ssh"
)

func Connect(user, pass, host string, cmd string) bytes.Buffer {
	cipher := ssh.Config{
		Ciphers: []string{"aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
	}
	config := &ssh.ClientConfig{
		User: user,
		Auth: []ssh.AuthMethod{
			ssh.Password(pass),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Config:          cipher,
	}
	conn, err := ssh.Dial("tcp", host, config)
	// time.Sleep(1)
	if err != nil {
		log.Fatal("Failed to dial: ", err)
	}
	sess, err := conn.NewSession()
	if err != nil {
		log.Fatal("Failed to create session: ", err)
	}
	stdin, err := sess.StdinPipe()
	if err != nil {
		log.Fatal("Failed to create session: ", err)
	}
	var bout bytes.Buffer
	var berr bytes.Buffer
	sess.Stdout = &bout
	sess.Stderr = &berr
	sess.Shell()
	fmt.Fprintf(stdin, "%s\n", "terminal length 0")
	fmt.Fprintf(stdin, "%s\n", cmd)
	fmt.Fprintf(stdin, "\nexit\n")
	fmt.Fprintf(stdin, "exit\n")
	sess.Wait()
	sess.Close()
	// scanner := bufio.NewScanner(&bout)
	// for scanner.Scan() {
	// fmt.Println(scanner.Text())
	// }
	// fmt.Println(bout.String())
	return bout
}
func main() {

	result1 := Connect("rancid", "JDACy6wK*yW%meQ", os.Args[1], os.Args[2])
	scanner := bufio.NewScanner(&result1)
	for scanner.Scan() {
		fmt.Println("Text: " + scanner.Text())
	}
}
func IntStringParser(str string) []string {

	re := regexp.MustCompile(`\s{1,}`)
	return strings.Split(re.ReplaceAllString(str, ","), ",")
}
