コード例 #1
0
ファイル: sshutils.go プロジェクト: edillmann/go-zfs
func prepareCommand(session *ssh.Session, cmd *command) error {
	for _, env := range cmd.Env {
		variable := strings.Split(env, "=")
		if len(variable) != 2 {
			continue
		}

		if err := session.Setenv(variable[0], variable[1]); err != nil {
			return err
		}
	}

	if cmd.Stdout == nil {
		session.Stdout = &cmd.stdout
	} else {
		session.Stdout = cmd.Stdout
	}

	if cmd.Stdin != nil {
		session.Stdin = cmd.Stdin

	}
	if cmd.Stderr == nil {
		session.Stderr = &cmd.stderr
	} else {
		session.Stderr = cmd.Stderr
	}
	return nil
}
コード例 #2
0
ファイル: ssh.go プロジェクト: cretz/systrument
func (s *sshConn) runSudoCommand(sess *ssh.Session, stdin io.Writer, cmd string) error {
	// Wrap the output
	if s.DebugEnabled() {
		debugOutWriter := util.NewDebugLogWriter("SSH OUT:", s.Context)
		if sess.Stdout != nil {
			sess.Stdout = io.MultiWriter(sess.Stdout, debugOutWriter)
		} else {
			sess.Stdout = debugOutWriter
		}
		debugErrWriter := util.NewDebugLogWriter("SSH ERR:", s.Context)
		if sess.Stderr != nil {
			sess.Stderr = io.MultiWriter(sess.Stderr, debugErrWriter)
		} else {
			sess.Stderr = debugErrWriter
		}
	}
	// We need a checker to enter the password
	passwordTyper := util.NewExpectListener(stdin, shell.SudoPasswordPromptMatch, s.server.SSH.Pass+"\n")

	if sess.Stdout == nil {
		sess.Stdout = passwordTyper
	} else {
		sess.Stdout = io.MultiWriter(sess.Stdout, passwordTyper)
	}
	if sess.Stderr == nil {
		sess.Stderr = passwordTyper
	} else {
		sess.Stderr = io.MultiWriter(sess.Stderr, passwordTyper)
	}
	if err := sess.Run("sudo -S " + cmd); err != nil {
		return fmt.Errorf("Error running command %v: %v", cmd, err)
	}
	return nil
}
コード例 #3
0
ファイル: loom.go プロジェクト: zyuyou/loom
func (config *Config) executeCommand(s *ssh.Session, cmd string, sudo bool) ([]byte, error) {
	if s.Stdout != nil {
		return nil, errors.New("ssh: Stdout already set")
	}
	if s.Stderr != nil {
		return nil, errors.New("ssh: Stderr already set")
	}

	b := newSingleWriterReader()
	s.Stdout = &b
	s.Stderr = &b
	done := make(chan bool)

	if sudo {
		stdInWriter, err := s.StdinPipe()
		if err != nil {
			if config.AbortOnError == true {
				log.Fatalf("%s", err)
			}
			return nil, err
		}

		go config.injectSudoPasswordIfNecessary(done, &b, stdInWriter)
	}

	err := s.Run(cmd)
	close(done)
	return b.Bytes(), err
}
コード例 #4
0
ファイル: ssh.go プロジェクト: treescale/treescale
func (ssc *SSHConfig) Exec(cmd string, stdout, stderr io.Writer, input chan string) (err tree_lib.TreeError) {
	var (
		session       *ssh.Session
		stdin         io.WriteCloser
		command_ended bool
	)
	err.From = tree_lib.FROM_SSH_EXEC
	session, err.Err = ssc.conn.NewSession()
	if !err.IsNull() {
		return
	}
	defer session.Close()

	session.Stdout = stdout
	session.Stderr = stderr

	stdin, err.Err = session.StdinPipe()
	if !err.IsNull() {
		return
	}

	err.Err = session.Start(cmd)
	if !err.IsNull() {
		return
	}
	command_ended = false
	go func() {
		for !command_ended {
			io.Copy(stdin, bytes.NewBufferString(<-input))
		}
	}()
	err.Err = session.Wait()
	command_ended = true
	return
}
コード例 #5
0
ファイル: formats.go プロジェクト: influx6/goproj
//NewSSHClientSession creates a new ssh session instance
func NewSSHClientSession(s *ssh.Session, in io.Reader) *SSHClientSession {
	out := new(bytes.Buffer)
	err := new(bytes.Buffer)
	s.Stdin = in
	s.Stdout = out
	s.Stderr = err
	return &SSHClientSession{s, in, out, err}
}
コード例 #6
0
ファイル: sshutil.go プロジェクト: ymomoi/vuls
func sshExecNative(c conf.ServerInfo, cmd string, sudo bool) (result execResult) {
	result.Servername = c.ServerName
	result.Host = c.Host
	result.Port = c.Port

	var client *ssh.Client
	var err error
	if client, err = sshConnect(c); err != nil {
		result.Error = err
		result.ExitStatus = 999
		return
	}
	defer client.Close()

	var session *ssh.Session
	if session, err = client.NewSession(); err != nil {
		result.Error = fmt.Errorf(
			"Failed to create a new session. servername: %s, err: %s",
			c.ServerName, err)
		result.ExitStatus = 999
		return
	}
	defer session.Close()

	// http://blog.ralch.com/tutorial/golang-ssh-connection/
	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", 400, 256, modes); err != nil {
		result.Error = fmt.Errorf(
			"Failed to request for pseudo terminal. servername: %s, err: %s",
			c.ServerName, err)
		result.ExitStatus = 999
		return
	}

	var stdoutBuf, stderrBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	session.Stderr = &stderrBuf

	cmd = decolateCmd(c, cmd, sudo)
	if err := session.Run(cmd); err != nil {
		if exitErr, ok := err.(*ssh.ExitError); ok {
			result.ExitStatus = exitErr.ExitStatus()
		} else {
			result.ExitStatus = 999
		}
	} else {
		result.ExitStatus = 0
	}

	result.Stdout = stdoutBuf.String()
	result.Stderr = stderrBuf.String()
	result.Cmd = strings.Replace(cmd, "\n", "", -1)
	return
}
コード例 #7
0
ファイル: ssh.go プロジェクト: apigee/henchman
func (sshTransport *SSHTransport) execCmd(session *ssh.Session, cmd string) (*bytes.Buffer, error) {
	modes := ssh.TerminalModes{
		ECHO:          0,
		TTY_OP_ISPEED: 14400,
		TTY_OP_OSPEED: 14400,
	}
	if err := session.RequestPty("xterm", 100, 80, modes); err != nil {
		return nil, HenchErr(err, nil, "request for psuedo terminal failed")
	}

	var stdoutbuf bytes.Buffer
	session.Stdout = &stdoutbuf
	err := session.Run(cmd)
	if err != nil {
		return nil, HenchErr(err, map[string]interface{}{
			"output": string(stdoutbuf.String()),
		}, "While retrieving output from ssh")
	}
	return &stdoutbuf, nil
}
コード例 #8
-1
ファイル: commands.go プロジェクト: grengojbo/sw-cli
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())
}
コード例 #9
-1
ファイル: sshutil.go プロジェクト: Rompei/vuls
func sshExecNative(c conf.ServerInfo, cmd string, sudo bool, log ...*logrus.Entry) (result sshResult) {
	logger := getSSHLogger(log...)

	cmd = decolateCmd(c, cmd, sudo)
	logger.Debugf("Command: %s",
		strings.Replace(maskPassword(cmd, c.Password), "\n", "", -1))

	var client *ssh.Client
	var err error
	client, err = sshConnect(c)
	defer client.Close()

	var session *ssh.Session
	if session, err = client.NewSession(); err != nil {
		logger.Errorf("Failed to new session. err: %s, c: %s",
			err,
			pp.Sprintf("%v", c))
		result.ExitStatus = 999
		return
	}
	defer session.Close()

	// http://blog.ralch.com/tutorial/golang-ssh-connection/
	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", 400, 256, modes); err != nil {
		logger.Errorf("Failed to request for pseudo terminal. err: %s, c: %s",
			err,
			pp.Sprintf("%v", c))

		result.ExitStatus = 999
		return
	}

	var stdoutBuf, stderrBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	session.Stderr = &stderrBuf

	if err := session.Run(cmd); err != nil {
		if exitErr, ok := err.(*ssh.ExitError); ok {
			result.ExitStatus = exitErr.ExitStatus()
		} else {
			result.ExitStatus = 999
		}
	} else {
		result.ExitStatus = 0
	}

	result.Stdout = stdoutBuf.String()
	result.Stderr = stderrBuf.String()
	result.Host = c.Host
	result.Port = c.Port

	logger.Debugf(
		"SSH executed. cmd: %s, err: %#v, status: %d\nstdout: \n%s\nstderr: \n%s",
		maskPassword(cmd, c.Password), err, result.ExitStatus, result.Stdout, result.Stderr)

	return
}
コード例 #10
-5
ファイル: commands.go プロジェクト: grengojbo/sw-cli
func runToHost(user, pass, host, cmd string) {
	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()

	var stdoutBuf bytes.Buffer
	session.Stdout = &stdoutBuf
	session.Run(cmd)

	fmt.Println(stdoutBuf.String())
}