Esempio n. 1
-1
func runPipe(user, pass, host, cmd string) {
	fmt.Println("runPipe...")
	sshConfig := &ssh.ClientConfig{
		User: user,
		Auth: []ssh.AuthMethod{ssh.Password(pass)},
	}
	sshConfig.SetDefaults()

	client, err := ssh.Dial("tcp", host, sshConfig)
	if err != nil {
		// errors.Wrap(err, err.Error())
		fmt.Println(err.Error())
	}

	var session *ssh.Session

	session, err = client.NewSession()
	if err != nil {
		// errors.Wrap(err, err.Error())
		fmt.Println(err.Error())
	}
	defer session.Close()

	// modes := ssh.TerminalModes{
	// 	ssh.ECHO:          0,     // disable echoing
	// 	ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
	// 	ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
	// }

	// if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
	// 	log.Fatal(err)
	// }

	w, err := session.StdinPipe()
	if err != nil {
		panic(err)
	}
	//    r, err := session.StdoutPipe()
	//    if err != nil {
	//        panic(err)
	//    }

	var stdoutBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	// session.Run(cmd)
	err = session.Shell()
	w.Write([]byte(fmt.Sprintf("%s\n", "configure")))
	w.Write([]byte(fmt.Sprintf("%s %s\n", "set interfaces ethernet eth4 description", cmd)))
	w.Write([]byte(fmt.Sprintf("%s\n", "commit")))
	w.Write([]byte(fmt.Sprintf("%s\n", "save")))
	w.Write([]byte(fmt.Sprintf("%s\n", "exit")))
	w.Write([]byte(fmt.Sprintf("%s\n", "exit")))

	fmt.Println(stdoutBuf.String())
}
				err = session.Run("ls")
				Expect(err).NotTo(HaveOccurred())

				stdin.Close()
			})

			It("terminates the shell when the stdin closes", func() {
				waitCh := make(chan error, 1)
				waitStub := runner.WaitStub
				runner.WaitStub = func(command *exec.Cmd) error {
					err := waitStub(command)
					waitCh <- err
					return err
				}

				err := session.Shell()
				Expect(err).NotTo(HaveOccurred())

				err = client.Conn.Close()
				client = nil
				Expect(err).NotTo(HaveOccurred())

				session.Wait()

				Eventually(waitCh, 3).Should(Receive(MatchError("signal: hangup")))
			})

			It("should set the terminal type", func() {
				result, err := session.Output(`/bin/echo -n "$TERM"`)
				Expect(err).NotTo(HaveOccurred())